1 import { Transaction } from 'sequelize'
2 import { logger } from '@server/helpers/logger'
3 import { CONFIG } from '@server/initializers/config'
4 import { sequelizeTypescript } from '@server/initializers/database'
5 import { VideoModel } from '@server/models/video/video'
6 import { VideoJobInfoModel } from '@server/models/video/video-job-info'
7 import { MVideo, MVideoFullLight, MVideoUUID } from '@server/types/models'
8 import { VideoState } from '@shared/models'
9 import { federateVideoIfNeeded } from './activitypub/videos'
10 import { Notifier } from './notifier'
11 import { addMoveToObjectStorageJob } from './video'
13 function buildNextVideoState (currentState?: VideoState) {
14 if (currentState === VideoState.PUBLISHED) {
15 throw new Error('Video is already in its final state')
19 currentState !== VideoState.TO_EDIT &&
20 currentState !== VideoState.TO_TRANSCODE &&
21 currentState !== VideoState.TO_MOVE_TO_EXTERNAL_STORAGE &&
22 CONFIG.TRANSCODING.ENABLED
24 return VideoState.TO_TRANSCODE
28 currentState !== VideoState.TO_MOVE_TO_EXTERNAL_STORAGE &&
29 CONFIG.OBJECT_STORAGE.ENABLED
31 return VideoState.TO_MOVE_TO_EXTERNAL_STORAGE
34 return VideoState.PUBLISHED
37 function moveToNextState (options: {
39 previousVideoState?: VideoState
40 isNewVideo?: boolean // Default true
42 const { video, previousVideoState, isNewVideo = true } = options
44 return sequelizeTypescript.transaction(async t => {
45 // Maybe the video changed in database, refresh it
46 const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t)
47 // Video does not exist anymore
48 if (!videoDatabase) return undefined
50 // Already in its final state
51 if (videoDatabase.state === VideoState.PUBLISHED) {
52 return federateVideoIfNeeded(videoDatabase, false, t)
55 const newState = buildNextVideoState(videoDatabase.state)
57 if (newState === VideoState.PUBLISHED) {
58 return moveToPublishedState({ video: videoDatabase, previousVideoState, isNewVideo, transaction: t })
61 if (newState === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) {
62 return moveToExternalStorageState({ video: videoDatabase, isNewVideo, transaction: t })
67 async function moveToExternalStorageState (options: {
68 video: MVideoFullLight
70 transaction: Transaction
72 const { video, isNewVideo, transaction } = options
74 const videoJobInfo = await VideoJobInfoModel.load(video.id, transaction)
75 const pendingTranscode = videoJobInfo?.pendingTranscode || 0
77 // We want to wait all transcoding jobs before moving the video on an external storage
78 if (pendingTranscode !== 0) return false
80 const previousVideoState = video.state
81 await video.setNewState(VideoState.TO_MOVE_TO_EXTERNAL_STORAGE, isNewVideo, transaction)
83 logger.info('Creating external storage move job for video %s.', video.uuid, { tags: [ video.uuid ] })
86 await addMoveToObjectStorageJob({ video, previousVideoState, isNewVideo })
90 logger.error('Cannot add move to object storage job', { err })
96 function moveToFailedTranscodingState (video: MVideo) {
97 if (video.state === VideoState.TRANSCODING_FAILED) return
99 return video.setNewState(VideoState.TRANSCODING_FAILED, false, undefined)
102 function moveToFailedMoveToObjectStorageState (video: MVideo) {
103 if (video.state === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE_FAILED) return
105 return video.setNewState(VideoState.TO_MOVE_TO_EXTERNAL_STORAGE_FAILED, false, undefined)
108 // ---------------------------------------------------------------------------
112 moveToExternalStorageState,
113 moveToFailedTranscodingState,
114 moveToFailedMoveToObjectStorageState,
118 // ---------------------------------------------------------------------------
120 async function moveToPublishedState (options: {
121 video: MVideoFullLight
123 transaction: Transaction
124 previousVideoState?: VideoState
126 const { video, isNewVideo, transaction, previousVideoState } = options
127 const previousState = previousVideoState ?? video.state
129 logger.info('Publishing video %s.', video.uuid, { isNewVideo, previousState, tags: [ video.uuid ] })
131 await video.setNewState(VideoState.PUBLISHED, isNewVideo, transaction)
133 await federateVideoIfNeeded(video, isNewVideo, transaction)
135 if (previousState === VideoState.TO_EDIT) {
136 Notifier.Instance.notifyOfFinishedVideoStudioEdition(video)
141 Notifier.Instance.notifyOnNewVideoIfNeeded(video)
143 if (previousState === VideoState.TO_TRANSCODE) {
144 Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(video)