From e12e5f7f68f61a90f0dd4b28bea20b9af3e9e64b Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Fri, 7 Feb 2014 12:19:56 -0800 Subject: [PATCH] agent: adding ability to reload services and checks --- command/agent/agent.go | 10 ++++++++++ command/agent/command.go | 34 +++++++++++++++++++++++++++++++++- command/agent/local.go | 4 ++-- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/command/agent/agent.go b/command/agent/agent.go index 28def6f287..dcbd462598 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -314,6 +314,16 @@ func (a *Agent) StartSync() { go a.state.antiEntropy(a.shutdownCh) } +// PauseSync is called to pause anti-entropy while bulk changes are make +func (a *Agent) PauseSync() { + a.state.Pause() +} + +// ResumeSync is called to to unpause anti-entropy after bulk changes are make +func (a *Agent) ResumeSync() { + a.state.Resume() +} + // AddService is used to add a service entry. // This entry is persistent and the agent will make a best effort to // ensure it is registered diff --git a/command/agent/command.go b/command/agent/command.go index 9052c8a92b..2ca2f015cd 100644 --- a/command/agent/command.go +++ b/command/agent/command.go @@ -323,7 +323,39 @@ func (c *Command) handleReload(config *Config) *Config { newConf.LogLevel = config.LogLevel } - // TODO: Update the services and checks + // Bulk update the services and checks + c.agent.PauseSync() + defer c.agent.ResumeSync() + + // Deregister the old services + for _, service := range config.Services { + ns := service.NodeService() + c.agent.RemoveService(ns.ID) + } + + // Deregister the old checks + for _, check := range config.Checks { + health := check.HealthCheck(config.NodeName) + c.agent.RemoveCheck(health.CheckID) + } + + // Register the services + for _, service := range newConf.Services { + ns := service.NodeService() + chkType := service.CheckType() + if err := c.agent.AddService(ns, chkType); err != nil { + c.Ui.Error(fmt.Sprintf("Failed to register service '%s': %v", service.Name, err)) + } + } + + // Register the checks + for _, check := range newConf.Checks { + health := check.HealthCheck(config.NodeName) + chkType := &check.CheckType + if err := c.agent.AddCheck(health, chkType); err != nil { + c.Ui.Error(fmt.Sprintf("Failed to register check '%s': %v %v", check.Name, err, check)) + } + } return newConf } diff --git a/command/agent/local.go b/command/agent/local.go index 6b5854bfbe..cef668966c 100644 --- a/command/agent/local.go +++ b/command/agent/local.go @@ -97,8 +97,8 @@ func (l *localState) Pause() { atomic.StoreInt32(&l.paused, 1) } -// Unpause is used to resume state syncronization -func (l *localState) Unpause() { +// Resume is used to resume state syncronization +func (l *localState) Resume() { atomic.StoreInt32(&l.paused, 0) l.changeMade() }