func main() { conf, err := getConfig() if err != nil { log.Fatal(err) } manifest, err := manifest.Get(conf.ManifestURL, conf.DefaultSampleInterval) if err != nil { log.Fatal(err) } if conf.RampupSensors { manifest.GenerateRampupDelays(conf.DefaultSampleInterval) } c := canary.New(createPublishers()) c.Config = conf c.Manifest = manifest // Start canary and block in the signal handler c.Run() u, _ := time.ParseDuration("0s") if c.Config.ReloadInterval != u { go c.StartAutoReload(c.Config.ReloadInterval) } c.SignalHandler() }
func (c *Canary) StartAutoReload(interval time.Duration) { t := time.NewTicker(interval) for { <-t.C manifest, err := manifest.Get(c.Config.ManifestURL, c.Config.DefaultSampleInterval) if err != nil { log.Fatal(err) } if manifest.Hash != c.Manifest.Hash { c.ReloadChan <- manifest } } }
func (c *Canary) SignalHandler() { signalChan := make(chan os.Signal, 1) signal.Notify(signalChan, syscall.SIGINT) signal.Notify(signalChan, syscall.SIGHUP) for s := range signalChan { switch s { case syscall.SIGINT: for _, sensor := range c.Sensors { sensor.Stop() } os.Exit(0) case syscall.SIGHUP: manifest, err := manifest.Get(c.Config.ManifestURL, c.Config.DefaultSampleInterval) if err != nil { log.Fatal(err) } // Split reload logic into reloader() as to allow other things to trigger a manifest reload c.ReloadChan <- manifest } } }