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.

config.go 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package main
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "time"
  6. yaml "gopkg.in/yaml.v2"
  7. )
  8. // loadConfig loads gogocron settings from preallocated yaml config file
  9. func loadConfig() cronConfig {
  10. var gogoconfig cronConfig
  11. yamldata, err := ioutil.ReadFile(cronDefaultConfigLocation)
  12. if err != nil {
  13. log.Fatalf("Error opening file %v", err)
  14. }
  15. marshErr := yaml.Unmarshal(yamldata, &gogoconfig)
  16. if marshErr != nil {
  17. log.Fatalf("Task file malformed: %v", marshErr)
  18. }
  19. gogoconfig.Precision, _ = time.ParseDuration(cronDefaultPrecision)
  20. if gogoconfig.TasksLocation == "" {
  21. gogoconfig.TasksLocation = cronDefaultTasksLocation
  22. }
  23. if gogoconfig.PrecisionString != "" {
  24. // config value was not empty, trying to parse it
  25. if precision, err := time.ParseDuration(gogoconfig.PrecisionString); err == nil {
  26. // check if precision between 10ms and 500ms
  27. if precision >= time.Duration(10000000) && precision <= time.Duration(500000000) {
  28. // ok! using value from config file
  29. gogoconfig.Precision = precision
  30. log.Printf("Precision was set to %s", gogoconfig.PrecisionString)
  31. }
  32. }
  33. }
  34. return gogoconfig
  35. }
  36. // readTaskConfigFile reads config file and returns cronTask struct back to loadTasks()
  37. func readTaskConfigFile(path string) cronTask {
  38. var config cronTask
  39. yamldata, err := ioutil.ReadFile(path)
  40. if err != nil {
  41. log.Fatalf("Error opening file %v", err)
  42. }
  43. marshErr := yaml.Unmarshal(yamldata, &config)
  44. if marshErr != nil {
  45. log.Fatalf("Task file malformed: %v", marshErr)
  46. }
  47. if config.GracefulStop == "" {
  48. config.GracefulStop = "10s"
  49. }
  50. return config
  51. }
  52. // loadTasks() searches for config files and reading them to cronTasks
  53. // used once at start and every HUP signal
  54. func loadTasks(gogoconfig cronConfig) cronTasks {
  55. var tasks cronTasks
  56. configFiles := readTasksDirectory(gogoconfig.TasksLocation)
  57. log.Printf("Configuration files found: %v", configFiles)
  58. for _, configFile := range configFiles {
  59. config := readTaskConfigFile(configFile)
  60. //log.Printf("Parsed %#v", config)
  61. if config.Name != "" && len(config.Commands) > 0 {
  62. tasks = append(tasks, config)
  63. } else {
  64. log.Printf("Config file %v malformed", configFile)
  65. }
  66. }
  67. return tasks
  68. }