From 359251744f8761e5430d97e2e200f030966a43e5 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Thu, 12 Nov 2015 15:28:22 -0800 Subject: [PATCH] Add TaskState to alloc --- api/allocations.go | 2 ++ api/tasks.go | 28 ++++++++++++++++++++ nomad/structs/structs.go | 56 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) diff --git a/api/allocations.go b/api/allocations.go index 829c1030f..00ab6984d 100644 --- a/api/allocations.go +++ b/api/allocations.go @@ -52,6 +52,7 @@ type Allocation struct { DesiredDescription string ClientStatus string ClientDescription string + TaskStates map[string]*TaskState CreateIndex uint64 ModifyIndex uint64 } @@ -83,6 +84,7 @@ type AllocationListStub struct { DesiredDescription string ClientStatus string ClientDescription string + TaskStates map[string]*TaskState CreateIndex uint64 ModifyIndex uint64 } diff --git a/api/tasks.go b/api/tasks.go index 2535d5ec5..5c6dd600b 100644 --- a/api/tasks.go +++ b/api/tasks.go @@ -110,3 +110,31 @@ func (t *Task) Constrain(c *Constraint) *Task { t.Constraints = append(t.Constraints, c) return t } + +// TaskState tracks the current state of a task and events that caused state +// transistions. +type TaskState struct { + State string + Events []*TaskEvent +} + +// TaskEventType is the set of events that effect the state of a task. +type TaskEventType int + +const ( + TaskDriverFailure TaskEventType = iota + TaskStarted + TaskTerminated + TaskKilled +) + +// TaskEvent is an event that effects the state of a task and contains meta-data +// appropriate to the events type. +type TaskEvent struct { + Type TaskEventType + Time int64 + DriverError error + ExitCode int + Signal int + Message string +} diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index ce5007ac5..a3d535e72 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -1053,6 +1053,57 @@ func (t *Task) GoString() string { return fmt.Sprintf("*%#v", *t) } +// Set of possible states for a task. +const ( + TaskStatePending = "pending" // The task is waiting to be run. + TaskStateRunning = "running" // The task is currently running. + TaskStateDead = "dead" // Terminal state of task. +) + +// TaskState tracks the current state of a task and events that caused state +// transistions. +type TaskState struct { + // The current state of the task. + State string + + // Series of task events that transistion the state of the task. + Events []*TaskEvent +} + +// TaskEventType is the set of events that effect the state of a task. +type TaskEventType int + +const ( + // A Driver failure indicates that the task could not be started due to a + // failure in the driver. + TaskDriverFailure TaskEventType = iota + + // Task Started signals that the task was started and its timestamp can be + // used to determine the running length of the task. + TaskStarted + + // Task terminated indicates that the task was started and exited. + TaskTerminated + + // Task Killed indicates a user has killed the task. + TaskKilled +) + +// TaskEvent is an event that effects the state of a task and contains meta-data +// appropriate to the events type. +type TaskEvent struct { + Type TaskEventType + Time int64 // Unix Nanosecond timestamp + + // Driver Failure fields. + DriverError error // A driver error occured while starting the task. + + // Task Terminated Fields. + ExitCode int // The exit code of the task. + Signal int // The signal that terminated the task. + Message string // A possible message explaining the termination of the task. +} + // Validate is used to sanity check a task group func (t *Task) Validate() error { var mErr multierror.Error @@ -1171,6 +1222,9 @@ type Allocation struct { // ClientStatusDescription is meant to provide more human useful information ClientDescription string + // TaskStates stores the state of each task, + TaskStates map[string]*TaskState + // Raft Indexes CreateIndex uint64 ModifyIndex uint64 @@ -1200,6 +1254,7 @@ func (a *Allocation) Stub() *AllocListStub { DesiredDescription: a.DesiredDescription, ClientStatus: a.ClientStatus, ClientDescription: a.ClientDescription, + TaskStates: a.TaskStates, CreateIndex: a.CreateIndex, ModifyIndex: a.ModifyIndex, } @@ -1217,6 +1272,7 @@ type AllocListStub struct { DesiredDescription string ClientStatus string ClientDescription string + TaskStates map[string]*TaskState CreateIndex uint64 ModifyIndex uint64 }