Skip to content

Commit

Permalink
Merge pull request #1457 from hashicorp/f-kill-event
Browse files Browse the repository at this point in the history
Add killing event and mark task as not running when killed
  • Loading branch information
dadgar committed Jul 23, 2016
2 parents 02a910e + 7bb0a23 commit 590e5e9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions api/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ const (
TaskFailedValidation = "Failed Validation"
TaskStarted = "Started"
TaskTerminated = "Terminated"
TaskKilling = "Killing"
TaskKilled = "Killed"
TaskRestarting = "Restarting"
TaskNotRestarting = "Not Restarting"
Expand All @@ -224,6 +225,7 @@ type TaskEvent struct {
ExitCode int
Signal int
Message string
KillTimeout time.Duration
KillError string
StartDelay int64
DownloadError string
Expand Down
10 changes: 10 additions & 0 deletions client/task_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,11 @@ func (r *TaskRunner) run() {
r.logger.Printf("[ERR] client: update to task %q failed: %v", r.task.Name, err)
}
case <-r.destroyCh:
// Mark that we received the kill event
timeout := driver.GetKillTimeout(r.task.KillTimeout, r.config.MaxKillTimeout)
r.setState(structs.TaskStateRunning,
structs.NewTaskEvent(structs.TaskKilling).SetKillTimeout(timeout))

// Kill the task using an exponential backoff in-case of failures.
destroySuccess, err := r.handleDestroy()
if !destroySuccess {
Expand All @@ -404,6 +409,11 @@ func (r *TaskRunner) run() {

// Store that the task has been destroyed and any associated error.
r.setState(structs.TaskStateDead, structs.NewTaskEvent(structs.TaskKilled).SetKillError(err))

r.runningLock.Lock()
r.running = false
r.runningLock.Unlock()

return
}
}
Expand Down
6 changes: 6 additions & 0 deletions command/alloc_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ func (c *AllocStatusCommand) outputTaskStatus(state *api.TaskState) {
} else {
desc = "Failed to download artifacts"
}
case api.TaskKilling:
if event.KillTimeout != 0 {
desc = fmt.Sprintf("Sent interupt. Waiting %v before force killing", event.KillTimeout)
} else {
desc = "Sent interupt"
}
case api.TaskKilled:
if event.KillError != "" {
desc = event.KillError
Expand Down
11 changes: 11 additions & 0 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2100,6 +2100,9 @@ const (
// TaskTerminated indicates that the task was started and exited.
TaskTerminated = "Terminated"

// TaskKilling indicates a kill signal has been sent to the task.
TaskKilling = "Killing"

// TaskKilled indicates a user has killed the task.
TaskKilled = "Killed"

Expand Down Expand Up @@ -2136,6 +2139,9 @@ type TaskEvent struct {
Signal int // The signal that terminated the task.
Message string // A possible message explaining the termination of the task.

// Killing fields
KillTimeout time.Duration

// Task Killed Fields.
KillError string // Error killing the task.

Expand Down Expand Up @@ -2224,6 +2230,11 @@ func (e *TaskEvent) SetValidationError(err error) *TaskEvent {
return e
}

func (e *TaskEvent) SetKillTimeout(timeout time.Duration) *TaskEvent {
e.KillTimeout = timeout
return e
}

// TaskArtifact is an artifact to download before running the task.
type TaskArtifact struct {
// GetterSource is the source to download an artifact using go-getter
Expand Down
1 change: 1 addition & 0 deletions website/source/docs/http/alloc.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ be specified using the `?region=` query parameter.
* `Started` - The task was started; either for the first time or due to a
restart.
* `Terminated` - The task was started and exited.
* `Killing` - The task has been sent the kill signal.
* `Killed` - The task was killed by an user.
* `Received` - The task has been pulled by the client at the given timestamp.
* `Failed Validation` - The task was invalid and as such it didn't run.
Expand Down

0 comments on commit 590e5e9

Please sign in to comment.