github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/api/jobs.go (about)

     1  package api
     2  
     3  import (
     4  	"sort"
     5  	"time"
     6  )
     7  
     8  const (
     9  	// JobTypeService indicates a long-running processes
    10  	JobTypeService = "service"
    11  
    12  	// JobTypeBatch indicates a short-lived process
    13  	JobTypeBatch = "batch"
    14  )
    15  
    16  // Jobs is used to access the job-specific endpoints.
    17  type Jobs struct {
    18  	client *Client
    19  }
    20  
    21  // Jobs returns a handle on the jobs endpoints.
    22  func (c *Client) Jobs() *Jobs {
    23  	return &Jobs{client: c}
    24  }
    25  
    26  // Register is used to register a new job. It returns the ID
    27  // of the evaluation, along with any errors encountered.
    28  func (j *Jobs) Register(job *Job, q *WriteOptions) (string, *WriteMeta, error) {
    29  	var resp registerJobResponse
    30  
    31  	req := &registerJobRequest{job}
    32  	wm, err := j.client.write("/v1/jobs", req, &resp, q)
    33  	if err != nil {
    34  		return "", nil, err
    35  	}
    36  	return resp.EvalID, wm, nil
    37  }
    38  
    39  // List is used to list all of the existing jobs.
    40  func (j *Jobs) List(q *QueryOptions) ([]*JobListStub, *QueryMeta, error) {
    41  	var resp []*JobListStub
    42  	qm, err := j.client.query("/v1/jobs", &resp, q)
    43  	if err != nil {
    44  		return nil, qm, err
    45  	}
    46  	sort.Sort(JobIDSort(resp))
    47  	return resp, qm, nil
    48  }
    49  
    50  // Info is used to retrieve information about a particular
    51  // job given its unique ID.
    52  func (j *Jobs) Info(jobID string, q *QueryOptions) (*Job, *QueryMeta, error) {
    53  	var resp Job
    54  	qm, err := j.client.query("/v1/job/"+jobID, &resp, q)
    55  	if err != nil {
    56  		return nil, nil, err
    57  	}
    58  	return &resp, qm, nil
    59  }
    60  
    61  // Allocations is used to return the allocs for a given job ID.
    62  func (j *Jobs) Allocations(jobID string, q *QueryOptions) ([]*AllocationListStub, *QueryMeta, error) {
    63  	var resp []*AllocationListStub
    64  	qm, err := j.client.query("/v1/job/"+jobID+"/allocations", &resp, q)
    65  	if err != nil {
    66  		return nil, nil, err
    67  	}
    68  	sort.Sort(AllocIndexSort(resp))
    69  	return resp, qm, nil
    70  }
    71  
    72  // Evaluations is used to query the evaluations associated with
    73  // the given job ID.
    74  func (j *Jobs) Evaluations(jobID string, q *QueryOptions) ([]*Evaluation, *QueryMeta, error) {
    75  	var resp []*Evaluation
    76  	qm, err := j.client.query("/v1/job/"+jobID+"/evaluations", &resp, q)
    77  	if err != nil {
    78  		return nil, nil, err
    79  	}
    80  	sort.Sort(EvalIndexSort(resp))
    81  	return resp, qm, nil
    82  }
    83  
    84  // Deregister is used to remove an existing job.
    85  func (j *Jobs) Deregister(jobID string, q *WriteOptions) (string, *WriteMeta, error) {
    86  	var resp deregisterJobResponse
    87  	wm, err := j.client.delete("/v1/job/"+jobID, &resp, q)
    88  	if err != nil {
    89  		return "", nil, err
    90  	}
    91  	return resp.EvalID, wm, nil
    92  }
    93  
    94  // ForceEvaluate is used to force-evaluate an existing job.
    95  func (j *Jobs) ForceEvaluate(jobID string, q *WriteOptions) (string, *WriteMeta, error) {
    96  	var resp registerJobResponse
    97  	wm, err := j.client.write("/v1/job/"+jobID+"/evaluate", nil, &resp, q)
    98  	if err != nil {
    99  		return "", nil, err
   100  	}
   101  	return resp.EvalID, wm, nil
   102  }
   103  
   104  //UpdateStrategy is for serializing update strategy for a job.
   105  type UpdateStrategy struct {
   106  	Stagger     time.Duration
   107  	MaxParallel int
   108  }
   109  
   110  // Job is used to serialize a job.
   111  type Job struct {
   112  	Region            string
   113  	ID                string
   114  	Name              string
   115  	Type              string
   116  	Priority          int
   117  	AllAtOnce         bool
   118  	Datacenters       []string
   119  	Constraints       []*Constraint
   120  	TaskGroups        []*TaskGroup
   121  	Update            *UpdateStrategy
   122  	Meta              map[string]string
   123  	Status            string
   124  	StatusDescription string
   125  	CreateIndex       uint64
   126  	ModifyIndex       uint64
   127  }
   128  
   129  // JobListStub is used to return a subset of information about
   130  // jobs during list operations.
   131  type JobListStub struct {
   132  	ID                string
   133  	Name              string
   134  	Type              string
   135  	Priority          int
   136  	Status            string
   137  	StatusDescription string
   138  	CreateIndex       uint64
   139  	ModifyIndex       uint64
   140  }
   141  
   142  // JobIDSort is used to sort jobs by their job ID's.
   143  type JobIDSort []*JobListStub
   144  
   145  func (j JobIDSort) Len() int {
   146  	return len(j)
   147  }
   148  
   149  func (j JobIDSort) Less(a, b int) bool {
   150  	return j[a].ID < j[b].ID
   151  }
   152  
   153  func (j JobIDSort) Swap(a, b int) {
   154  	j[a], j[b] = j[b], j[a]
   155  }
   156  
   157  // NewServiceJob creates and returns a new service-style job
   158  // for long-lived processes using the provided name, ID, and
   159  // relative job priority.
   160  func NewServiceJob(id, name, region string, pri int) *Job {
   161  	return newJob(id, name, region, JobTypeService, pri)
   162  }
   163  
   164  // NewBatchJob creates and returns a new batch-style job for
   165  // short-lived processes using the provided name and ID along
   166  // with the relative job priority.
   167  func NewBatchJob(id, name, region string, pri int) *Job {
   168  	return newJob(id, name, region, JobTypeBatch, pri)
   169  }
   170  
   171  // newJob is used to create a new Job struct.
   172  func newJob(id, name, region, typ string, pri int) *Job {
   173  	return &Job{
   174  		Region:   region,
   175  		ID:       id,
   176  		Name:     name,
   177  		Type:     typ,
   178  		Priority: pri,
   179  	}
   180  }
   181  
   182  // SetMeta is used to set arbitrary k/v pairs of metadata on a job.
   183  func (j *Job) SetMeta(key, val string) *Job {
   184  	if j.Meta == nil {
   185  		j.Meta = make(map[string]string)
   186  	}
   187  	j.Meta[key] = val
   188  	return j
   189  }
   190  
   191  // AddDatacenter is used to add a datacenter to a job.
   192  func (j *Job) AddDatacenter(dc string) *Job {
   193  	j.Datacenters = append(j.Datacenters, dc)
   194  	return j
   195  }
   196  
   197  // Constrain is used to add a constraint to a job.
   198  func (j *Job) Constrain(c *Constraint) *Job {
   199  	j.Constraints = append(j.Constraints, c)
   200  	return j
   201  }
   202  
   203  // AddTaskGroup adds a task group to an existing job.
   204  func (j *Job) AddTaskGroup(grp *TaskGroup) *Job {
   205  	j.TaskGroups = append(j.TaskGroups, grp)
   206  	return j
   207  }
   208  
   209  // registerJobRequest is used to serialize a job registration
   210  type registerJobRequest struct {
   211  	Job *Job
   212  }
   213  
   214  // registerJobResponse is used to deserialize a job response
   215  type registerJobResponse struct {
   216  	EvalID string
   217  }
   218  
   219  // deregisterJobResponse is used to decode a deregister response
   220  type deregisterJobResponse struct {
   221  	EvalID string
   222  }