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