aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/runner/runner-job.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/runner/runner-job.ts')
-rw-r--r--server/models/runner/runner-job.ts357
1 files changed, 0 insertions, 357 deletions
diff --git a/server/models/runner/runner-job.ts b/server/models/runner/runner-job.ts
deleted file mode 100644
index f2ffd6a84..000000000
--- a/server/models/runner/runner-job.ts
+++ /dev/null
@@ -1,357 +0,0 @@
1import { Op, Transaction } from 'sequelize'
2import {
3 AllowNull,
4 BelongsTo,
5 Column,
6 CreatedAt,
7 DataType,
8 Default,
9 ForeignKey,
10 IsUUID,
11 Model,
12 Scopes,
13 Table,
14 UpdatedAt
15} from 'sequelize-typescript'
16import { isArray, isUUIDValid } from '@server/helpers/custom-validators/misc'
17import { CONSTRAINTS_FIELDS, RUNNER_JOB_STATES } from '@server/initializers/constants'
18import { MRunnerJob, MRunnerJobRunner, MRunnerJobRunnerParent } from '@server/types/models/runners'
19import { RunnerJob, RunnerJobAdmin, RunnerJobPayload, RunnerJobPrivatePayload, RunnerJobState, RunnerJobType } from '@shared/models'
20import { AttributesOnly } from '@shared/typescript-utils'
21import { getSort, searchAttribute } from '../shared'
22import { RunnerModel } from './runner'
23
24enum ScopeNames {
25 WITH_RUNNER = 'WITH_RUNNER',
26 WITH_PARENT = 'WITH_PARENT'
27}
28
29@Scopes(() => ({
30 [ScopeNames.WITH_RUNNER]: {
31 include: [
32 {
33 model: RunnerModel.unscoped(),
34 required: false
35 }
36 ]
37 },
38 [ScopeNames.WITH_PARENT]: {
39 include: [
40 {
41 model: RunnerJobModel.unscoped(),
42 required: false
43 }
44 ]
45 }
46}))
47@Table({
48 tableName: 'runnerJob',
49 indexes: [
50 {
51 fields: [ 'uuid' ],
52 unique: true
53 },
54 {
55 fields: [ 'processingJobToken' ],
56 unique: true
57 },
58 {
59 fields: [ 'runnerId' ]
60 }
61 ]
62})
63export class RunnerJobModel extends Model<Partial<AttributesOnly<RunnerJobModel>>> {
64
65 @AllowNull(false)
66 @IsUUID(4)
67 @Column(DataType.UUID)
68 uuid: string
69
70 @AllowNull(false)
71 @Column
72 type: RunnerJobType
73
74 @AllowNull(false)
75 @Column(DataType.JSONB)
76 payload: RunnerJobPayload
77
78 @AllowNull(false)
79 @Column(DataType.JSONB)
80 privatePayload: RunnerJobPrivatePayload
81
82 @AllowNull(false)
83 @Column
84 state: RunnerJobState
85
86 @AllowNull(false)
87 @Default(0)
88 @Column
89 failures: number
90
91 @AllowNull(true)
92 @Column(DataType.STRING(CONSTRAINTS_FIELDS.RUNNER_JOBS.ERROR_MESSAGE.max))
93 error: string
94
95 // Less has priority
96 @AllowNull(false)
97 @Column
98 priority: number
99
100 // Used to fetch the appropriate job when the runner wants to post the result
101 @AllowNull(true)
102 @Column
103 processingJobToken: string
104
105 @AllowNull(true)
106 @Column
107 progress: number
108
109 @AllowNull(true)
110 @Column
111 startedAt: Date
112
113 @AllowNull(true)
114 @Column
115 finishedAt: Date
116
117 @CreatedAt
118 createdAt: Date
119
120 @UpdatedAt
121 updatedAt: Date
122
123 @ForeignKey(() => RunnerJobModel)
124 @Column
125 dependsOnRunnerJobId: number
126
127 @BelongsTo(() => RunnerJobModel, {
128 foreignKey: {
129 name: 'dependsOnRunnerJobId',
130 allowNull: true
131 },
132 onDelete: 'cascade'
133 })
134 DependsOnRunnerJob: RunnerJobModel
135
136 @ForeignKey(() => RunnerModel)
137 @Column
138 runnerId: number
139
140 @BelongsTo(() => RunnerModel, {
141 foreignKey: {
142 name: 'runnerId',
143 allowNull: true
144 },
145 onDelete: 'SET NULL'
146 })
147 Runner: RunnerModel
148
149 // ---------------------------------------------------------------------------
150
151 static loadWithRunner (uuid: string) {
152 const query = {
153 where: { uuid }
154 }
155
156 return RunnerJobModel.scope(ScopeNames.WITH_RUNNER).findOne<MRunnerJobRunner>(query)
157 }
158
159 static loadByRunnerAndJobTokensWithRunner (options: {
160 uuid: string
161 runnerToken: string
162 jobToken: string
163 }) {
164 const { uuid, runnerToken, jobToken } = options
165
166 const query = {
167 where: {
168 uuid,
169 processingJobToken: jobToken
170 },
171 include: {
172 model: RunnerModel.unscoped(),
173 required: true,
174 where: {
175 runnerToken
176 }
177 }
178 }
179
180 return RunnerJobModel.findOne<MRunnerJobRunner>(query)
181 }
182
183 static listAvailableJobs () {
184 const query = {
185 limit: 10,
186 order: getSort('priority'),
187 where: {
188 state: RunnerJobState.PENDING
189 }
190 }
191
192 return RunnerJobModel.findAll<MRunnerJob>(query)
193 }
194
195 static listStalledJobs (options: {
196 staleTimeMS: number
197 types: RunnerJobType[]
198 }) {
199 const before = new Date(Date.now() - options.staleTimeMS)
200
201 return RunnerJobModel.findAll<MRunnerJob>({
202 where: {
203 type: {
204 [Op.in]: options.types
205 },
206 state: RunnerJobState.PROCESSING,
207 updatedAt: {
208 [Op.lt]: before
209 }
210 }
211 })
212 }
213
214 static listChildrenOf (job: MRunnerJob, transaction?: Transaction) {
215 const query = {
216 where: {
217 dependsOnRunnerJobId: job.id
218 },
219 transaction
220 }
221
222 return RunnerJobModel.findAll<MRunnerJob>(query)
223 }
224
225 static listForApi (options: {
226 start: number
227 count: number
228 sort: string
229 search?: string
230 stateOneOf?: RunnerJobState[]
231 }) {
232 const { start, count, sort, search, stateOneOf } = options
233
234 const query = {
235 offset: start,
236 limit: count,
237 order: getSort(sort),
238 where: []
239 }
240
241 if (search) {
242 if (isUUIDValid(search)) {
243 query.where.push({ uuid: search })
244 } else {
245 query.where.push({
246 [Op.or]: [
247 searchAttribute(search, 'type'),
248 searchAttribute(search, '$Runner.name$')
249 ]
250 })
251 }
252 }
253
254 if (isArray(stateOneOf) && stateOneOf.length !== 0) {
255 query.where.push({
256 state: {
257 [Op.in]: stateOneOf
258 }
259 })
260 }
261
262 return Promise.all([
263 RunnerJobModel.scope([ ScopeNames.WITH_RUNNER ]).count(query),
264 RunnerJobModel.scope([ ScopeNames.WITH_RUNNER, ScopeNames.WITH_PARENT ]).findAll<MRunnerJobRunnerParent>(query)
265 ]).then(([ total, data ]) => ({ total, data }))
266 }
267
268 static updateDependantJobsOf (runnerJob: MRunnerJob) {
269 const where = {
270 dependsOnRunnerJobId: runnerJob.id
271 }
272
273 return RunnerJobModel.update({ state: RunnerJobState.PENDING }, { where })
274 }
275
276 static cancelAllJobs (options: { type: RunnerJobType }) {
277 const where = {
278 type: options.type
279 }
280
281 return RunnerJobModel.update({ state: RunnerJobState.CANCELLED }, { where })
282 }
283
284 // ---------------------------------------------------------------------------
285
286 resetToPending () {
287 this.state = RunnerJobState.PENDING
288 this.processingJobToken = null
289 this.progress = null
290 this.startedAt = null
291 this.runnerId = null
292 }
293
294 setToErrorOrCancel (
295 state: RunnerJobState.PARENT_ERRORED | RunnerJobState.ERRORED | RunnerJobState.CANCELLED | RunnerJobState.PARENT_CANCELLED
296 ) {
297 this.state = state
298 this.processingJobToken = null
299 this.finishedAt = new Date()
300 }
301
302 toFormattedJSON (this: MRunnerJobRunnerParent): RunnerJob {
303 const runner = this.Runner
304 ? {
305 id: this.Runner.id,
306 name: this.Runner.name,
307 description: this.Runner.description
308 }
309 : null
310
311 const parent = this.DependsOnRunnerJob
312 ? {
313 id: this.DependsOnRunnerJob.id,
314 uuid: this.DependsOnRunnerJob.uuid,
315 type: this.DependsOnRunnerJob.type,
316 state: {
317 id: this.DependsOnRunnerJob.state,
318 label: RUNNER_JOB_STATES[this.DependsOnRunnerJob.state]
319 }
320 }
321 : undefined
322
323 return {
324 uuid: this.uuid,
325 type: this.type,
326
327 state: {
328 id: this.state,
329 label: RUNNER_JOB_STATES[this.state]
330 },
331
332 progress: this.progress,
333 priority: this.priority,
334 failures: this.failures,
335 error: this.error,
336
337 payload: this.payload,
338
339 startedAt: this.startedAt?.toISOString(),
340 finishedAt: this.finishedAt?.toISOString(),
341
342 createdAt: this.createdAt.toISOString(),
343 updatedAt: this.updatedAt.toISOString(),
344
345 parent,
346 runner
347 }
348 }
349
350 toFormattedAdminJSON (this: MRunnerJobRunnerParent): RunnerJobAdmin {
351 return {
352 ...this.toFormattedJSON(),
353
354 privatePayload: this.privatePayload
355 }
356 }
357}