github.com/maier/nomad@v0.4.1-0.20161110003312-a9e3d0b8549d/api/tasks.go (about) 1 package api 2 3 import ( 4 "time" 5 ) 6 7 // MemoryStats holds memory usage related stats 8 type MemoryStats struct { 9 RSS uint64 10 Cache uint64 11 Swap uint64 12 MaxUsage uint64 13 KernelUsage uint64 14 KernelMaxUsage uint64 15 Measured []string 16 } 17 18 // CpuStats holds cpu usage related stats 19 type CpuStats struct { 20 SystemMode float64 21 UserMode float64 22 TotalTicks float64 23 ThrottledPeriods uint64 24 ThrottledTime uint64 25 Percent float64 26 Measured []string 27 } 28 29 // ResourceUsage holds information related to cpu and memory stats 30 type ResourceUsage struct { 31 MemoryStats *MemoryStats 32 CpuStats *CpuStats 33 } 34 35 // TaskResourceUsage holds aggregated resource usage of all processes in a Task 36 // and the resource usage of the individual pids 37 type TaskResourceUsage struct { 38 ResourceUsage *ResourceUsage 39 Timestamp int64 40 Pids map[string]*ResourceUsage 41 } 42 43 // AllocResourceUsage holds the aggregated task resource usage of the 44 // allocation. 45 type AllocResourceUsage struct { 46 ResourceUsage *ResourceUsage 47 Tasks map[string]*TaskResourceUsage 48 Timestamp int64 49 } 50 51 // RestartPolicy defines how the Nomad client restarts 52 // tasks in a taskgroup when they fail 53 type RestartPolicy struct { 54 Interval time.Duration 55 Attempts int 56 Delay time.Duration 57 Mode string 58 } 59 60 // The ServiceCheck data model represents the consul health check that 61 // Nomad registers for a Task 62 type ServiceCheck struct { 63 Id string 64 Name string 65 Type string 66 Command string 67 Args []string 68 Path string 69 Protocol string `mapstructure:"port"` 70 PortLabel string `mapstructure:"port"` 71 Interval time.Duration 72 Timeout time.Duration 73 InitialStatus string `mapstructure:"initial_status"` 74 } 75 76 // The Service model represents a Consul service definition 77 type Service struct { 78 Id string 79 Name string 80 Tags []string 81 PortLabel string `mapstructure:"port"` 82 Checks []ServiceCheck 83 } 84 85 // EphemeralDisk is an ephemeral disk object 86 type EphemeralDisk struct { 87 Sticky bool 88 Migrate bool 89 SizeMB int `mapstructure:"size"` 90 } 91 92 // TaskGroup is the unit of scheduling. 93 type TaskGroup struct { 94 Name string 95 Count int 96 Constraints []*Constraint 97 Tasks []*Task 98 RestartPolicy *RestartPolicy 99 EphemeralDisk *EphemeralDisk 100 Meta map[string]string 101 } 102 103 // NewTaskGroup creates a new TaskGroup. 104 func NewTaskGroup(name string, count int) *TaskGroup { 105 return &TaskGroup{ 106 Name: name, 107 Count: count, 108 } 109 } 110 111 // Constrain is used to add a constraint to a task group. 112 func (g *TaskGroup) Constrain(c *Constraint) *TaskGroup { 113 g.Constraints = append(g.Constraints, c) 114 return g 115 } 116 117 // AddMeta is used to add a meta k/v pair to a task group 118 func (g *TaskGroup) SetMeta(key, val string) *TaskGroup { 119 if g.Meta == nil { 120 g.Meta = make(map[string]string) 121 } 122 g.Meta[key] = val 123 return g 124 } 125 126 // AddTask is used to add a new task to a task group. 127 func (g *TaskGroup) AddTask(t *Task) *TaskGroup { 128 g.Tasks = append(g.Tasks, t) 129 return g 130 } 131 132 // RequireDisk adds a ephemeral disk to the task group 133 func (g *TaskGroup) RequireDisk(disk *EphemeralDisk) *TaskGroup { 134 g.EphemeralDisk = disk 135 return g 136 } 137 138 // LogConfig provides configuration for log rotation 139 type LogConfig struct { 140 MaxFiles int 141 MaxFileSizeMB int 142 } 143 144 // Task is a single process in a task group. 145 type Task struct { 146 Name string 147 Driver string 148 User string 149 Config map[string]interface{} 150 Constraints []*Constraint 151 Env map[string]string 152 Services []Service 153 Resources *Resources 154 Meta map[string]string 155 KillTimeout time.Duration 156 LogConfig *LogConfig 157 Artifacts []*TaskArtifact 158 Vault *Vault 159 Templates []*Template 160 } 161 162 // TaskArtifact is used to download artifacts before running a task. 163 type TaskArtifact struct { 164 GetterSource string 165 GetterOptions map[string]string 166 RelativeDest string 167 } 168 169 type Template struct { 170 SourcePath string 171 DestPath string 172 EmbeddedTmpl string 173 ChangeMode string 174 ChangeSignal string 175 Splay time.Duration 176 } 177 178 type Vault struct { 179 Policies []string 180 Env bool 181 ChangeMode string 182 ChangeSignal string 183 } 184 185 // NewTask creates and initializes a new Task. 186 func NewTask(name, driver string) *Task { 187 return &Task{ 188 Name: name, 189 Driver: driver, 190 } 191 } 192 193 // Configure is used to configure a single k/v pair on 194 // the task. 195 func (t *Task) SetConfig(key string, val interface{}) *Task { 196 if t.Config == nil { 197 t.Config = make(map[string]interface{}) 198 } 199 t.Config[key] = val 200 return t 201 } 202 203 // SetMeta is used to add metadata k/v pairs to the task. 204 func (t *Task) SetMeta(key, val string) *Task { 205 if t.Meta == nil { 206 t.Meta = make(map[string]string) 207 } 208 t.Meta[key] = val 209 return t 210 } 211 212 // Require is used to add resource requirements to a task. 213 func (t *Task) Require(r *Resources) *Task { 214 t.Resources = r 215 return t 216 } 217 218 // Constraint adds a new constraints to a single task. 219 func (t *Task) Constrain(c *Constraint) *Task { 220 t.Constraints = append(t.Constraints, c) 221 return t 222 } 223 224 // SetLogConfig sets a log config to a task 225 func (t *Task) SetLogConfig(l *LogConfig) *Task { 226 t.LogConfig = l 227 return t 228 } 229 230 // TaskState tracks the current state of a task and events that caused state 231 // transitions. 232 type TaskState struct { 233 State string 234 Failed bool 235 Events []*TaskEvent 236 } 237 238 const ( 239 TaskSetupFailure = "Setup Failure" 240 TaskDriverFailure = "Driver Failure" 241 TaskReceived = "Received" 242 TaskFailedValidation = "Failed Validation" 243 TaskStarted = "Started" 244 TaskTerminated = "Terminated" 245 TaskKilling = "Killing" 246 TaskKilled = "Killed" 247 TaskRestarting = "Restarting" 248 TaskNotRestarting = "Not Restarting" 249 TaskDownloadingArtifacts = "Downloading Artifacts" 250 TaskArtifactDownloadFailed = "Failed Artifact Download" 251 TaskVaultRenewalFailed = "Vault token renewal failed" 252 TaskSiblingFailed = "Sibling task failed" 253 TaskSignaling = "Signaling" 254 TaskRestartSignal = "Restart Signaled" 255 ) 256 257 // TaskEvent is an event that effects the state of a task and contains meta-data 258 // appropriate to the events type. 259 type TaskEvent struct { 260 Type string 261 Time int64 262 FailsTask bool 263 RestartReason string 264 SetupError string 265 DriverError string 266 ExitCode int 267 Signal int 268 Message string 269 KillReason string 270 KillTimeout time.Duration 271 KillError string 272 StartDelay int64 273 DownloadError string 274 ValidationError string 275 DiskLimit int64 276 DiskSize int64 277 FailedSibling string 278 VaultError string 279 TaskSignalReason string 280 TaskSignal string 281 }