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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. //log.Printf(string(yamldata))
  44. marshErr := yaml.Unmarshal(yamldata, &config)
  45. if marshErr != nil {
  46. log.Fatalf("Task file malformed: %v", marshErr)
  47. }
  48. return config
  49. }
  50. // loadTasks() searches for config files and reading them to cronTasks
  51. // used once at start and every HUP signal
  52. func loadTasks(gogoconfig cronConfig) cronTasks {
  53. var tasks cronTasks
  54. configFiles := readTasksDirectory(gogoconfig.TasksLocation)
  55. log.Printf("Configuration files found: %v", configFiles)
  56. for _, configFile := range configFiles {
  57. config := readTaskConfigFile(configFile)
  58. //log.Printf("Parsed %#v", config)
  59. if config.Name != "" && len(config.Commands) > 0 {
  60. tasks = append(tasks, config)
  61. } else {
  62. log.Printf("Config file %v malformed", configFile)
  63. }
  64. }
  65. return tasks
  66. }