-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #986 from hashicorp/scripts
Implement script checks
- Loading branch information
Showing
65 changed files
with
1,388 additions
and
3,728 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package consul | ||
|
||
import ( | ||
"log" | ||
"math/rand" | ||
"sync" | ||
"time" | ||
|
||
cstructs "github.com/hashicorp/nomad/client/driver/structs" | ||
) | ||
|
||
// CheckRunner runs a given check in a specific interval and update a | ||
// corresponding Consul TTL check | ||
type CheckRunner struct { | ||
check Check | ||
runCheck func(Check) | ||
logger *log.Logger | ||
stop bool | ||
stopCh chan struct{} | ||
stopLock sync.Mutex | ||
|
||
started bool | ||
startedLock sync.Mutex | ||
} | ||
|
||
// NewCheckRunner configures and returns a CheckRunner | ||
func NewCheckRunner(check Check, runCheck func(Check), logger *log.Logger) *CheckRunner { | ||
cr := CheckRunner{ | ||
check: check, | ||
runCheck: runCheck, | ||
logger: logger, | ||
stopCh: make(chan struct{}), | ||
} | ||
return &cr | ||
} | ||
|
||
// Start is used to start the check. The check runs until stop is called | ||
func (r *CheckRunner) Start() { | ||
r.startedLock.Lock() | ||
defer r.startedLock.Unlock() | ||
if r.started { | ||
return | ||
} | ||
r.stopLock.Lock() | ||
defer r.stopLock.Unlock() | ||
go r.run() | ||
r.started = true | ||
} | ||
|
||
// Stop is used to stop the check. | ||
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 (r *CheckRunner) run() { | ||
// Get the randomized initial pause time | ||
initialPauseTime := randomStagger(r.check.Interval()) | ||
r.logger.Printf("[DEBUG] agent: pausing %v before first invocation of %s", initialPauseTime, r.check.ID()) | ||
next := time.NewTimer(initialPauseTime) | ||
for { | ||
select { | ||
case <-next.C: | ||
r.runCheck(r.check) | ||
next.Reset(r.check.Interval()) | ||
case <-r.stopCh: | ||
next.Stop() | ||
return | ||
} | ||
} | ||
} | ||
|
||
// Check is an interface which check providers can implement for Nomad to run | ||
type Check interface { | ||
Run() *cstructs.CheckResult | ||
ID() string | ||
Interval() time.Duration | ||
} | ||
|
||
// Returns a random stagger interval between 0 and the duration | ||
func randomStagger(intv time.Duration) time.Duration { | ||
return time.Duration(uint64(rand.Int63()) % uint64(intv)) | ||
} |
Oops, something went wrong.