Skip to content
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

Not restarting if a task exited properly #491

Merged
merged 5 commits into from
Nov 23, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions client/restarts.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// For Batch jobs, the interval is set to zero value since the takss
// will be restarted only upto maxAttempts times
type restartTracker interface {
nextRestart() (bool, time.Duration)
nextRestart(exitCode int) (bool, time.Duration)
}

func newRestartTracker(jobType string, restartPolicy *structs.RestartPolicy) restartTracker {
Expand Down Expand Up @@ -47,8 +47,8 @@ func (b *batchRestartTracker) increment() {
b.count += 1
}

func (b *batchRestartTracker) nextRestart() (bool, time.Duration) {
if b.count < b.maxAttempts {
func (b *batchRestartTracker) nextRestart(exitCode int) (bool, time.Duration) {
if (b.count < b.maxAttempts) && (exitCode > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get rid of the parenthesis. Not very idiomatic go.

b.increment()
return true, b.delay
}
Expand All @@ -68,7 +68,7 @@ func (s *serviceRestartTracker) increment() {
s.count += 1
}

func (s *serviceRestartTracker) nextRestart() (bool, time.Duration) {
func (s *serviceRestartTracker) nextRestart(exitCode int) (bool, time.Duration) {
defer s.increment()
windowEndTime := s.startTime.Add(s.interval)
now := time.Now()
Expand Down
26 changes: 22 additions & 4 deletions client/restarts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestTaskRunner_ServiceRestartCounter(t *testing.T) {
rt := newRestartTracker(structs.JobTypeService, &structs.RestartPolicy{Attempts: attempts, Interval: interval, Delay: delay})

for i := 0; i < attempts; i++ {
actual, when := rt.nextRestart()
actual, when := rt.nextRestart(127)
if !actual {
t.Fatalf("should restart returned %v, actual %v", actual, true)
}
Expand All @@ -24,7 +24,7 @@ func TestTaskRunner_ServiceRestartCounter(t *testing.T) {

time.Sleep(1 * time.Second)
for i := 0; i < 3; i++ {
actual, when := rt.nextRestart()
actual, when := rt.nextRestart(127)
if !actual {
t.Fail()
}
Expand All @@ -46,16 +46,34 @@ func TestTaskRunner_BatchRestartCounter(t *testing.T) {
},
)
for i := 0; i < attempts; i++ {
shouldRestart, when := rt.nextRestart()
shouldRestart, when := rt.nextRestart(127)
if !shouldRestart {
t.Fatalf("should restart returned %v, actual %v", shouldRestart, true)
}
if when != delay {
t.Fatalf("Delay should be %v, actual: %v", delay, when)
}
}
actual, _ := rt.nextRestart()
actual, _ := rt.nextRestart(1)
if actual {
t.Fatalf("Expect %v, Actual: %v", false, actual)
}
}

func TestTaskRunner_BatchRestartOnSuccess(t *testing.T) {
attempts := 2
interval := 1 * time.Second
delay := 1 * time.Second
rt := newRestartTracker(structs.JobTypeBatch,
&structs.RestartPolicy{Attempts: attempts,
Interval: interval,
Delay: delay,
},
)
shouldRestart, _ := rt.nextRestart(0)
if shouldRestart {
t.Fatalf("should restart returned %v, expected: %v", shouldRestart, false)
t.Fail()
}

}
2 changes: 1 addition & 1 deletion client/task_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ func (r *TaskRunner) run() {
}

// Check if we should restart. If not mark task as dead and exit.
shouldRestart, when := r.restartTracker.nextRestart(waitRes.ExitCode)
waitEvent := r.waitErrorToEvent(waitRes)
shouldRestart, when := r.restartTracker.nextRestart()
if !shouldRestart {
r.logger.Printf("[INFO] client: Not restarting task: %v for alloc: %v ", r.task.Name, r.allocID)
r.setState(structs.TaskStateDead, waitEvent)
Expand Down