github.com/mattyr/nomad@v0.3.3-0.20160919021406-3485a065154a/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  	SizeMB int `mapstructure:"size"`
    89  }
    90  
    91  // TaskGroup is the unit of scheduling.
    92  type TaskGroup struct {
    93  	Name          string
    94  	Count         int
    95  	Constraints   []*Constraint
    96  	Tasks         []*Task
    97  	RestartPolicy *RestartPolicy
    98  	EphemeralDisk *EphemeralDisk
    99  	Meta          map[string]string
   100  }
   101  
   102  // NewTaskGroup creates a new TaskGroup.
   103  func NewTaskGroup(name string, count int) *TaskGroup {
   104  	return &TaskGroup{
   105  		Name:  name,
   106  		Count: count,
   107  	}
   108  }
   109  
   110  // Constrain is used to add a constraint to a task group.
   111  func (g *TaskGroup) Constrain(c *Constraint) *TaskGroup {
   112  	g.Constraints = append(g.Constraints, c)
   113  	return g
   114  }
   115  
   116  // AddMeta is used to add a meta k/v pair to a task group
   117  func (g *TaskGroup) SetMeta(key, val string) *TaskGroup {
   118  	if g.Meta == nil {
   119  		g.Meta = make(map[string]string)
   120  	}
   121  	g.Meta[key] = val
   122  	return g
   123  }
   124  
   125  // AddTask is used to add a new task to a task group.
   126  func (g *TaskGroup) AddTask(t *Task) *TaskGroup {
   127  	g.Tasks = append(g.Tasks, t)
   128  	return g
   129  }
   130  
   131  // RequireDisk adds a ephemeral disk to the task group
   132  func (g *TaskGroup) RequireDisk(disk *EphemeralDisk) *TaskGroup {
   133  	g.EphemeralDisk = disk
   134  	return g
   135  }
   136  
   137  // LogConfig provides configuration for log rotation
   138  type LogConfig struct {
   139  	MaxFiles         int
   140  	MaxFileSizeMB    int
   141  	LogShuttleConfig *LogShuttleConfig
   142  }
   143  
   144  // LogShuttleConfig configures log-shuttle log delivery
   145  type LogShuttleConfig struct {
   146  	UseGzip       bool
   147  	Drop          bool
   148  	LogToSyslog   bool
   149  	Prival        string
   150  	Version       string
   151  	Procid        string
   152  	Appname       string
   153  	LogplexToken  string
   154  	Hostname      string
   155  	Msgid         string
   156  	LogsURL       string
   157  	StatsSource   string
   158  	StatsInterval time.Duration
   159  	WaitDuration  time.Duration
   160  	Timeout       time.Duration
   161  	MaxAttempts   int
   162  	NumOutlets    int
   163  	BatchSize     int
   164  	BackBuff      int
   165  	MaxLineLength int
   166  	KinesisShards int
   167  }
   168  
   169  // Task is a single process in a task group.
   170  type Task struct {
   171  	Name            string
   172  	Driver          string
   173  	User            string
   174  	Config          map[string]interface{}
   175  	Constraints     []*Constraint
   176  	Env             map[string]string
   177  	ExcludeNomadEnv bool
   178  	Services        []Service
   179  	Resources       *Resources
   180  	Meta            map[string]string
   181  	KillTimeout     time.Duration
   182  	LogConfig       *LogConfig
   183  	Artifacts       []*TaskArtifact
   184  	Vault           *Vault
   185  }
   186  
   187  // TaskArtifact is used to download artifacts before running a task.
   188  type TaskArtifact struct {
   189  	GetterSource  string
   190  	GetterOptions map[string]string
   191  	RelativeDest  string
   192  }
   193  
   194  type Vault struct {
   195  	Policies []string
   196  }
   197  
   198  // NewTask creates and initializes a new Task.
   199  func NewTask(name, driver string) *Task {
   200  	return &Task{
   201  		Name:   name,
   202  		Driver: driver,
   203  	}
   204  }
   205  
   206  // Configure is used to configure a single k/v pair on
   207  // the task.
   208  func (t *Task) SetConfig(key string, val interface{}) *Task {
   209  	if t.Config == nil {
   210  		t.Config = make(map[string]interface{})
   211  	}
   212  	t.Config[key] = val
   213  	return t
   214  }
   215  
   216  // SetMeta is used to add metadata k/v pairs to the task.
   217  func (t *Task) SetMeta(key, val string) *Task {
   218  	if t.Meta == nil {
   219  		t.Meta = make(map[string]string)
   220  	}
   221  	t.Meta[key] = val
   222  	return t
   223  }
   224  
   225  // Require is used to add resource requirements to a task.
   226  func (t *Task) Require(r *Resources) *Task {
   227  	t.Resources = r
   228  	return t
   229  }
   230  
   231  // Constraint adds a new constraints to a single task.
   232  func (t *Task) Constrain(c *Constraint) *Task {
   233  	t.Constraints = append(t.Constraints, c)
   234  	return t
   235  }
   236  
   237  // SetLogConfig sets a log config to a task
   238  func (t *Task) SetLogConfig(l *LogConfig) *Task {
   239  	t.LogConfig = l
   240  	return t
   241  }
   242  
   243  // TaskState tracks the current state of a task and events that caused state
   244  // transitions.
   245  type TaskState struct {
   246  	State  string
   247  	Events []*TaskEvent
   248  }
   249  
   250  const (
   251  	TaskDriverFailure          = "Driver Failure"
   252  	TaskReceived               = "Received"
   253  	TaskFailedValidation       = "Failed Validation"
   254  	TaskStarted                = "Started"
   255  	TaskTerminated             = "Terminated"
   256  	TaskKilling                = "Killing"
   257  	TaskKilled                 = "Killed"
   258  	TaskRestarting             = "Restarting"
   259  	TaskNotRestarting          = "Not Restarting"
   260  	TaskDownloadingArtifacts   = "Downloading Artifacts"
   261  	TaskArtifactDownloadFailed = "Failed Artifact Download"
   262  	TaskDiskExceeded           = "Disk Exceeded"
   263  )
   264  
   265  // TaskEvent is an event that effects the state of a task and contains meta-data
   266  // appropriate to the events type.
   267  type TaskEvent struct {
   268  	Type            string
   269  	Time            int64
   270  	RestartReason   string
   271  	DriverError     string
   272  	ExitCode        int
   273  	Signal          int
   274  	Message         string
   275  	KillTimeout     time.Duration
   276  	KillError       string
   277  	StartDelay      int64
   278  	DownloadError   string
   279  	ValidationError string
   280  }