12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- package main
-
- import (
- "io/ioutil"
- "log"
- "time"
-
- yaml "gopkg.in/yaml.v2"
- )
-
- // loadConfig loads gogocron settings from preallocated yaml config file
- func loadConfig() cronConfig {
- var gogoconfig cronConfig
-
- yamldata, err := ioutil.ReadFile(cronDefaultConfigLocation)
- if err != nil {
- log.Fatalf("Error opening file %v", err)
- }
-
- marshErr := yaml.Unmarshal(yamldata, &gogoconfig)
- if marshErr != nil {
- log.Fatalf("Task file malformed: %v", marshErr)
- }
-
- gogoconfig.Precision, _ = time.ParseDuration(cronDefaultPrecision)
-
- if gogoconfig.TasksLocation == "" {
- gogoconfig.TasksLocation = cronDefaultTasksLocation
- }
-
- if gogoconfig.PrecisionString != "" {
- // config value was not empty, trying to parse it
- if precision, err := time.ParseDuration(gogoconfig.PrecisionString); err == nil {
- // check if precision between 10ms and 500ms
- if precision >= time.Duration(10000000) && precision <= time.Duration(500000000) {
- // ok! using value from config file
- gogoconfig.Precision = precision
- log.Printf("Precision was set to %s", gogoconfig.PrecisionString)
- }
- }
-
- }
-
- return gogoconfig
- }
-
- // readTaskConfigFile reads config file and returns cronTask struct back to loadTasks()
- func readTaskConfigFile(path string) cronTask {
- var config cronTask
-
- yamldata, err := ioutil.ReadFile(path)
- if err != nil {
- log.Fatalf("Error opening file %v", err)
- }
-
- marshErr := yaml.Unmarshal(yamldata, &config)
- if marshErr != nil {
- log.Fatalf("Task file malformed: %v", marshErr)
- }
-
- if config.GracefulStop == "" {
- config.GracefulStop = "10s"
- }
-
- return config
- }
-
- // loadTasks() searches for config files and reading them to cronTasks
- // used once at start and every HUP signal
- func loadTasks(gogoconfig cronConfig) cronTasks {
- var tasks cronTasks
-
- configFiles := readTasksDirectory(gogoconfig.TasksLocation)
- log.Printf("Configuration files found: %v", configFiles)
- for _, configFile := range configFiles {
- config := readTaskConfigFile(configFile)
- //log.Printf("Parsed %#v", config)
- if config.Name != "" && len(config.Commands) > 0 {
- tasks = append(tasks, config)
- } else {
- log.Printf("Config file %v malformed", configFile)
- }
- }
- return tasks
- }
|