X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fjob-queue%2Fhandlers%2Fmanage-video-torrent.ts;h=cef93afda011cfd932df166112272a20817b7970;hb=a32bf8cd20212b903d3fa478e629f051eb77fecc;hp=4505ca79ebb085f9814a5aa21e6255f877a0c95c;hpb=5a921e7b74910414626bfc9672b857e987e3ebed;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/job-queue/handlers/manage-video-torrent.ts b/server/lib/job-queue/handlers/manage-video-torrent.ts index 4505ca79e..cef93afda 100644 --- a/server/lib/job-queue/handlers/manage-video-torrent.ts +++ b/server/lib/job-queue/handlers/manage-video-torrent.ts @@ -1,5 +1,7 @@ import { Job } from 'bullmq' +import { extractVideo } from '@server/helpers/video' import { createTorrentAndSetInfoHash, updateTorrentMetadata } from '@server/helpers/webtorrent' +import { VideoPathManager } from '@server/lib/video-path-manager' import { VideoModel } from '@server/models/video/video' import { VideoFileModel } from '@server/models/video/video-file' import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' @@ -8,7 +10,7 @@ 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) + logger.info('Processing torrent in job %s.', job.id) if (payload.action === 'create') return doCreateAction(payload) if (payload.action === 'update-metadata') return doUpdateMetadataAction(payload) @@ -30,17 +32,23 @@ async function doCreateAction (payload: ManageVideoTorrentPayload & { action: 'c if (!video || !file) return - await createTorrentAndSetInfoHash(video, file) + const fileMutexReleaser = await VideoPathManager.Instance.lockFiles(video.uuid) - // 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() + try { + await createTorrentAndSetInfoHash(video, file) - refreshedFile.infoHash = file.infoHash - refreshedFile.torrentFilename = file.torrentFilename + // 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() - return refreshedFile.save() + refreshedFile.infoHash = file.infoHash + refreshedFile.torrentFilename = file.torrentFilename + + await refreshedFile.save() + } finally { + fileMutexReleaser() + } } async function doUpdateMetadataAction (payload: ManageVideoTorrentPayload & { action: 'update-metadata' }) { @@ -52,9 +60,16 @@ async function doUpdateMetadataAction (payload: ManageVideoTorrentPayload & { ac if ((!video && !streamingPlaylist) || !file) return - await updateTorrentMetadata(video || streamingPlaylist, file) + const extractedVideo = extractVideo(video || streamingPlaylist) + const fileMutexReleaser = await VideoPathManager.Instance.lockFiles(extractedVideo.uuid) - await file.save() + try { + await updateTorrentMetadata(video || streamingPlaylist, file) + + await file.save() + } finally { + fileMutexReleaser() + } } async function loadVideoOrLog (videoId: number) { @@ -82,7 +97,7 @@ async function loadStreamingPlaylistOrLog (streamingPlaylistId: number) { async function loadFileOrLog (videoFileId: number) { if (!videoFileId) return undefined - const file = await VideoFileModel.loadWithVideo(videoFileId) + const file = await VideoFileModel.load(videoFileId) if (!file) { logger.debug('Do not process torrent for file %d: does not exist anymore.', videoFileId)