-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement script checks #986
Merged
Changes from 19 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
4faf16b
Added implementation to run checks for docker, exec and raw_exec
diptanu 42dc8be
Added a check type for consul service to delegate certain checks
diptanu 2b60e8b
Updated consul related dependencies
diptanu 1e4da82
Running script checks periodically
diptanu f1cf87b
Enabling script checks
diptanu e8995be
Fixed merge conflicts
diptanu 588666d
Introducing ConsulContext
diptanu fe9e271
Creating the docker driver in the executor properly
diptanu 52d4b01
Added a test for the exec script check
diptanu 8342d7d
Updated the vendored copy of github.com/fsouza/go-dockerclient
diptanu 62249fe
Added an impl for Nomad Checks
diptanu 12dc439
Changing the logic of keep services
diptanu 52f7f93
Added some docs
diptanu 0fed52c
Removing non relevant tests
diptanu 9a27bf4
Added some more docs to the executor
diptanu e866d3b
Vendoring circbuf
diptanu 7454ffd
Renamed NomadChecks to CheckRunner
diptanu 4c27660
Renamed NomadChecks to CheckRunner and a fix for checkrunner start
diptanu f8db6a4
Using tickers instead of creating new timers
diptanu bfc2a0d
using switch to determine the state of checks
diptanu e0b9f03
Using a single timer to run checks
diptanu 644710a
Added more tests for the checks
diptanu f1d9b2c
Removing the container after running script check
diptanu d4a5f07
Moved the dockerIsConnected to testutils
diptanu 6ee99af
Fixing the exec script check to run within the chroot
diptanu 59e91e1
Using latest busybox
diptanu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,87 @@ | ||
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.After(initialPauseTime) | ||
for { | ||
select { | ||
case <-next: | ||
r.runCheck(r.check) | ||
next = time.After(r.check.Interval()) | ||
case <-r.stopCh: | ||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a duplicate from somewhere. Can you put it in a utils package or something to share? |
||
return time.Duration(uint64(rand.Int63()) % uint64(intv)) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dadgar Can't use a single timer or ticker here since we are sleeping for a different duration for the first time, and from then onwards sleeping for the duration of the check interval.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes you can: https://golang.org/pkg/time/#Timer.Reset
Reset takes a duration
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh cool.