// runChangeFunc will manage the state of the manager and enforce PreRunDelay and QuietPeriod func (m *Manager) runChangeFunc(skipQuiet bool) { // mark that the manger is currently running to block re-fireing the run based on // changes that come in during a run. m.SetRunning(true) log.Debug("Setting running status to true") // if we have a PreRunDelay, sleep for that period of time before firing the ChangeFunc if m.PreRunDelay { log.Debugf("pre-run flag is set to true, pausing for %v seconds to wait for more changes", m.PreRunDelay) time.Sleep(time.Duration(m.PreRunDelayTime) * time.Second) } // run the change func and log the result if there is an error err := m.ChangeFunc() if err != nil { log.Infof("Running change func resulted in error: %v", err) } // clear the runnign flag m.SetRunning(false) log.Debug("Clearing running state") // if we need to have a quiet period, and the skipQuiet period override is not set // run the quiet period if m.QuietPeriod && !skipQuiet { // set that we are in a quiet period m.SetQuiet(true) log.Debugf("entering quiet period of %v", m.QuietPeriodTime) // sleep for the amount of time in QuietPeriodTime time.Sleep(time.Duration(m.QuietPeriodTime) * time.Second) // clear the quiet state m.SetQuiet(false) log.Debug("exiting quiet period") // check to see if more changes happened while we were sleeping if m.ChangeHappened() { log.Info("More changes happened during process run, so running again") // if so, run the change func with the quiet period delayed. m.runChangeFunc(true) } } }
// initAndListen will start a go func to listen on the Manager.Change channel and update the // ChangeHappened state anytime a chance occurs. It will then check to see if the ChangeFunc // can be ran, and if so - will run it. func (m *Manager) initAndListen() { go func() { log.Info("starting listener...") for { // listen to the change channel <-m.Change log.Debug("recieved message from change channel") // set changed happened when a message is recieved log.Debug("setting change happend to true") m.SetChangeHappened(true) // check to see if we can run a cycle of the ChangeFunc if m.canRunChangeFunc() { log.Debug("can run change func, so calling as go routine") // if so, launch it in a goroutine. go m.runChangeFunc(false) } } }() }