/
jobs.go
208 lines (174 loc) · 5.72 KB
/
jobs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
Package jobs implements the basic Job structure and related functionality
*/
package jobs
import (
"time"
)
// Job Status Constants
const (
StatusFailed = "FAILED"
StatusPending = "PENDING"
StatusRunnable = "RUNNABLE"
StatusRunning = "RUNNING"
StatusStarting = "STARTING"
StatusSubmitted = "SUBMITTED"
StatusSucceeded = "SUCCEEDED"
)
// StatusList is a list of all possible job statuses
var StatusList = [...]string{
StatusFailed,
StatusPending,
StatusRunnable,
StatusRunning,
StatusStarting,
StatusSubmitted,
StatusSucceeded,
}
type JobStatus struct {
Id string `json:"id"`
Status string `json:"status"`
}
type Job struct {
Id string `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
Description string `json:"desc"`
LastUpdated time.Time `json:"last_updated"`
JobQueue string `json:"job_queue"`
Image string `json:"image"`
CreatedAt time.Time `json:"created_at"`
StoppedAt *time.Time `json:"stopped_at"`
VCpus int64 `json:"vcpus"`
Memory int64 `json:"memory"`
Timeout int `json:"timeout"`
CommandLine string `json:"command_line"`
StatusReason *string `json:"status_reason"`
RunStartTime *time.Time `json:"run_start_time"`
ExitCode *int64 `json:"exitcode"`
LogStreamName *string `json:"log_stream_name"`
TerminationRequested bool `json:"termination_requested"`
TaskARN *string `json:"task_arn"`
InstanceID *string `json:"instance_id"`
PublicIP *string `json:"public_ip"`
PrivateIP *string `json:"private_ip"`
}
// Options is the query options for the Find method to use
type Options struct {
Search string
Limit int
Offset int
Queues []string
SortBy string
SortAsc bool
Status []string
}
type JobStatsOptions struct {
Queues []string
Status []string
Interval int64
Start int64
End int64
}
type JobStats struct {
JobQueue string `json:"job_queue"`
Status string `json:"status"`
Timestamp float64 `json:"timestamp"`
VCPUSeconds float64 `json:"vcpu_seconds"`
MemorySeconds float64 `json:"memory_seconds"`
InstanceSeconds float64 `json:"instance_seconds"`
JobCount int `json:"job_count"`
Interval int64 `json:"interval"`
}
// KillTaskID is a struct to handle JSON request to kill a task
type KillTaskID struct {
ID string `json:"id" form:"id" query:"id"`
}
// FinderStorer is an interface that can both save and retrieve jobs
type FinderStorer interface {
Finder
Storer
// Methods to get information about Job Queues
ListActiveJobQueues() ([]string, error)
ListForcedScalingJobQueues() ([]string, error)
ActivateJobQueue(string) error
DeactivateJobQueue(string) error
}
// Finder is an interface to find jobs in a database/store
type Finder interface {
// Find finds a jobs matching the query
Find(opts *Options) ([]*Job, error)
// FindOne finds a job matching the query
FindOne(query string) (*Job, error)
// FindTimedoutJobs finds all job IDs that should have timed out by now
FindTimedoutJobs() ([]string, error)
// Simple endpoint that returns a string for job status.
GetStatus(jobid string) (*JobStatus, error)
JobStats(opts *JobStatsOptions) ([]*JobStats, error)
}
// Storer is an interface to save jobs in a database/store
type Storer interface {
// Store saves a job
Store(job []*Job) error
// Gives the store a chance to stale jobs we no longer know about
// The argument is a set (value is ignored) of all known job_ids currently by AWS Batch
StaleOldJobs(map[string]bool) error
// Finds estimated load per job queue
EstimateRunningLoadByJobQueue([]string) (map[string]RunningLoad, error)
// Update compute environment logs
UpdateComputeEnvironmentsLog([]ComputeEnvironment) error
// Update job summaries
UpdateJobSummaryLog([]JobSummary) error
// Mark on job that we requested it to be terminated
UpdateJobLogTerminationRequested(string) error
// Updates information on task arns and ec2 metadata
UpdateTaskArnsInstanceIDs(map[string]Ec2Info, map[string]string) error
// Updates information on EC2 instances running on ECS
UpdateECSInstances(map[string]Ec2Info, map[string][]string) error
// Gets alive EC2 instances (according to database)
GetAliveEC2Instances() ([]string, error)
// Gets all instance IDs that have jobs stuck in "STARTING" status
GetStartingStateStuckEC2Instances() ([]string, error)
// Subscribes to updates about a job status. (see more info on this
// function in postgres_store.go)
SubscribeToJobStatus(jobID string) (<-chan Job, func())
}
// Killer is an interface to kill jobs in the queue
type Killer interface {
// KillOne kills a job matching the query
KillOne(jobID string, reason string, store Storer) error
// Kills jobs and instances that are stuck in STARTING status
KillInstances(instances []string) error
}
// This structure describes how many vcpus and memory the currently queued jobs require
type RunningLoad struct {
WantedVCpus int64
WantedMemory int64
}
type ComputeEnvironment struct {
Name string
WantedvCpus int64
MinvCpus int64
MaxvCpus int64
State string
ServiceRole string
}
type JobSummary struct {
JobQueue string
Submitted int64
Pending int64
Runnable int64
Starting int64
Running int64
}
type Ec2Info struct {
PrivateIP *string
PublicIP *string
AMI string
ComputeEnvironmentARN string
ECSClusterARN string
AvailabilityZone string
SpotInstanceRequestID *string
InstanceType string
LaunchedAt *time.Time
}