-
Notifications
You must be signed in to change notification settings - Fork 13
/
job.go
211 lines (185 loc) · 6.73 KB
/
job.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
209
210
211
package opslevel
import (
"encoding/base64"
"strings"
"github.com/hasura/go-graphql-client"
"github.com/relvacode/iso8601"
)
// RunnerJobOutcomeEnum represents the runner job outcome.
type RunnerJobOutcomeEnum string
const (
RunnerJobOutcomeEnumUnstarted RunnerJobOutcomeEnum = "unstarted" // translation missing: en.graphql.types.runner_job_outcome_enum.unstarted.
RunnerJobOutcomeEnumCanceled RunnerJobOutcomeEnum = "canceled" // Job was canceled.
RunnerJobOutcomeEnumFailed RunnerJobOutcomeEnum = "failed" // Job failed during execution.
RunnerJobOutcomeEnumSuccess RunnerJobOutcomeEnum = "success" // Job succeded the execution.
RunnerJobOutcomeEnumQueueTimeout RunnerJobOutcomeEnum = "queue_timeout" // Job was not assigned to a runner for too long.
RunnerJobOutcomeEnumExecutionTimeout RunnerJobOutcomeEnum = "execution_timeout" // Job run took too long to complete, and was marked as failed.
RunnerJobOutcomeEnumPodTimeout RunnerJobOutcomeEnum = "pod_timeout" // A pod could not be scheduled for the job in time.
)
// All RunnerJobOutcomeEnum as []string
func AllRunnerJobOutcomeEnum() []string {
return []string{
string(RunnerJobOutcomeEnumUnstarted),
string(RunnerJobOutcomeEnumCanceled),
string(RunnerJobOutcomeEnumFailed),
string(RunnerJobOutcomeEnumSuccess),
string(RunnerJobOutcomeEnumQueueTimeout),
string(RunnerJobOutcomeEnumExecutionTimeout),
string(RunnerJobOutcomeEnumPodTimeout),
}
}
// RunnerJobStatusEnum represents the runner job status.
type RunnerJobStatusEnum string
const (
RunnerJobStatusEnumCreated RunnerJobStatusEnum = "created" // A created runner job, but not yet ready to be run.
RunnerJobStatusEnumPending RunnerJobStatusEnum = "pending" // A runner job ready to be run.
RunnerJobStatusEnumRunning RunnerJobStatusEnum = "running" // A runner job being run by a runner.
RunnerJobStatusEnumComplete RunnerJobStatusEnum = "complete" // A finished runner job.
)
// All RunnerJobStatusEnum as []string
func AllRunnerJobStatusEnum() []string {
return []string{
string(RunnerJobStatusEnumCreated),
string(RunnerJobStatusEnumPending),
string(RunnerJobStatusEnumRunning),
string(RunnerJobStatusEnumComplete),
}
}
// RunnerStatusTypeEnum represents The status of an OpsLevel runner.
type RunnerStatusTypeEnum string
const (
RunnerStatusTypeEnumInactive RunnerJobStatusEnum = "inactive" // The runner will not actively take jobs.
RunnerStatusTypeEnumRegistered RunnerJobStatusEnum = "registered" // The runner will process jobs.
)
// All RunnerStatusTypeEnum as []string
func AllRunnerStatusTypeEnum() []string {
return []string{
string(RunnerStatusTypeEnumInactive),
string(RunnerStatusTypeEnumRegistered),
}
}
type Runner struct {
Id ID `json:"id"`
Status RunnerStatusTypeEnum `json:"status"`
}
type RunnerJobVariable struct {
Key string `json:"key"`
Sensitive bool `json:"sensitive"`
Value string `json:"value"`
}
type RunnerJobFile struct {
Name string `json:"name"`
Contents string `json:"contents"`
}
type RunnerJob struct {
Commands []string `json:"commands"`
Id ID `json:"id"`
Image string `json:"image"`
Outcome RunnerJobOutcomeEnum `json:"outcome"`
Status RunnerJobStatusEnum `json:"status"`
Variables []RunnerJobVariable `json:"variables"`
Files []RunnerJobFile `json:"files"`
}
func (j *RunnerJob) Number() string {
id := string(j.Id)
decoded, err := base64.RawURLEncoding.DecodeString(id)
if err != nil {
return id
}
return strings.ReplaceAll(string(decoded), "gid://opslevel/Runners::JobRun/", "")
}
type RunnerAppendJobLogInput struct {
RunnerId ID `json:"runnerId"`
RunnerJobId ID `json:"runnerJobId"`
SentAt iso8601.Time `json:"sentAt"`
Logs []string `json:"logChunk"`
}
type RunnerJobOutcomeVariable struct {
Key string `json:"key"`
Value string `json:"value"`
}
type RunnerReportJobOutcomeInput struct {
RunnerId ID `json:"runnerId"`
RunnerJobId ID `json:"runnerJobId"`
Outcome RunnerJobOutcomeEnum `json:"outcome"`
OutcomeVariables []RunnerJobOutcomeVariable `json:"outcomeVariables,omitempty"`
}
type RunnerScale struct {
RecommendedReplicaCount int `json:"recommendedReplicaCount"`
}
func (c *Client) RunnerRegister() (*Runner, error) {
var m struct {
Payload struct {
Runner Runner
Errors []OpsLevelErrors
} `graphql:"runnerRegister"`
}
v := PayloadVariables{}
err := c.Mutate(&m, v, WithName("RunnerRegister"))
return &m.Payload.Runner, HandleErrors(err, m.Payload.Errors)
}
func (c *Client) RunnerGetPendingJob(runnerId ID, lastUpdateToken ID) (*RunnerJob, ID, error) {
var m struct {
Payload struct {
RunnerJob RunnerJob
LastUpdateToken ID
Errors []OpsLevelErrors
} `graphql:"runnerGetPendingJob(runnerId: $id lastUpdateToken: $token)"`
}
v := PayloadVariables{
"id": runnerId,
"token": &lastUpdateToken,
}
err := c.Mutate(&m, v, WithName("RunnerGetPendingJob"))
return &m.Payload.RunnerJob, m.Payload.LastUpdateToken, HandleErrors(err, m.Payload.Errors)
}
func (c *Client) RunnerScale(runnerId ID, currentReplicaCount, jobConcurrency int) (*RunnerScale, error) {
var q struct {
Account struct {
RunnerScale RunnerScale `graphql:"runnerScale(runnerId: $runnerId, currentReplicaCount: $currentReplicaCount, jobConcurrency: $jobConcurrency)"`
}
}
v := PayloadVariables{
"runnerId": runnerId,
"currentReplicaCount": graphql.Int(currentReplicaCount),
"jobConcurrency": graphql.Int(jobConcurrency),
}
err := c.Query(&q, v, WithName("RunnerScale"))
return &q.Account.RunnerScale, HandleErrors(err, nil)
}
func (c *Client) RunnerAppendJobLog(input RunnerAppendJobLogInput) error {
var m struct {
Payload struct {
Errors []OpsLevelErrors
} `graphql:"runnerAppendJobLog(input: $input)"`
}
v := PayloadVariables{
"input": input,
}
err := c.Mutate(&m, v, WithName("RunnerAppendJobLog"))
return HandleErrors(err, m.Payload.Errors)
}
func (c *Client) RunnerReportJobOutcome(input RunnerReportJobOutcomeInput) error {
var m struct {
Payload struct {
Errors []OpsLevelErrors
} `graphql:"runnerReportJobOutcome(input: $input)"`
}
v := PayloadVariables{
"input": input,
}
err := c.Mutate(&m, v, WithName("RunnerReportJobOutcome"))
return HandleErrors(err, m.Payload.Errors)
}
func (c *Client) RunnerUnregister(runnerId ID) error {
var m struct {
Payload struct {
Errors []OpsLevelErrors
} `graphql:"runnerUnregister(runnerId: $runnerId)"`
}
v := PayloadVariables{
"runnerId": runnerId,
}
err := c.Mutate(&m, v, WithName("RunnerUnregister"))
return HandleErrors(err, m.Payload.Errors)
}