Cron with interval 1 second, yaml based configs http://gogocron.blindage.org/
go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package main
  2. import (
  3. "context"
  4. "io"
  5. "io/ioutil"
  6. "log"
  7. "os"
  8. "os/exec"
  9. "reflect"
  10. "syscall"
  11. "time"
  12. "github.com/kr/pty" // required to run commands as other users
  13. )
  14. // readTasksDirectory get contents of target directory
  15. func readTasksDirectory(path string) []string {
  16. var res []string
  17. files, err := ioutil.ReadDir(path)
  18. if err != nil {
  19. log.Fatal(err)
  20. }
  21. for _, file := range files {
  22. //log.Println(file.Name())
  23. res = append(res, path+"/"+file.Name())
  24. }
  25. return res
  26. }
  27. func inArray(val interface{}, array interface{}) (exists bool, index int) {
  28. exists = false
  29. index = -1
  30. switch reflect.TypeOf(array).Kind() {
  31. case reflect.Slice:
  32. s := reflect.ValueOf(array)
  33. for i := 0; i < s.Len(); i++ {
  34. if reflect.DeepEqual(val, s.Index(i).Interface()) == true {
  35. index = i
  36. exists = true
  37. return
  38. }
  39. }
  40. }
  41. return
  42. }
  43. // run commands
  44. func runCmd(ctx context.Context, env []string, cmdname string, params ...string) error {
  45. // Read graceful stop value from context
  46. var gracefulStop string
  47. if v := ctx.Value("gracefulstopvalue"); v != nil {
  48. gracefulStop = v.(string)
  49. } else {
  50. gracefulStop = "10s"
  51. }
  52. gracefulStopSeconds, err := time.ParseDuration(gracefulStop)
  53. if err != nil {
  54. log.Fatalln("Error parsing gracefulstop value as time duration")
  55. }
  56. //log.Println("Executing", cmdname, params)
  57. cmd := exec.Command(cmdname, params...)
  58. // set env variables
  59. cmd.Env = append(cmd.Env, os.Environ()...)
  60. cmd.Env = append(cmd.Env, env...)
  61. // run su command in PTY
  62. f, err := pty.Start(cmd)
  63. if err != nil {
  64. panic(err)
  65. }
  66. // set stdout, stderr
  67. if v, _ := inArray("LOGTYPE=full", env); v == true {
  68. io.Copy(os.Stdout, f)
  69. io.Copy(os.Stderr, f)
  70. }
  71. // Use a channel to signal completion so we can use a select statement
  72. done := make(chan error)
  73. go func() { done <- cmd.Wait() }()
  74. // The select statement allows us to execute based on which channel
  75. // we get a message from first.
  76. select {
  77. case <-ctx.Done():
  78. // Timeout happened first, kill the process and print a message.
  79. cmd.Process.Signal(syscall.SIGTERM)
  80. // gracefulStopSeconds is a time between SIGTERM and SIGKILL, default 10s, minimum 1s
  81. time.AfterFunc(gracefulStopSeconds, func() {
  82. if !cmd.ProcessState.Exited() {
  83. cmd.Process.Kill()
  84. }
  85. })
  86. return ctx.Err()
  87. case err := <-done:
  88. return err
  89. }
  90. return nil
  91. }