X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=server%2Flib%2Fvideo-state.ts;h=893725d85012f674b96d7274574c66c2d37d26d5;hb=dc9c9500bf5f0fd66906576ee3df4f1c49a1871d;hp=ae2725d650c6f2c90a7d6cdacc3133fe6642786f;hpb=26e3e98ff0e222a9fb9226938ac6902af77921bd;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/video-state.ts b/server/lib/video-state.ts index ae2725d65..893725d85 100644 --- a/server/lib/video-state.ts +++ b/server/lib/video-state.ts @@ -1,4 +1,5 @@ import { Transaction } from 'sequelize' +import { retryTransactionWrapper } from '@server/helpers/database-utils' import { logger } from '@server/helpers/logger' import { CONFIG } from '@server/initializers/config' import { sequelizeTypescript } from '@server/initializers/database' @@ -7,8 +8,9 @@ import { VideoJobInfoModel } from '@server/models/video/video-job-info' import { MVideo, MVideoFullLight, MVideoUUID } from '@server/types/models' import { VideoState } from '@shared/models' import { federateVideoIfNeeded } from './activitypub/videos' +import { JobQueue } from './job-queue' import { Notifier } from './notifier' -import { addMoveToObjectStorageJob } from './video' +import { buildMoveToObjectStorageJob } from './video' function buildNextVideoState (currentState?: VideoState) { if (currentState === VideoState.PUBLISHED) { @@ -41,26 +43,28 @@ function moveToNextState (options: { }) { const { video, previousVideoState, isNewVideo = true } = options - return sequelizeTypescript.transaction(async t => { - // Maybe the video changed in database, refresh it - const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t) - // Video does not exist anymore - if (!videoDatabase) return undefined + return retryTransactionWrapper(() => { + return sequelizeTypescript.transaction(async t => { + // Maybe the video changed in database, refresh it + const videoDatabase = await VideoModel.loadFull(video.uuid, t) + // Video does not exist anymore + if (!videoDatabase) return undefined - // Already in its final state - if (videoDatabase.state === VideoState.PUBLISHED) { - return federateVideoIfNeeded(videoDatabase, false, t) - } + // Already in its final state + if (videoDatabase.state === VideoState.PUBLISHED) { + return federateVideoIfNeeded(videoDatabase, false, t) + } - const newState = buildNextVideoState(videoDatabase.state) + const newState = buildNextVideoState(videoDatabase.state) - if (newState === VideoState.PUBLISHED) { - return moveToPublishedState({ video: videoDatabase, previousVideoState, isNewVideo, transaction: t }) - } + if (newState === VideoState.PUBLISHED) { + return moveToPublishedState({ video: videoDatabase, previousVideoState, isNewVideo, transaction: t }) + } - if (newState === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) { - return moveToExternalStorageState({ video: videoDatabase, isNewVideo, transaction: t }) - } + if (newState === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) { + return moveToExternalStorageState({ video: videoDatabase, isNewVideo, transaction: t }) + } + }) }) } @@ -78,12 +82,15 @@ async function moveToExternalStorageState (options: { if (pendingTranscode !== 0) return false const previousVideoState = video.state - await video.setNewState(VideoState.TO_MOVE_TO_EXTERNAL_STORAGE, isNewVideo, transaction) + + if (video.state !== VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) { + await video.setNewState(VideoState.TO_MOVE_TO_EXTERNAL_STORAGE, isNewVideo, transaction) + } logger.info('Creating external storage move job for video %s.', video.uuid, { tags: [ video.uuid ] }) try { - await addMoveToObjectStorageJob({ video, previousVideoState, isNewVideo }) + await JobQueue.Instance.createJob(await buildMoveToObjectStorageJob({ video, previousVideoState, isNewVideo })) return true } catch (err) {