2021-08-17 01:26:20 -05: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'
|
|
|
|
import { MVideoFullLight, MVideoUUID } from '@server/types/models'
|
|
|
|
import { VideoState } from '@shared/models'
|
|
|
|
import { federateVideoIfNeeded } from './activitypub/videos'
|
|
|
|
import { Notifier } from './notifier'
|
|
|
|
import { addMoveToObjectStorageJob } from './video'
|
|
|
|
|
|
|
|
function buildNextVideoState (currentState?: VideoState) {
|
|
|
|
if (currentState === VideoState.PUBLISHED) {
|
|
|
|
throw new Error('Video is already in its final state')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
function moveToNextState (video: MVideoUUID, isNewVideo = true) {
|
|
|
|
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
|
|
|
|
|
|
|
|
// Already in its final state
|
|
|
|
if (videoDatabase.state === VideoState.PUBLISHED) {
|
|
|
|
return federateVideoIfNeeded(videoDatabase, false, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
const newState = buildNextVideoState(videoDatabase.state)
|
|
|
|
|
|
|
|
if (newState === VideoState.PUBLISHED) {
|
|
|
|
return moveToPublishedState(videoDatabase, isNewVideo, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newState === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) {
|
|
|
|
return moveToExternalStorageState(videoDatabase, isNewVideo, t)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-09 04:05:35 -06:00
|
|
|
async function moveToExternalStorageState (video: MVideoFullLight, isNewVideo: boolean, transaction: Transaction) {
|
|
|
|
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
|
|
|
|
|
|
|
|
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, isNewVideo)
|
|
|
|
|
|
|
|
return true
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot add move to object storage job', { err })
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 21:20:04 -06:00
|
|
|
function moveToFailedState (video: MVideoFullLight) {
|
|
|
|
return sequelizeTypescript.transaction(async t => {
|
|
|
|
await video.setNewState(VideoState.TRANSCODING_FAILED, false, t)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-17 01:26:20 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
buildNextVideoState,
|
2021-11-09 04:05:35 -06:00
|
|
|
moveToExternalStorageState,
|
2021-11-07 21:20:04 -06:00
|
|
|
moveToFailedState,
|
2021-08-17 01:26:20 -05:00
|
|
|
moveToNextState
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function moveToPublishedState (video: MVideoFullLight, isNewVideo: boolean, transaction: Transaction) {
|
|
|
|
logger.info('Publishing video %s.', video.uuid, { tags: [ video.uuid ] })
|
|
|
|
|
|
|
|
const previousState = video.state
|
2021-10-08 08:34:07 -05:00
|
|
|
await video.setNewState(VideoState.PUBLISHED, isNewVideo, transaction)
|
2021-08-17 01:26:20 -05:00
|
|
|
|
|
|
|
// If the video was not published, we consider it is a new one for other instances
|
|
|
|
// Live videos are always federated, so it's not a new video
|
|
|
|
await federateVideoIfNeeded(video, isNewVideo, transaction)
|
|
|
|
|
2021-10-08 08:34:07 -05:00
|
|
|
if (isNewVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
|
2021-08-17 01:26:20 -05:00
|
|
|
|
|
|
|
if (previousState === VideoState.TO_TRANSCODE) {
|
|
|
|
Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(video)
|
|
|
|
}
|
|
|
|
}
|