1 import { Transaction } from 'sequelize'
2 import { retryTransactionWrapper } from '@server/helpers/database-utils'
3 import { logger } from '@server/helpers/logger'
4 import { CONFIG } from '@server/initializers/config'
5 import { sequelizeTypescript } from '@server/initializers/database'
6 import { VideoModel } from '@server/models/video/video'
7 import { VideoJobInfoModel } from '@server/models/video/video-job-info'
8 import { MVideo, MVideoFullLight, MVideoUUID } from '@server/types/models'
9 import { VideoState } from '@shared/models'
10 import { federateVideoIfNeeded } from './activitypub/videos'
11 import { JobQueue } from './job-queue'
12 import { Notifier } from './notifier'
13 import { buildMoveToObjectStorageJob } from './video'
15 function buildNextVideoState (currentState?: VideoState) {
16 if (currentState === VideoState.PUBLISHED) {
17 throw new Error('Video is already in its final state')
21 currentState !== VideoState.TO_EDIT &&
22 currentState !== VideoState.TO_TRANSCODE &&
23 currentState !== VideoState.TO_MOVE_TO_EXTERNAL_STORAGE &&
24 CONFIG.TRANSCODING.ENABLED
26 return VideoState.TO_TRANSCODE
30 currentState !== VideoState.TO_MOVE_TO_EXTERNAL_STORAGE &&
31 CONFIG.OBJECT_STORAGE.ENABLED
33 return VideoState.TO_MOVE_TO_EXTERNAL_STORAGE
36 return VideoState.PUBLISHED
39 function moveToNextState (options: {
41 previousVideoState?: VideoState
42 isNewVideo?: boolean // Default true
44 const { video, previousVideoState, isNewVideo = true } = options
46 return retryTransactionWrapper(() => {
47 return sequelizeTypescript.transaction(async t => {
48 // Maybe the video changed in database, refresh it
49 const videoDatabase = await VideoModel.loadFull(video.uuid, t)
50 // Video does not exist anymore
51 if (!videoDatabase) return undefined
53 // Already in its final state
54 if (videoDatabase.state === VideoState.PUBLISHED) {
55 return federateVideoIfNeeded(videoDatabase, false, t)
58 const newState = buildNextVideoState(videoDatabase.state)
60 if (newState === VideoState.PUBLISHED) {
61 return moveToPublishedState({ video: videoDatabase, previousVideoState, isNewVideo, transaction: t })
64 if (newState === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) {
65 return moveToExternalStorageState({ video: videoDatabase, isNewVideo, transaction: t })
71 async function moveToExternalStorageState (options: {
72 video: MVideoFullLight
74 transaction: Transaction
76 const { video, isNewVideo, transaction } = options
78 const videoJobInfo = await VideoJobInfoModel.load(video.id, transaction)
79 const pendingTranscode = videoJobInfo?.pendingTranscode || 0
81 // We want to wait all transcoding jobs before moving the video on an external storage
82 if (pendingTranscode !== 0) return false
84 const previousVideoState = video.state
86 if (video.state !== VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) {
87 await video.setNewState(VideoState.TO_MOVE_TO_EXTERNAL_STORAGE, isNewVideo, transaction)
90 logger.info('Creating external storage move job for video %s.', video.uuid, { tags: [ video.uuid ] })
93 await JobQueue.Instance.createJob(await buildMoveToObjectStorageJob({ video, previousVideoState, isNewVideo }))
97 logger.error('Cannot add move to object storage job', { err })
103 function moveToFailedTranscodingState (video: MVideo) {
104 if (video.state === VideoState.TRANSCODING_FAILED) return
106 return video.setNewState(VideoState.TRANSCODING_FAILED, false, undefined)
109 function moveToFailedMoveToObjectStorageState (video: MVideo) {
110 if (video.state === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE_FAILED) return
112 return video.setNewState(VideoState.TO_MOVE_TO_EXTERNAL_STORAGE_FAILED, false, undefined)
115 // ---------------------------------------------------------------------------
119 moveToExternalStorageState,
120 moveToFailedTranscodingState,
121 moveToFailedMoveToObjectStorageState,
125 // ---------------------------------------------------------------------------
127 async function moveToPublishedState (options: {
128 video: MVideoFullLight
130 transaction: Transaction
131 previousVideoState?: VideoState
133 const { video, isNewVideo, transaction, previousVideoState } = options
134 const previousState = previousVideoState ?? video.state
136 logger.info('Publishing video %s.', video.uuid, { isNewVideo, previousState, tags: [ video.uuid ] })
138 await video.setNewState(VideoState.PUBLISHED, isNewVideo, transaction)
140 await federateVideoIfNeeded(video, isNewVideo, transaction)
142 if (previousState === VideoState.TO_EDIT) {
143 Notifier.Instance.notifyOfFinishedVideoStudioEdition(video)
148 Notifier.Instance.notifyOnNewVideoIfNeeded(video)
150 if (previousState === VideoState.TO_TRANSCODE) {
151 Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(video)