diff --git a/command/agent/consul/check.go b/command/agent/consul/check.go index 28df291f6..551f94b6f 100644 --- a/command/agent/consul/check.go +++ b/command/agent/consul/check.go @@ -47,6 +47,13 @@ func (r *CheckRunner) Start() { r.started = true } +// Started returns if the check runner has started running +func (r *CheckRunner) Started() bool { + r.startedLock.Lock() + defer r.startedLock.Unlock() + return r.started +} + // Stop is used to stop the check. func (r *CheckRunner) Stop() { r.stopLock.Lock() diff --git a/command/agent/consul/syncer.go b/command/agent/consul/syncer.go index 736e15c8e..8ac66fd9f 100644 --- a/command/agent/consul/syncer.go +++ b/command/agent/consul/syncer.go @@ -394,7 +394,7 @@ func (c *Syncer) syncChecks() error { } // Synchronize checks with Consul - missingChecks, _, changedChecks, staleChecks := c.calcChecksDiff(consulChecks) + missingChecks, existingChecks, changedChecks, staleChecks := c.calcChecksDiff(consulChecks) for _, check := range missingChecks { if err := c.registerCheck(check); err != nil { mErr.Errors = append(mErr.Errors, err) @@ -403,6 +403,9 @@ func (c *Syncer) syncChecks() error { c.trackedChecks[consulCheckID(check.ID)] = check c.registryLock.Unlock() } + for _, check := range existingChecks { + c.ensureCheckRunning(check) + } for _, check := range changedChecks { // NOTE(sean@): Do we need to deregister the check before // re-registering it? Not deregistering to avoid missing the @@ -684,6 +687,16 @@ func (c *Syncer) registerCheck(chkReg *consul.AgentCheckRegistration) error { return c.client.Agent().CheckRegister(chkReg) } +// ensureCheckRunning starts the check runner for a check if it's not already running +func (c *Syncer) ensureCheckRunning(chk *consul.AgentCheckRegistration) { + c.registryLock.RLock() + defer c.registryLock.RUnlock() + if cr, ok := c.checkRunners[consulCheckID(chk.ID)]; ok && !cr.Started() { + c.logger.Printf("[DEBUG] consul.syncer: starting runner for existing check. %v", chk.ID) + cr.Start() + } +} + // createCheckReg creates a Check that can be registered with Nomad. It also // creates a Nomad check for the check types that it can handle. func (c *Syncer) createCheckReg(check *structs.ServiceCheck, serviceReg *consul.AgentServiceRegistration) (*consul.AgentCheckRegistration, error) {