Renamed NomadChecks to CheckRunner

This commit is contained in:
Diptanu Choudhury 2016-03-24 21:17:33 -07:00
parent 7ea42ed615
commit 966b4ca26f
2 changed files with 34 additions and 34 deletions

View file

@ -9,9 +9,9 @@ import (
cstructs "github.com/hashicorp/nomad/client/driver/structs"
)
// NomadCheck runs a given check in a specific interval and update a
// CheckRunner runs a given check in a specific interval and update a
// corresponding Consul TTL check
type NomadCheck struct {
type CheckRunner struct {
check Check
runCheck func(Check)
logger *log.Logger
@ -23,8 +23,8 @@ type NomadCheck struct {
startedLock sync.Mutex
}
// NewNomadCheck configures and returns a NomadCheck
func NewNomadCheck(check Check, runCheck func(Check), logger *log.Logger) *NomadCheck {
// NewCheckRunner configures and returns a CheckRunner
func NewCheckRunner(check Check, runCheck func(Check), logger *log.Logger) *CheckRunner {
nc := NomadCheck{
check: check,
runCheck: runCheck,
@ -35,40 +35,40 @@ func NewNomadCheck(check Check, runCheck func(Check), logger *log.Logger) *Nomad
}
// Start is used to start the check. The check runs until stop is called
func (n *NomadCheck) Start() {
n.startedLock.Lock()
if n.started {
func (r *CheckRunner) Start() {
r.startedLock.Lock()
if r.started {
return
}
n.started = true
n.stopLock.Lock()
defer n.stopLock.Unlock()
n.stopCh = make(chan struct{})
go n.run()
r.started = true
r.stopLock.Lock()
defer r.stopLock.Unlock()
r.stopCh = make(chan struct{})
go r.run()
}
// Stop is used to stop the check.
func (n *NomadCheck) Stop() {
n.stopLock.Lock()
defer n.stopLock.Unlock()
if !n.stop {
n.stop = true
close(n.stopCh)
func (r *CheckRunner) Stop() {
r.stopLock.Lock()
defer r.stopLock.Unlock()
if !r.stop {
r.stop = true
close(r.stopCh)
}
}
// run is invoked by a goroutine to run until Stop() is called
func (n *NomadCheck) run() {
func (r *CheckRunner) run() {
// Get the randomized initial pause time
initialPauseTime := randomStagger(n.check.Interval())
n.logger.Printf("[DEBUG] agent: pausing %v before first invocation of %s", initialPauseTime, n.check.ID())
initialPauseTime := randomStagger(r.check.Interval())
r.logger.Printf("[DEBUG] agent: pausing %v before first invocation of %s", initialPauseTime, r.check.ID())
next := time.After(initialPauseTime)
for {
select {
case <-next:
n.runCheck(n.check)
next = time.After(n.check.Interval())
case <-n.stopCh:
r.runCheck(r.check)
next = time.After(r.check.Interval())
case <-r.stopCh:
return
}
}

View file

@ -28,7 +28,7 @@ type ConsulService struct {
trackedServices map[string]*consul.AgentService
trackedChecks map[string]*consul.AgentCheckRegistration
nomadChecks map[string]*NomadCheck
checkRunners map[string]*CheckRunner
logger *log.Logger
@ -188,8 +188,8 @@ func (c *ConsulService) Shutdown() error {
c.shutdownLock.Unlock()
// Stop all the checks that nomad is running
for _, nc := range c.nomadChecks {
nc.Stop()
for _, cr := range c.checkRunners {
cr.Stop()
}
// de-register all the services from consul
@ -226,8 +226,8 @@ func (c *ConsulService) KeepServices(services map[string]struct{}) error {
// registerCheck registers a check definition with Consul
func (c *ConsulService) registerCheck(chkReg *consul.AgentCheckRegistration) error {
if nc, ok := c.nomadChecks[chkReg.ID]; ok {
nc.Start()
if cr, ok := c.checkRunners[chkReg.ID]; ok {
cr.Start()
}
return c.client.Agent().CheckRegister(chkReg)
}
@ -267,8 +267,8 @@ func (c *ConsulService) createCheckReg(check *structs.ServiceCheck, service *con
if err != nil {
return nil, err
}
nc := NewNomadCheck(chk, c.runCheck, c.logger)
c.nomadChecks[chk.ID()] = nc
cr := NewCheckRunner(chk, c.runCheck, c.logger)
c.checkRunners[chk.ID()] = cr
}
return &chkReg, nil
}
@ -313,9 +313,9 @@ func (c *ConsulService) deregisterService(ID string) error {
// deregisterCheck de-registers a check with a given ID from Consul.
func (c *ConsulService) deregisterCheck(ID string) error {
// Deleting the nomad check
if nc, ok := c.nomadChecks[ID]; ok {
nc.Stop()
delete(c.nomadChecks, ID)
if cr, ok := c.checkRunners[ID]; ok {
cr.Stop()
delete(c.checkRunners, ID)
}
// Deleteting from consul