2021-08-17 08:26:20 +02:00
|
|
|
import { Transaction } from 'sequelize'
|
|
|
|
import { logger } from '@server/helpers/logger'
|
|
|
|
import { CONFIG } from '@server/initializers/config'
|
|
|
|
import { sequelizeTypescript } from '@server/initializers/database'
|
|
|
|
import { VideoModel } from '@server/models/video/video'
|
|
|
|
import { VideoJobInfoModel } from '@server/models/video/video-job-info'
|
2021-12-23 11:09:31 +01:00
|
|
|
import { MVideo, MVideoFullLight, MVideoUUID } from '@server/types/models'
|
2021-08-17 08:26:20 +02:00
|
|
|
import { VideoState } from '@shared/models'
|
|
|
|
import { federateVideoIfNeeded } from './activitypub/videos'
|
|
|
|
import { Notifier } from './notifier'
|
|
|
|
import { addMoveToObjectStorageJob } from './video'
|
2022-07-12 16:25:21 +02:00
|
|
|
import { retryTransactionWrapper } from '@server/helpers/database-utils'
|
2021-08-17 08:26:20 +02:00
|
|
|
|
|
|
|
function buildNextVideoState (currentState?: VideoState) {
|
|
|
|
if (currentState === VideoState.PUBLISHED) {
|
|
|
|
throw new Error('Video is already in its final state')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2022-03-22 14:35:04 +01:00
|
|
|
currentState !== VideoState.TO_EDIT &&
|
2021-08-17 08:26:20 +02:00
|
|
|
currentState !== VideoState.TO_TRANSCODE &&
|
|
|
|
currentState !== VideoState.TO_MOVE_TO_EXTERNAL_STORAGE &&
|
|
|
|
CONFIG.TRANSCODING.ENABLED
|
|
|
|
) {
|
|
|
|
return VideoState.TO_TRANSCODE
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
currentState !== VideoState.TO_MOVE_TO_EXTERNAL_STORAGE &&
|
|
|
|
CONFIG.OBJECT_STORAGE.ENABLED
|
|
|
|
) {
|
|
|
|
return VideoState.TO_MOVE_TO_EXTERNAL_STORAGE
|
|
|
|
}
|
|
|
|
|
|
|
|
return VideoState.PUBLISHED
|
|
|
|
}
|
|
|
|
|
2022-03-22 14:35:04 +01:00
|
|
|
function moveToNextState (options: {
|
|
|
|
video: MVideoUUID
|
|
|
|
previousVideoState?: VideoState
|
|
|
|
isNewVideo?: boolean // Default true
|
|
|
|
}) {
|
|
|
|
const { video, previousVideoState, isNewVideo = true } = options
|
|
|
|
|
2022-07-12 16:25:21 +02:00
|
|
|
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
|
2021-08-17 08:26:20 +02:00
|
|
|
|
2022-07-12 16:25:21 +02:00
|
|
|
// Already in its final state
|
|
|
|
if (videoDatabase.state === VideoState.PUBLISHED) {
|
|
|
|
return federateVideoIfNeeded(videoDatabase, false, t)
|
|
|
|
}
|
2021-08-17 08:26:20 +02:00
|
|
|
|
2022-07-12 16:25:21 +02:00
|
|
|
const newState = buildNextVideoState(videoDatabase.state)
|
2021-08-17 08:26:20 +02:00
|
|
|
|
2022-07-12 16:25:21 +02:00
|
|
|
if (newState === VideoState.PUBLISHED) {
|
|
|
|
return moveToPublishedState({ video: videoDatabase, previousVideoState, isNewVideo, transaction: t })
|
|
|
|
}
|
2021-08-17 08:26:20 +02:00
|
|
|
|
2022-07-12 16:25:21 +02:00
|
|
|
if (newState === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) {
|
|
|
|
return moveToExternalStorageState({ video: videoDatabase, isNewVideo, transaction: t })
|
|
|
|
}
|
|
|
|
})
|
2021-08-17 08:26:20 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-22 14:35:04 +01:00
|
|
|
async function moveToExternalStorageState (options: {
|
|
|
|
video: MVideoFullLight
|
|
|
|
isNewVideo: boolean
|
|
|
|
transaction: Transaction
|
|
|
|
}) {
|
|
|
|
const { video, isNewVideo, transaction } = options
|
|
|
|
|
2021-11-09 11:05:35 +01:00
|
|
|
const videoJobInfo = await VideoJobInfoModel.load(video.id, transaction)
|
|
|
|
const pendingTranscode = videoJobInfo?.pendingTranscode || 0
|
|
|
|
|
|
|
|
// We want to wait all transcoding jobs before moving the video on an external storage
|
|
|
|
if (pendingTranscode !== 0) return false
|
|
|
|
|
2022-03-22 14:35:04 +01:00
|
|
|
const previousVideoState = video.state
|
2021-11-09 11:05:35 +01:00
|
|
|
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 {
|
2022-03-22 14:35:04 +01:00
|
|
|
await addMoveToObjectStorageJob({ video, previousVideoState, isNewVideo })
|
2021-11-09 11:05:35 +01:00
|
|
|
|
|
|
|
return true
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot add move to object storage job', { err })
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-23 11:09:31 +01:00
|
|
|
function moveToFailedTranscodingState (video: MVideo) {
|
2021-11-18 14:35:08 +01:00
|
|
|
if (video.state === VideoState.TRANSCODING_FAILED) return
|
|
|
|
|
2021-11-09 11:52:41 +01:00
|
|
|
return video.setNewState(VideoState.TRANSCODING_FAILED, false, undefined)
|
2021-11-08 04:20:04 +01:00
|
|
|
}
|
|
|
|
|
2021-12-23 11:09:31 +01:00
|
|
|
function moveToFailedMoveToObjectStorageState (video: MVideo) {
|
|
|
|
if (video.state === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE_FAILED) return
|
|
|
|
|
|
|
|
return video.setNewState(VideoState.TO_MOVE_TO_EXTERNAL_STORAGE_FAILED, false, undefined)
|
|
|
|
}
|
|
|
|
|
2021-08-17 08:26:20 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
buildNextVideoState,
|
2021-11-09 11:05:35 +01:00
|
|
|
moveToExternalStorageState,
|
2021-11-09 11:52:41 +01:00
|
|
|
moveToFailedTranscodingState,
|
2021-12-23 11:09:31 +01:00
|
|
|
moveToFailedMoveToObjectStorageState,
|
2021-08-17 08:26:20 +02:00
|
|
|
moveToNextState
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2022-03-22 14:35:04 +01:00
|
|
|
async function moveToPublishedState (options: {
|
|
|
|
video: MVideoFullLight
|
|
|
|
isNewVideo: boolean
|
|
|
|
transaction: Transaction
|
|
|
|
previousVideoState?: VideoState
|
|
|
|
}) {
|
|
|
|
const { video, isNewVideo, transaction, previousVideoState } = options
|
|
|
|
const previousState = previousVideoState ?? video.state
|
|
|
|
|
2022-05-03 11:38:07 +02:00
|
|
|
logger.info('Publishing video %s.', video.uuid, { isNewVideo, previousState, tags: [ video.uuid ] })
|
2021-08-17 08:26:20 +02:00
|
|
|
|
2021-10-08 15:34:07 +02:00
|
|
|
await video.setNewState(VideoState.PUBLISHED, isNewVideo, transaction)
|
2021-08-17 08:26:20 +02:00
|
|
|
|
|
|
|
await federateVideoIfNeeded(video, isNewVideo, transaction)
|
|
|
|
|
2022-03-22 14:35:04 +01:00
|
|
|
if (previousState === VideoState.TO_EDIT) {
|
2022-03-22 16:58:49 +01:00
|
|
|
Notifier.Instance.notifyOfFinishedVideoStudioEdition(video)
|
2022-03-22 14:35:04 +01:00
|
|
|
return
|
|
|
|
}
|
2021-12-01 12:33:46 +01:00
|
|
|
|
2022-03-22 14:35:04 +01:00
|
|
|
if (isNewVideo) {
|
|
|
|
Notifier.Instance.notifyOnNewVideoIfNeeded(video)
|
2021-08-17 08:26:20 +02:00
|
|
|
|
2022-03-22 14:35:04 +01:00
|
|
|
if (previousState === VideoState.TO_TRANSCODE) {
|
|
|
|
Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(video)
|
|
|
|
}
|
2021-08-17 08:26:20 +02:00
|
|
|
}
|
|
|
|
}
|