]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/runners/jobs.ts
More robust accept job controller
[github/Chocobozzz/PeerTube.git] / server / controllers / api / runners / jobs.ts
index 8e34c07a38ee0a79d3fe5e29edb4936d97fa75ca..bdeb0c6cd73358a8778daf310820209bc979debd 100644 (file)
@@ -1,4 +1,5 @@
 import express, { UploadFiles } from 'express'
+import { retryTransactionWrapper } from '@server/helpers/database-utils'
 import { createReqFiles } from '@server/helpers/express-utils'
 import { logger, loggerTagsFactory } from '@server/helpers/logger'
 import { generateRunnerJobToken } from '@server/helpers/token-generator'
@@ -42,7 +43,7 @@ import {
   RunnerJobUpdateBody,
   RunnerJobUpdatePayload,
   UserRight,
-  VideoEditionTranscodingSuccess,
+  VideoStudioTranscodingSuccess,
   VODAudioMergeTranscodingSuccess,
   VODHLSTranscodingSuccess,
   VODWebVideoTranscodingSuccess
@@ -161,14 +162,29 @@ async function acceptRunnerJob (req: express.Request, res: express.Response) {
   const runner = res.locals.runner
   const runnerJob = res.locals.runnerJob
 
-  runnerJob.state = RunnerJobState.PROCESSING
-  runnerJob.processingJobToken = generateRunnerJobToken()
-  runnerJob.startedAt = new Date()
-  runnerJob.runnerId = runner.id
+  const newRunnerJob = await retryTransactionWrapper(() => {
+    return sequelizeTypescript.transaction(async transaction => {
+      await runnerJob.reload({ transaction })
 
-  const newRunnerJob = await sequelizeTypescript.transaction(transaction => {
-    return runnerJob.save({ transaction })
+      if (runnerJob.state !== RunnerJobState.PENDING) {
+        res.fail({
+          message: 'This job is not in pending state anymore',
+          status: HttpStatusCode.CONFLICT_409
+        })
+
+        return undefined
+      }
+
+      runnerJob.state = RunnerJobState.PROCESSING
+      runnerJob.processingJobToken = generateRunnerJobToken()
+      runnerJob.startedAt = new Date()
+      runnerJob.runnerId = runner.id
+
+      return runnerJob.save({ transaction })
+    })
   })
+  if (!newRunnerJob) return
+
   newRunnerJob.Runner = runner as RunnerModel
 
   const result: AcceptRunnerJobResult = {
@@ -300,7 +316,7 @@ const jobSuccessPayloadBuilders: {
     }
   },
 
-  'video-edition-transcoding': (payload: VideoEditionTranscodingSuccess, files) => {
+  'video-studio-transcoding': (payload: VideoStudioTranscodingSuccess, files) => {
     return {
       ...payload,