aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/runners
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-05-17 14:47:17 +0200
committerChocobozzz <me@florianbigard.com>2023-05-17 14:47:17 +0200
commitd38541fd59f3e590135612ea23a660a31b1eb68b (patch)
treef66cabf63425753fa5d9e42d1fde11f1a3411c10 /server/controllers/api/runners
parent77666e343379b475e09076bca8be6c9595bb4e92 (diff)
downloadPeerTube-d38541fd59f3e590135612ea23a660a31b1eb68b.tar.gz
PeerTube-d38541fd59f3e590135612ea23a660a31b1eb68b.tar.zst
PeerTube-d38541fd59f3e590135612ea23a660a31b1eb68b.zip
More robust accept job controller
Diffstat (limited to 'server/controllers/api/runners')
-rw-r--r--server/controllers/api/runners/jobs.ts28
1 files changed, 22 insertions, 6 deletions
diff --git a/server/controllers/api/runners/jobs.ts b/server/controllers/api/runners/jobs.ts
index 3f2a92182..bdeb0c6cd 100644
--- a/server/controllers/api/runners/jobs.ts
+++ b/server/controllers/api/runners/jobs.ts
@@ -1,4 +1,5 @@
1import express, { UploadFiles } from 'express' 1import express, { UploadFiles } from 'express'
2import { retryTransactionWrapper } from '@server/helpers/database-utils'
2import { createReqFiles } from '@server/helpers/express-utils' 3import { createReqFiles } from '@server/helpers/express-utils'
3import { logger, loggerTagsFactory } from '@server/helpers/logger' 4import { logger, loggerTagsFactory } from '@server/helpers/logger'
4import { generateRunnerJobToken } from '@server/helpers/token-generator' 5import { generateRunnerJobToken } from '@server/helpers/token-generator'
@@ -161,14 +162,29 @@ async function acceptRunnerJob (req: express.Request, res: express.Response) {
161 const runner = res.locals.runner 162 const runner = res.locals.runner
162 const runnerJob = res.locals.runnerJob 163 const runnerJob = res.locals.runnerJob
163 164
164 runnerJob.state = RunnerJobState.PROCESSING 165 const newRunnerJob = await retryTransactionWrapper(() => {
165 runnerJob.processingJobToken = generateRunnerJobToken() 166 return sequelizeTypescript.transaction(async transaction => {
166 runnerJob.startedAt = new Date() 167 await runnerJob.reload({ transaction })
167 runnerJob.runnerId = runner.id
168 168
169 const newRunnerJob = await sequelizeTypescript.transaction(transaction => { 169 if (runnerJob.state !== RunnerJobState.PENDING) {
170 return runnerJob.save({ transaction }) 170 res.fail({
171 message: 'This job is not in pending state anymore',
172 status: HttpStatusCode.CONFLICT_409
173 })
174
175 return undefined
176 }
177
178 runnerJob.state = RunnerJobState.PROCESSING
179 runnerJob.processingJobToken = generateRunnerJobToken()
180 runnerJob.startedAt = new Date()
181 runnerJob.runnerId = runner.id
182
183 return runnerJob.save({ transaction })
184 })
171 }) 185 })
186 if (!newRunnerJob) return
187
172 newRunnerJob.Runner = runner as RunnerModel 188 newRunnerJob.Runner = runner as RunnerModel
173 189
174 const result: AcceptRunnerJobResult = { 190 const result: AcceptRunnerJobResult = {