github.com/dkerwin/nomad@v0.3.3-0.20160525181927-74554135514b/api/tasks.go (about) 1 package api 2 3 import ( 4 "time" 5 ) 6 7 // RestartPolicy defines how the Nomad client restarts 8 // tasks in a taskgroup when they fail 9 type RestartPolicy struct { 10 Interval time.Duration 11 Attempts int 12 Delay time.Duration 13 Mode string 14 } 15 16 // The ServiceCheck data model represents the consul health check that 17 // Nomad registers for a Task 18 type ServiceCheck struct { 19 Id string 20 Name string 21 Type string 22 Command string 23 Args []string 24 Path string 25 Protocol string 26 Interval time.Duration 27 Timeout time.Duration 28 } 29 30 // The Service model represents a Consul service definition 31 type Service struct { 32 Id string 33 Name string 34 Tags []string 35 PortLabel string `mapstructure:"port"` 36 Checks []ServiceCheck 37 } 38 39 // TaskGroup is the unit of scheduling. 40 type TaskGroup struct { 41 Name string 42 Count int 43 Constraints []*Constraint 44 Tasks []*Task 45 RestartPolicy *RestartPolicy 46 Meta map[string]string 47 } 48 49 // NewTaskGroup creates a new TaskGroup. 50 func NewTaskGroup(name string, count int) *TaskGroup { 51 return &TaskGroup{ 52 Name: name, 53 Count: count, 54 } 55 } 56 57 // Constrain is used to add a constraint to a task group. 58 func (g *TaskGroup) Constrain(c *Constraint) *TaskGroup { 59 g.Constraints = append(g.Constraints, c) 60 return g 61 } 62 63 // AddMeta is used to add a meta k/v pair to a task group 64 func (g *TaskGroup) SetMeta(key, val string) *TaskGroup { 65 if g.Meta == nil { 66 g.Meta = make(map[string]string) 67 } 68 g.Meta[key] = val 69 return g 70 } 71 72 // AddTask is used to add a new task to a task group. 73 func (g *TaskGroup) AddTask(t *Task) *TaskGroup { 74 g.Tasks = append(g.Tasks, t) 75 return g 76 } 77 78 // LogConfig provides configuration for log rotation 79 type LogConfig struct { 80 MaxFiles int 81 MaxFileSizeMB int 82 } 83 84 // Task is a single process in a task group. 85 type Task struct { 86 Name string 87 Driver string 88 User string 89 Config map[string]interface{} 90 Constraints []*Constraint 91 Env map[string]string 92 Services []Service 93 Resources *Resources 94 Meta map[string]string 95 KillTimeout time.Duration 96 LogConfig *LogConfig 97 Artifacts []*TaskArtifact 98 } 99 100 // TaskArtifact is used to download artifacts before running a task. 101 type TaskArtifact struct { 102 GetterSource string 103 GetterOptions map[string]string 104 RelativeDest string 105 } 106 107 // NewTask creates and initializes a new Task. 108 func NewTask(name, driver string) *Task { 109 return &Task{ 110 Name: name, 111 Driver: driver, 112 } 113 } 114 115 // Configure is used to configure a single k/v pair on 116 // the task. 117 func (t *Task) SetConfig(key, val string) *Task { 118 if t.Config == nil { 119 t.Config = make(map[string]interface{}) 120 } 121 t.Config[key] = val 122 return t 123 } 124 125 // SetMeta is used to add metadata k/v pairs to the task. 126 func (t *Task) SetMeta(key, val string) *Task { 127 if t.Meta == nil { 128 t.Meta = make(map[string]string) 129 } 130 t.Meta[key] = val 131 return t 132 } 133 134 // Require is used to add resource requirements to a task. 135 func (t *Task) Require(r *Resources) *Task { 136 t.Resources = r 137 return t 138 } 139 140 // Constraint adds a new constraints to a single task. 141 func (t *Task) Constrain(c *Constraint) *Task { 142 t.Constraints = append(t.Constraints, c) 143 return t 144 } 145 146 // SetLogConfig sets a log config to a task 147 func (t *Task) SetLogConfig(l *LogConfig) *Task { 148 t.LogConfig = l 149 return t 150 } 151 152 // TaskState tracks the current state of a task and events that caused state 153 // transitions. 154 type TaskState struct { 155 State string 156 Events []*TaskEvent 157 } 158 159 const ( 160 TaskDriverFailure = "Driver Failure" 161 TaskReceived = "Received" 162 TaskFailedValidation = "Failed Validation" 163 TaskStarted = "Started" 164 TaskTerminated = "Terminated" 165 TaskKilled = "Killed" 166 TaskRestarting = "Restarting" 167 TaskNotRestarting = "Not Restarting" 168 TaskDownloadingArtifacts = "Downloading Artifacts" 169 TaskArtifactDownloadFailed = "Failed Artifact Download" 170 ) 171 172 // TaskEvent is an event that effects the state of a task and contains meta-data 173 // appropriate to the events type. 174 type TaskEvent struct { 175 Type string 176 Time int64 177 RestartReason string 178 DriverError string 179 ExitCode int 180 Signal int 181 Message string 182 KillError string 183 StartDelay int64 184 DownloadError string 185 ValidationError string 186 }