-
Notifications
You must be signed in to change notification settings - Fork 13
/
job.go
207 lines (181 loc) · 6.99 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
package opslevel
import (
"encoding/base64"
"strings"
"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 succeeded 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.
)
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.
)
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.
)
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 (runnerJob *RunnerJob) Number() string {
id := string(runnerJob.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" yaml:"runnerId" default:"46290"`
RunnerJobId ID `json:"runnerJobId" yaml:"runnerJobId" default:"4133720"`
SentAt iso8601.Time `json:"sentAt" yaml:"sentAt" default:"2023-11-05T01:00:00.000Z"`
Logs []string `json:"logChunk" yaml:"logChunk" default:"[\"LogRoger\",\"LogDodger\"]"`
}
type RunnerJobOutcomeVariable struct {
Key string `json:"key" yaml:"key" default:"job_task"`
Value string `json:"value" yaml:"value" default:"job_status"`
}
type RunnerReportJobOutcomeInput struct {
RunnerId ID `json:"runnerId" yaml:"runnerId" default:"42690"`
RunnerJobId ID `json:"runnerJobId" yaml:"runnerJobId" default:"4213370"`
Outcome RunnerJobOutcomeEnum `json:"outcome" yaml:"outcome" default:"pod_timeout"`
OutcomeVariables []RunnerJobOutcomeVariable `json:"outcomeVariables,omitempty" yaml:"outcomeVariables,omitempty"`
}
type RunnerScale struct {
RecommendedReplicaCount int `json:"recommendedReplicaCount"`
}
func (client *Client) RunnerRegister() (*Runner, error) {
var m struct {
Payload struct {
Runner Runner
Errors []OpsLevelErrors
} `graphql:"runnerRegister"`
}
v := PayloadVariables{}
err := client.Mutate(&m, v, WithName("RunnerRegister"))
return &m.Payload.Runner, HandleErrors(err, m.Payload.Errors)
}
func (client *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 := client.Mutate(&m, v, WithName("RunnerGetPendingJob"))
return &m.Payload.RunnerJob, m.Payload.LastUpdateToken, HandleErrors(err, m.Payload.Errors)
}
func (client *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": currentReplicaCount,
"jobConcurrency": jobConcurrency,
}
err := client.Query(&q, v, WithName("RunnerScale"))
return &q.Account.RunnerScale, HandleErrors(err, nil)
}
func (client *Client) RunnerAppendJobLog(input RunnerAppendJobLogInput) error {
var m struct {
Payload struct {
Errors []OpsLevelErrors
} `graphql:"runnerAppendJobLog(input: $input)"`
}
v := PayloadVariables{
"input": input,
}
err := client.Mutate(&m, v, WithName("RunnerAppendJobLog"))
return HandleErrors(err, m.Payload.Errors)
}
func (client *Client) RunnerReportJobOutcome(input RunnerReportJobOutcomeInput) error {
var m struct {
Payload struct {
Errors []OpsLevelErrors
} `graphql:"runnerReportJobOutcome(input: $input)"`
}
v := PayloadVariables{
"input": input,
}
err := client.Mutate(&m, v, WithName("RunnerReportJobOutcome"))
return HandleErrors(err, m.Payload.Errors)
}
func (client *Client) RunnerUnregister(runnerId ID) error {
var m struct {
Payload struct {
Errors []OpsLevelErrors
} `graphql:"runnerUnregister(runnerId: $runnerId)"`
}
v := PayloadVariables{
"runnerId": runnerId,
}
err := client.Mutate(&m, v, WithName("RunnerUnregister"))
return HandleErrors(err, m.Payload.Errors)
}