From f012319a644fe8d9d33f2f567fa828442a3b39fd Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 16 Mar 2022 15:34:21 +0100 Subject: Process video torrents in order Prevent update before video torrent generation for example --- .../lib/job-queue/handlers/manage-video-torrent.ts | 88 ++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 server/lib/job-queue/handlers/manage-video-torrent.ts (limited to 'server/lib/job-queue/handlers') diff --git a/server/lib/job-queue/handlers/manage-video-torrent.ts b/server/lib/job-queue/handlers/manage-video-torrent.ts new file mode 100644 index 000000000..5cb4287e1 --- /dev/null +++ b/server/lib/job-queue/handlers/manage-video-torrent.ts @@ -0,0 +1,88 @@ +import { Job } from 'bull' +import { createTorrentAndSetInfoHash, updateTorrentMetadata } from '@server/helpers/webtorrent' +import { VideoModel } from '@server/models/video/video' +import { VideoFileModel } from '@server/models/video/video-file' +import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' +import { ManageVideoTorrentPayload } from '@shared/models' +import { logger } from '../../../helpers/logger' + +async function processManageVideoTorrent (job: Job) { + const payload = job.data as ManageVideoTorrentPayload + logger.info('Processing torrent in job %d.', job.id) + + if (payload.action === 'create') return doCreateAction(payload) + if (payload.action === 'update-metadata') return doUpdateMetadataAction(payload) +} + +// --------------------------------------------------------------------------- + +export { + processManageVideoTorrent +} + +// --------------------------------------------------------------------------- + +async function doCreateAction (payload: ManageVideoTorrentPayload & { action: 'create' }) { + const [ video, file ] = await Promise.all([ + loadVideoOrLog(payload.videoId), + loadFileOrLog(payload.videoFileId) + ]) + + await createTorrentAndSetInfoHash(video, file) + + // Refresh videoFile because the createTorrentAndSetInfoHash could be long + const refreshedFile = await VideoFileModel.loadWithVideo(file.id) + // File does not exist anymore, remove the generated torrent + if (!refreshedFile) return file.removeTorrent() + + refreshedFile.infoHash = file.infoHash + refreshedFile.torrentFilename = file.torrentFilename + + return refreshedFile.save() +} + +async function doUpdateMetadataAction (payload: ManageVideoTorrentPayload & { action: 'update-metadata' }) { + const [ video, streamingPlaylist, file ] = await Promise.all([ + loadVideoOrLog(payload.videoId), + loadStreamingPlaylistOrLog(payload.streamingPlaylistId), + loadFileOrLog(payload.videoFileId) + ]) + + await updateTorrentMetadata(video || streamingPlaylist, file) + + await file.save() +} + +async function loadVideoOrLog (videoId: number) { + if (!videoId) return undefined + + const video = await VideoModel.load(videoId) + if (!video) { + logger.debug('Do not process torrent for video %d: does not exist anymore.', videoId) + } + + return video +} + +async function loadStreamingPlaylistOrLog (streamingPlaylistId: number) { + if (!streamingPlaylistId) return undefined + + const streamingPlaylist = await VideoStreamingPlaylistModel.loadWithVideo(streamingPlaylistId) + if (!streamingPlaylist) { + logger.debug('Do not process torrent for streaming playlist %d: does not exist anymore.', streamingPlaylistId) + } + + return streamingPlaylist +} + +async function loadFileOrLog (videoFileId: number) { + if (!videoFileId) return undefined + + const file = await VideoFileModel.loadWithVideo(videoFileId) + + if (!file) { + logger.debug('Do not process torrent for file %d: does not exist anymore.', videoFileId) + } + + return file +} -- cgit v1.2.3