]>
Commit | Line | Data |
---|---|---|
0c9668f7 C |
1 | import express from 'express' |
2 | import { body, param } from 'express-validator' | |
3 | import { isUUIDValid } from '@server/helpers/custom-validators/misc' | |
4 | import { | |
5 | isRunnerJobAbortReasonValid, | |
6 | isRunnerJobErrorMessageValid, | |
7 | isRunnerJobProgressValid, | |
8 | isRunnerJobSuccessPayloadValid, | |
9 | isRunnerJobTokenValid, | |
10 | isRunnerJobUpdatePayloadValid | |
11 | } from '@server/helpers/custom-validators/runners/jobs' | |
12 | import { isRunnerTokenValid } from '@server/helpers/custom-validators/runners/runners' | |
13 | import { cleanUpReqFiles } from '@server/helpers/express-utils' | |
14 | import { RunnerJobModel } from '@server/models/runner/runner-job' | |
15 | import { HttpStatusCode, RunnerJobState, RunnerJobSuccessBody, RunnerJobUpdateBody, ServerErrorCode } from '@shared/models' | |
16 | import { areValidationErrors } from '../shared' | |
17 | ||
18 | const tags = [ 'runner' ] | |
19 | ||
20 | export const acceptRunnerJobValidator = [ | |
21 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
22 | if (res.locals.runnerJob.state !== RunnerJobState.PENDING) { | |
23 | return res.fail({ | |
24 | status: HttpStatusCode.BAD_REQUEST_400, | |
25 | message: 'This runner job is not in pending state', | |
26 | tags | |
27 | }) | |
28 | } | |
29 | ||
30 | return next() | |
31 | } | |
32 | ] | |
33 | ||
34 | export const abortRunnerJobValidator = [ | |
35 | body('reason').custom(isRunnerJobAbortReasonValid), | |
36 | ||
37 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
38 | if (areValidationErrors(req, res, { tags })) return | |
39 | ||
40 | return next() | |
41 | } | |
42 | ] | |
43 | ||
44 | export const updateRunnerJobValidator = [ | |
45 | body('progress').optional().custom(isRunnerJobProgressValid), | |
46 | ||
47 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
48 | if (areValidationErrors(req, res, { tags })) return cleanUpReqFiles(req) | |
49 | ||
50 | const body = req.body as RunnerJobUpdateBody | |
51 | ||
52 | if (isRunnerJobUpdatePayloadValid(body.payload, res.locals.runnerJob.type, req.files) !== true) { | |
53 | cleanUpReqFiles(req) | |
54 | ||
55 | return res.fail({ | |
56 | status: HttpStatusCode.BAD_REQUEST_400, | |
57 | message: 'Payload is invalid', | |
58 | tags | |
59 | }) | |
60 | } | |
61 | ||
62 | return next() | |
63 | } | |
64 | ] | |
65 | ||
66 | export const errorRunnerJobValidator = [ | |
67 | body('message').custom(isRunnerJobErrorMessageValid), | |
68 | ||
69 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
70 | if (areValidationErrors(req, res, { tags })) return | |
71 | ||
72 | return next() | |
73 | } | |
74 | ] | |
75 | ||
76 | export const successRunnerJobValidator = [ | |
77 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
78 | const body = req.body as RunnerJobSuccessBody | |
79 | ||
80 | if (isRunnerJobSuccessPayloadValid(body.payload, res.locals.runnerJob.type, req.files) !== true) { | |
81 | cleanUpReqFiles(req) | |
82 | ||
83 | return res.fail({ | |
84 | status: HttpStatusCode.BAD_REQUEST_400, | |
85 | message: 'Payload is invalid', | |
86 | tags | |
87 | }) | |
88 | } | |
89 | ||
90 | return next() | |
91 | } | |
92 | ] | |
93 | ||
5e47f6ab C |
94 | export const cancelRunnerJobValidator = [ |
95 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
96 | const runnerJob = res.locals.runnerJob | |
97 | ||
98 | const allowedStates = new Set<RunnerJobState>([ | |
99 | RunnerJobState.PENDING, | |
100 | RunnerJobState.PROCESSING, | |
101 | RunnerJobState.WAITING_FOR_PARENT_JOB | |
102 | ]) | |
103 | ||
104 | if (allowedStates.has(runnerJob.state) !== true) { | |
105 | return res.fail({ | |
106 | status: HttpStatusCode.BAD_REQUEST_400, | |
107 | message: 'Cannot cancel this job that is not in "pending", "processing" or "waiting for parent job" state', | |
108 | tags | |
109 | }) | |
110 | } | |
111 | ||
112 | return next() | |
113 | } | |
114 | ] | |
115 | ||
0c9668f7 C |
116 | export const runnerJobGetValidator = [ |
117 | param('jobUUID').custom(isUUIDValid), | |
118 | ||
119 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
120 | if (areValidationErrors(req, res, { tags })) return | |
121 | ||
122 | const runnerJob = await RunnerJobModel.loadWithRunner(req.params.jobUUID) | |
123 | ||
124 | if (!runnerJob) { | |
125 | return res.fail({ | |
126 | status: HttpStatusCode.NOT_FOUND_404, | |
127 | message: 'Unknown runner job', | |
128 | tags | |
129 | }) | |
130 | } | |
131 | ||
132 | res.locals.runnerJob = runnerJob | |
133 | ||
134 | return next() | |
135 | } | |
136 | ] | |
137 | ||
138 | export const jobOfRunnerGetValidator = [ | |
139 | param('jobUUID').custom(isUUIDValid), | |
140 | ||
141 | body('runnerToken').custom(isRunnerTokenValid), | |
142 | body('jobToken').custom(isRunnerJobTokenValid), | |
143 | ||
144 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
145 | if (areValidationErrors(req, res, { tags })) return cleanUpReqFiles(req) | |
146 | ||
147 | const runnerJob = await RunnerJobModel.loadByRunnerAndJobTokensWithRunner({ | |
148 | uuid: req.params.jobUUID, | |
149 | runnerToken: req.body.runnerToken, | |
150 | jobToken: req.body.jobToken | |
151 | }) | |
152 | ||
153 | if (!runnerJob) { | |
154 | cleanUpReqFiles(req) | |
155 | ||
156 | return res.fail({ | |
157 | status: HttpStatusCode.NOT_FOUND_404, | |
158 | message: 'Unknown runner job', | |
159 | tags | |
160 | }) | |
161 | } | |
162 | ||
163 | if (runnerJob.state !== RunnerJobState.PROCESSING) { | |
164 | cleanUpReqFiles(req) | |
165 | ||
166 | return res.fail({ | |
167 | status: HttpStatusCode.BAD_REQUEST_400, | |
168 | type: ServerErrorCode.RUNNER_JOB_NOT_IN_PROCESSING_STATE, | |
169 | message: 'Job is not in "processing" state', | |
170 | tags | |
171 | }) | |
172 | } | |
173 | ||
174 | res.locals.runnerJob = runnerJob | |
175 | ||
176 | return next() | |
177 | } | |
178 | ] |