Merge pull request #4034 from hashicorp/b-0-attempt-restarts

client: always mark exited sys/svc allocs as failed
This commit is contained in:
Michael Schurter 2018-03-27 16:26:58 -07:00 committed by GitHub
commit 8c5f5ac8b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 2 deletions

View file

@ -127,10 +127,15 @@ func (r *RestartTracker) GetState() (string, time.Duration) {
// Hot path if no attempts are expected // Hot path if no attempts are expected
if r.policy.Attempts == 0 { if r.policy.Attempts == 0 {
r.reason = ReasonNoRestartsAllowed r.reason = ReasonNoRestartsAllowed
if r.waitRes != nil && r.waitRes.Successful() {
// If the task does not restart on a successful exit code and
// the exit code was successful: terminate.
if !r.onSuccess && r.waitRes != nil && r.waitRes.Successful() {
return structs.TaskTerminated, 0 return structs.TaskTerminated, 0
} }
// Task restarts even on a successful exit code but no restarts
// allowed.
return structs.TaskNotRestarting, 0 return structs.TaskNotRestarting, 0
} }

View file

@ -88,9 +88,32 @@ func TestClient_RestartTracker_ZeroAttempts(t *testing.T) {
t.Parallel() t.Parallel()
p := testPolicy(true, structs.RestartPolicyModeFail) p := testPolicy(true, structs.RestartPolicyModeFail)
p.Attempts = 0 p.Attempts = 0
// Test with a non-zero exit code
rt := newRestartTracker(p, structs.JobTypeService) rt := newRestartTracker(p, structs.JobTypeService)
if state, when := rt.SetWaitResult(testWaitResult(1)).GetState(); state != structs.TaskNotRestarting { if state, when := rt.SetWaitResult(testWaitResult(1)).GetState(); state != structs.TaskNotRestarting {
t.Fatalf("expect no restart, got restart/delay: %v", when) t.Fatalf("expect no restart, got restart/delay: %v/%v", state, when)
}
// Even with a zero (successful) exit code non-batch jobs should exit
// with TaskNotRestarting
rt = newRestartTracker(p, structs.JobTypeService)
if state, when := rt.SetWaitResult(testWaitResult(0)).GetState(); state != structs.TaskNotRestarting {
t.Fatalf("expect no restart, got restart/delay: %v/%v", state, when)
}
// Batch jobs with a zero exit code and 0 attempts *do* exit cleanly
// with Terminated
rt = newRestartTracker(p, structs.JobTypeBatch)
if state, when := rt.SetWaitResult(testWaitResult(0)).GetState(); state != structs.TaskTerminated {
t.Fatalf("expect terminated, got restart/delay: %v/%v", state, when)
}
// Batch jobs with a non-zero exit code and 0 attempts exit with
// TaskNotRestarting
rt = newRestartTracker(p, structs.JobTypeBatch)
if state, when := rt.SetWaitResult(testWaitResult(1)).GetState(); state != structs.TaskNotRestarting {
t.Fatalf("expect no restart, got restart/delay: %v/%v", state, when)
} }
} }