open-nomad/api/tasks.go

113 lines
2.4 KiB
Go
Raw Normal View History

2015-09-09 20:02:39 +00:00
package api
import (
"time"
)
// RestartPolicy defines how the Nomad client restarts
// tasks in a taskgroup when they fail
type RestartPolicy struct {
Interval time.Duration
Attempts int
Delay time.Duration
}
2015-10-31 04:28:56 +00:00
func NewRestartPolicy() *RestartPolicy {
return &RestartPolicy{
Attempts: 10,
Interval: 3 * time.Minute,
Delay: 5 * time.Second,
}
}
2015-09-09 20:02:39 +00:00
// TaskGroup is the unit of scheduling.
type TaskGroup struct {
Name string
Count int
Constraints []*Constraint
Tasks []*Task
RestartPolicy *RestartPolicy
Meta map[string]string
2015-09-09 20:02:39 +00:00
}
// NewTaskGroup creates a new TaskGroup.
2015-09-10 00:59:18 +00:00
func NewTaskGroup(name string, count int) *TaskGroup {
2015-10-31 04:28:56 +00:00
restartPolicy := NewRestartPolicy()
2015-09-09 20:02:39 +00:00
return &TaskGroup{
2015-10-31 04:28:56 +00:00
Name: name,
Count: count,
RestartPolicy: restartPolicy,
2015-09-09 20:02:39 +00:00
}
}
// Constrain is used to add a constraint to a task group.
func (g *TaskGroup) Constrain(c *Constraint) *TaskGroup {
g.Constraints = append(g.Constraints, c)
return g
}
// AddMeta is used to add a meta k/v pair to a task group
func (g *TaskGroup) SetMeta(key, val string) *TaskGroup {
if g.Meta == nil {
g.Meta = make(map[string]string)
}
g.Meta[key] = val
return g
}
// AddTask is used to add a new task to a task group.
func (g *TaskGroup) AddTask(t *Task) *TaskGroup {
g.Tasks = append(g.Tasks, t)
return g
}
// Task is a single process in a task group.
type Task struct {
Name string
Driver string
Config map[string]string
Constraints []*Constraint
Env map[string]string
2015-09-09 20:02:39 +00:00
Resources *Resources
Meta map[string]string
}
// NewTask creates and initializes a new Task.
func NewTask(name, driver string) *Task {
return &Task{
Name: name,
Driver: driver,
}
}
// Configure is used to configure a single k/v pair on
// the task.
func (t *Task) SetConfig(key, val string) *Task {
if t.Config == nil {
t.Config = make(map[string]string)
}
t.Config[key] = val
return t
}
// SetMeta is used to add metadata k/v pairs to the task.
func (t *Task) SetMeta(key, val string) *Task {
if t.Meta == nil {
t.Meta = make(map[string]string)
}
t.Meta[key] = val
return t
}
// Require is used to add resource requirements to a task.
func (t *Task) Require(r *Resources) *Task {
t.Resources = r
return t
}
2015-09-10 00:29:43 +00:00
// Constraint adds a new constraints to a single task.
func (t *Task) Constrain(c *Constraint) *Task {
t.Constraints = append(t.Constraints, c)
return t
}