github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/api/tasks.go (about) 1 package api 2 3 // TaskGroup is the unit of scheduling. 4 type TaskGroup struct { 5 Name string 6 Count int 7 Constraints []*Constraint 8 Tasks []*Task 9 Meta map[string]string 10 } 11 12 // NewTaskGroup creates a new TaskGroup. 13 func NewTaskGroup(name string, count int) *TaskGroup { 14 return &TaskGroup{ 15 Name: name, 16 Count: count, 17 } 18 } 19 20 // Constrain is used to add a constraint to a task group. 21 func (g *TaskGroup) Constrain(c *Constraint) *TaskGroup { 22 g.Constraints = append(g.Constraints, c) 23 return g 24 } 25 26 // AddMeta is used to add a meta k/v pair to a task group 27 func (g *TaskGroup) SetMeta(key, val string) *TaskGroup { 28 if g.Meta == nil { 29 g.Meta = make(map[string]string) 30 } 31 g.Meta[key] = val 32 return g 33 } 34 35 // AddTask is used to add a new task to a task group. 36 func (g *TaskGroup) AddTask(t *Task) *TaskGroup { 37 g.Tasks = append(g.Tasks, t) 38 return g 39 } 40 41 // Task is a single process in a task group. 42 type Task struct { 43 Name string 44 Driver string 45 Config map[string]string 46 Constraints []*Constraint 47 Env map[string]string 48 Resources *Resources 49 Meta map[string]string 50 } 51 52 // NewTask creates and initializes a new Task. 53 func NewTask(name, driver string) *Task { 54 return &Task{ 55 Name: name, 56 Driver: driver, 57 } 58 } 59 60 // Configure is used to configure a single k/v pair on 61 // the task. 62 func (t *Task) SetConfig(key, val string) *Task { 63 if t.Config == nil { 64 t.Config = make(map[string]string) 65 } 66 t.Config[key] = val 67 return t 68 } 69 70 // SetMeta is used to add metadata k/v pairs to the task. 71 func (t *Task) SetMeta(key, val string) *Task { 72 if t.Meta == nil { 73 t.Meta = make(map[string]string) 74 } 75 t.Meta[key] = val 76 return t 77 } 78 79 // Require is used to add resource requirements to a task. 80 func (t *Task) Require(r *Resources) *Task { 81 t.Resources = r 82 return t 83 } 84 85 // Constraint adds a new constraints to a single task. 86 func (t *Task) Constrain(c *Constraint) *Task { 87 t.Constraints = append(t.Constraints, c) 88 return t 89 }