github.com/ryanslade/nomad@v0.2.4-0.20160128061903-fc95782f2089/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  	RestartOnSuccess bool
    14  	Mode             string
    15  }
    16  
    17  // The ServiceCheck data model represents the consul health check that
    18  // Nomad registers for a Task
    19  type ServiceCheck struct {
    20  	Id       string
    21  	Name     string
    22  	Type     string
    23  	Script   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 defintion
    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  // Task is a single process in a task group.
    79  type Task struct {
    80  	Name        string
    81  	Driver      string
    82  	Config      map[string]interface{}
    83  	Constraints []*Constraint
    84  	Env         map[string]string
    85  	Services    []Service
    86  	Resources   *Resources
    87  	Meta        map[string]string
    88  	KillTimeout time.Duration
    89  }
    90  
    91  // NewTask creates and initializes a new Task.
    92  func NewTask(name, driver string) *Task {
    93  	return &Task{
    94  		Name:   name,
    95  		Driver: driver,
    96  	}
    97  }
    98  
    99  // Configure is used to configure a single k/v pair on
   100  // the task.
   101  func (t *Task) SetConfig(key, val string) *Task {
   102  	if t.Config == nil {
   103  		t.Config = make(map[string]interface{})
   104  	}
   105  	t.Config[key] = val
   106  	return t
   107  }
   108  
   109  // SetMeta is used to add metadata k/v pairs to the task.
   110  func (t *Task) SetMeta(key, val string) *Task {
   111  	if t.Meta == nil {
   112  		t.Meta = make(map[string]string)
   113  	}
   114  	t.Meta[key] = val
   115  	return t
   116  }
   117  
   118  // Require is used to add resource requirements to a task.
   119  func (t *Task) Require(r *Resources) *Task {
   120  	t.Resources = r
   121  	return t
   122  }
   123  
   124  // Constraint adds a new constraints to a single task.
   125  func (t *Task) Constrain(c *Constraint) *Task {
   126  	t.Constraints = append(t.Constraints, c)
   127  	return t
   128  }
   129  
   130  // TaskState tracks the current state of a task and events that caused state
   131  // transistions.
   132  type TaskState struct {
   133  	State  string
   134  	Events []*TaskEvent
   135  }
   136  
   137  const (
   138  	TaskDriverFailure = "Driver Failure"
   139  	TaskStarted       = "Started"
   140  	TaskTerminated    = "Terminated"
   141  	TaskKilled        = "Killed"
   142  )
   143  
   144  // TaskEvent is an event that effects the state of a task and contains meta-data
   145  // appropriate to the events type.
   146  type TaskEvent struct {
   147  	Type        string
   148  	Time        int64
   149  	DriverError string
   150  	ExitCode    int
   151  	Signal      int
   152  	Message     string
   153  	KillError   string
   154  }