2021-08-17 02:26:20 -04:00
|
|
|
import { Transaction } from 'sequelize'
|
2023-07-31 08:34:36 -04:00
|
|
|
import { VideoState, VideoStateType } from '@peertube/peertube-models'
|
|
|
|
import { retryTransactionWrapper } from '@server/helpers/database-utils.js'
|
|
|
|
import { logger } from '@server/helpers/logger.js'
|
|
|
|
import { CONFIG } from '@server/initializers/config.js'
|
|
|
|
import { sequelizeTypescript } from '@server/initializers/database.js'
|
|
|
|
import { VideoJobInfoModel } from '@server/models/video/video-job-info.js'
|
|
|
|
import { VideoModel } from '@server/models/video/video.js'
|
|
|
|
import { MVideo, MVideoFullLight, MVideoUUID } from '@server/types/models/index.js'
|
|
|
|
import { federateVideoIfNeeded } from './activitypub/videos/index.js'
|
|
|
|
import { JobQueue } from './job-queue/index.js'
|
|
|
|
import { Notifier } from './notifier/index.js'
|
2024-02-12 04:47:52 -05:00
|
|
|
import { buildMoveJob } from './video-jobs.js'
|
2023-07-31 08:34:36 -04:00
|
|
|
|
|
|
|
function buildNextVideoState (currentState?: VideoStateType) {
|
2021-08-17 02:26:20 -04:00
|
|
|
if (currentState === VideoState.PUBLISHED) {
|
|
|
|
throw new Error('Video is already in its final state')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2022-03-22 09:35:04 -04:00
|
|
|
currentState !== VideoState.TO_EDIT &&
|
2021-08-17 02:26:20 -04:00
|
|
|
currentState !== VideoState.TO_TRANSCODE &&
|
|
|
|
currentState !== VideoState.TO_MOVE_TO_EXTERNAL_STORAGE &&
|
2023-10-31 07:15:40 -04:00
|
|
|
currentState !== VideoState.TO_MOVE_TO_FILE_SYSTEM &&
|
2021-08-17 02:26:20 -04:00
|
|
|
CONFIG.TRANSCODING.ENABLED
|
|
|
|
) {
|
|
|
|
return VideoState.TO_TRANSCODE
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
currentState !== VideoState.TO_MOVE_TO_EXTERNAL_STORAGE &&
|
2023-10-31 07:15:40 -04:00
|
|
|
currentState !== VideoState.TO_MOVE_TO_FILE_SYSTEM &&
|
2021-08-17 02:26:20 -04:00
|
|
|
CONFIG.OBJECT_STORAGE.ENABLED
|
|
|
|
) {
|
|
|
|
return VideoState.TO_MOVE_TO_EXTERNAL_STORAGE
|
|
|
|
}
|
|
|
|
|
|
|
|
return VideoState.PUBLISHED
|
|
|
|
}
|
|
|
|
|
2022-03-22 09:35:04 -04:00
|
|
|
function moveToNextState (options: {
|
|
|
|
video: MVideoUUID
|
2023-07-31 08:34:36 -04:00
|
|
|
previousVideoState?: VideoStateType
|
2022-03-22 09:35:04 -04:00
|
|
|
isNewVideo?: boolean // Default true
|
|
|
|
}) {
|
|
|
|
const { video, previousVideoState, isNewVideo = true } = options
|
|
|
|
|
2022-07-12 10:25:21 -04: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 02:26:20 -04:00
|
|
|
|
2022-07-12 10:25:21 -04:00
|
|
|
// Already in its final state
|
|
|
|
if (videoDatabase.state === VideoState.PUBLISHED) {
|
|
|
|
return federateVideoIfNeeded(videoDatabase, false, t)
|
|
|
|
}
|
2021-08-17 02:26:20 -04:00
|
|
|
|
2022-07-12 10:25:21 -04:00
|
|
|
const newState = buildNextVideoState(videoDatabase.state)
|
2021-08-17 02:26:20 -04:00
|
|
|
|
2022-07-12 10:25:21 -04:00
|
|
|
if (newState === VideoState.PUBLISHED) {
|
|
|
|
return moveToPublishedState({ video: videoDatabase, previousVideoState, isNewVideo, transaction: t })
|
|
|
|
}
|
2021-08-17 02:26:20 -04:00
|
|
|
|
2022-07-12 10:25:21 -04:00
|
|
|
if (newState === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) {
|
|
|
|
return moveToExternalStorageState({ video: videoDatabase, isNewVideo, transaction: t })
|
|
|
|
}
|
|
|
|
})
|
2021-08-17 02:26:20 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-10-31 07:15:40 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2022-03-22 09:35:04 -04:00
|
|
|
async function moveToExternalStorageState (options: {
|
|
|
|
video: MVideoFullLight
|
|
|
|
isNewVideo: boolean
|
|
|
|
transaction: Transaction
|
|
|
|
}) {
|
|
|
|
const { video, isNewVideo, transaction } = options
|
|
|
|
|
2021-11-09 05:05:35 -05: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 09:35:04 -04:00
|
|
|
const previousVideoState = video.state
|
2022-09-28 04:03:06 -04:00
|
|
|
|
|
|
|
if (video.state !== VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) {
|
|
|
|
await video.setNewState(VideoState.TO_MOVE_TO_EXTERNAL_STORAGE, isNewVideo, transaction)
|
|
|
|
}
|
2021-11-09 05:05:35 -05:00
|
|
|
|
|
|
|
logger.info('Creating external storage move job for video %s.', video.uuid, { tags: [ video.uuid ] })
|
|
|
|
|
|
|
|
try {
|
2023-10-31 07:15:40 -04:00
|
|
|
await JobQueue.Instance.createJob(await buildMoveJob({ video, previousVideoState, isNewVideo, type: 'move-to-object-storage' }))
|
2021-11-09 05:05:35 -05:00
|
|
|
|
|
|
|
return true
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot add move to object storage job', { err })
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-31 07:15:40 -04:00
|
|
|
async function moveToFileSystemState (options: {
|
|
|
|
video: MVideoFullLight
|
|
|
|
isNewVideo: boolean
|
|
|
|
transaction: Transaction
|
|
|
|
}) {
|
|
|
|
const { video, isNewVideo, transaction } = options
|
|
|
|
|
|
|
|
const previousVideoState = video.state
|
|
|
|
|
|
|
|
if (video.state !== VideoState.TO_MOVE_TO_FILE_SYSTEM) {
|
|
|
|
await video.setNewState(VideoState.TO_MOVE_TO_FILE_SYSTEM, false, transaction)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info('Creating move to file system job for video %s.', video.uuid, { tags: [ video.uuid ] })
|
|
|
|
|
|
|
|
try {
|
|
|
|
await JobQueue.Instance.createJob(await buildMoveJob({ video, previousVideoState, isNewVideo, type: 'move-to-file-system' }))
|
|
|
|
|
|
|
|
return true
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot add move to file system job', { err })
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2021-12-23 05:09:31 -05:00
|
|
|
function moveToFailedTranscodingState (video: MVideo) {
|
2021-11-18 08:35:08 -05:00
|
|
|
if (video.state === VideoState.TRANSCODING_FAILED) return
|
|
|
|
|
2021-11-09 05:52:41 -05:00
|
|
|
return video.setNewState(VideoState.TRANSCODING_FAILED, false, undefined)
|
2021-11-07 22:20:04 -05:00
|
|
|
}
|
|
|
|
|
2021-12-23 05:09:31 -05: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)
|
|
|
|
}
|
|
|
|
|
2023-10-31 07:15:40 -04:00
|
|
|
function moveToFailedMoveToFileSystemState (video: MVideo) {
|
|
|
|
if (video.state === VideoState.TO_MOVE_TO_FILE_SYSTEM_FAILED) return
|
|
|
|
|
|
|
|
return video.setNewState(VideoState.TO_MOVE_TO_FILE_SYSTEM_FAILED, false, undefined)
|
|
|
|
}
|
|
|
|
|
2021-08-17 02:26:20 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
buildNextVideoState,
|
2023-10-31 07:15:40 -04:00
|
|
|
moveToFailedMoveToFileSystemState,
|
2021-11-09 05:05:35 -05:00
|
|
|
moveToExternalStorageState,
|
2023-10-31 07:15:40 -04:00
|
|
|
moveToFileSystemState,
|
2021-11-09 05:52:41 -05:00
|
|
|
moveToFailedTranscodingState,
|
2021-12-23 05:09:31 -05:00
|
|
|
moveToFailedMoveToObjectStorageState,
|
2021-08-17 02:26:20 -04:00
|
|
|
moveToNextState
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2022-03-22 09:35:04 -04:00
|
|
|
async function moveToPublishedState (options: {
|
|
|
|
video: MVideoFullLight
|
|
|
|
isNewVideo: boolean
|
|
|
|
transaction: Transaction
|
2023-07-31 08:34:36 -04:00
|
|
|
previousVideoState?: VideoStateType
|
2022-03-22 09:35:04 -04:00
|
|
|
}) {
|
|
|
|
const { video, isNewVideo, transaction, previousVideoState } = options
|
|
|
|
const previousState = previousVideoState ?? video.state
|
|
|
|
|
2022-05-03 05:38:07 -04:00
|
|
|
logger.info('Publishing video %s.', video.uuid, { isNewVideo, previousState, tags: [ video.uuid ] })
|
2021-08-17 02:26:20 -04:00
|
|
|
|
2021-10-08 09:34:07 -04:00
|
|
|
await video.setNewState(VideoState.PUBLISHED, isNewVideo, transaction)
|
2021-08-17 02:26:20 -04:00
|
|
|
|
|
|
|
await federateVideoIfNeeded(video, isNewVideo, transaction)
|
|
|
|
|
2022-03-22 09:35:04 -04:00
|
|
|
if (previousState === VideoState.TO_EDIT) {
|
2022-03-22 11:58:49 -04:00
|
|
|
Notifier.Instance.notifyOfFinishedVideoStudioEdition(video)
|
2022-03-22 09:35:04 -04:00
|
|
|
return
|
|
|
|
}
|
2021-12-01 06:33:46 -05:00
|
|
|
|
2022-03-22 09:35:04 -04:00
|
|
|
if (isNewVideo) {
|
2024-02-15 07:55:12 -05:00
|
|
|
Notifier.Instance.notifyOnNewVideoOrLiveIfNeeded(video)
|
2021-08-17 02:26:20 -04:00
|
|
|
|
2022-03-22 09:35:04 -04:00
|
|
|
if (previousState === VideoState.TO_TRANSCODE) {
|
|
|
|
Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(video)
|
|
|
|
}
|
2021-08-17 02:26:20 -04:00
|
|
|
}
|
|
|
|
}
|