func TestOutputLogfile(t *testing.T) { path := utils.Basepath("./output.test.log") msg := "test output" err := SetLogfile(path) if err != nil { t.Log("SetLogfile() should not return an error") t.Log("err: ", err) t.FailNow() } SetDebug(true) Debug(msg) b, err := ioutil.ReadFile(path) if err != nil { t.Log("ioutil.ReadFile() should not return an error") t.Log("err: ", err) t.FailNow() } if !strings.Contains(string(b), msg) { t.Log("logfile should contain the debug-message") t.Fail() } err = os.Remove(path) if err != nil { t.Log("os.Remove() should not return an error") t.Log("err: ", err) t.Fail() } }
// New reads the provided file and returns a Config instance with the values from it. // It may also return an error, when the file doesn't exist, or the content could not be parsed. func New(path string) (c Config, err error) { file, err := ioutil.ReadFile(path) if err != nil { return } _, err = toml.Decode(string(file), &c) if err != nil { return } // take care of default-value if c.RedisNetwork != "tcp" || c.RedisNetwork != "udp" { c.RedisNetwork = "tcp" } // ensure reasonable interval-values if c.IntervalMin < 100 { c.IntervalMin = 100 } if c.IntervalMax < c.IntervalMin { c.IntervalMax = c.IntervalMin } if c.IntervalFactor < 1 { c.IntervalFactor = 1 } // make sure that the backoff values are positive if c.BackoffMin < 0 { c.BackoffMin = 0 } if c.BackoffMax < 0 { c.BackoffMax = 0 } if c.BackoffFactor < 0 { c.BackoffFactor = 0 } for taskType, task := range c.Tasks { task.Type = taskType task.Script = utils.Basepath(task.Script) if task.Workers < 1 { task.Workers = 1 } // override the failed-task-ttl if not set on this level if task.FailedTasksTTL == 0 && c.FailedTasksTTL > 0 { task.FailedTasksTTL = c.FailedTasksTTL } // if general error-backoff values are set, but not the task-specific // ones, then we'll 'override' them here. if c.BackoffEnabled { task.BackoffEnabled = true } if task.BackoffMin == 0 { task.BackoffMin = c.BackoffMin } if task.BackoffMax == 0 { task.BackoffMax = c.BackoffMax } if task.BackoffFactor == 0 { task.BackoffFactor = c.BackoffFactor } // ensure reasonable values for error-backoff if task.BackoffMin < 100 { task.BackoffMin = 100 } if task.BackoffMax < task.BackoffMin { task.BackoffMax = task.BackoffMin } if task.BackoffFactor < 1 { task.BackoffFactor = 1 } c.Tasks[taskType] = task } return }
func main() { flag.Parse() if cli.version == true { fmt.Printf("Gordon version %s\n", GordonVersion) os.Exit(0) } // When no configuration file was passed as a flag, use the default location. if cli.config == "" { cli.config = utils.Basepath(config.DefaultConfig) } conf, err := config.New(cli.config) // When test-flag is set, respond accordingly if cli.test { if err != nil { fmt.Println("Configuration is invalid:", err) } else { fmt.Println("Configuration is valid") } os.Exit(0) } if err != nil { log.Fatal("Configuration is invalid:", err) } stats.GordonVersion = GordonVersion output.SetDebug(cli.verbose) output.SetErrorScript(conf.ErrorScript) output.SetTempDir(utils.Basepath(conf.TempDir)) // Set logfile for output, when configured if conf.Logfile != "" { err = output.SetLogfile(utils.Basepath(conf.Logfile)) if err != nil { log.Fatal("output.SetLogfile(): ", err) } } taskqueue.Start(conf) // If the StatsInterface was set, start the HTTP-server for it. if conf.Stats.Interface != "" { if conf.Stats.Pattern == "" { conf.Stats.Pattern = "/" } go func() { if err := stats.Serve(conf.Stats); err != nil { output.NotifyError("stats.Serve():", err) } }() } // Start another go-routine to initiate the graceful shutdown of all taskqueue-workers, // when the application shall be terminated. cc := make(chan os.Signal) signal.Notify(cc, os.Interrupt, os.Kill, syscall.SIGTERM) go func() { <-cc output.Debug("Stopping taskqueue") taskqueue.Stop() }() output.Debug("Up and waiting for tasks") taskqueue.Wait() }