diff options
author | Chocobozzz <me@florianbigard.com> | 2022-03-16 15:34:21 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2022-03-16 15:45:58 +0100 |
commit | f012319a644fe8d9d33f2f567fa828442a3b39fd (patch) | |
tree | 8fc7aeff10749ed8088e3f89745433b59bb62c20 /server/lib/job-queue/handlers | |
parent | 83664918901564830f3b7d1bd9879411a1b857a8 (diff) | |
download | PeerTube-f012319a644fe8d9d33f2f567fa828442a3b39fd.tar.gz PeerTube-f012319a644fe8d9d33f2f567fa828442a3b39fd.tar.zst PeerTube-f012319a644fe8d9d33f2f567fa828442a3b39fd.zip |
Process video torrents in order
Prevent update before video torrent generation for example
Diffstat (limited to 'server/lib/job-queue/handlers')
-rw-r--r-- | server/lib/job-queue/handlers/manage-video-torrent.ts | 88 |
1 files changed, 88 insertions, 0 deletions
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 @@ | |||
1 | import { Job } from 'bull' | ||
2 | import { createTorrentAndSetInfoHash, updateTorrentMetadata } from '@server/helpers/webtorrent' | ||
3 | import { VideoModel } from '@server/models/video/video' | ||
4 | import { VideoFileModel } from '@server/models/video/video-file' | ||
5 | import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' | ||
6 | import { ManageVideoTorrentPayload } from '@shared/models' | ||
7 | import { logger } from '../../../helpers/logger' | ||
8 | |||
9 | async function processManageVideoTorrent (job: Job) { | ||
10 | const payload = job.data as ManageVideoTorrentPayload | ||
11 | logger.info('Processing torrent in job %d.', job.id) | ||
12 | |||
13 | if (payload.action === 'create') return doCreateAction(payload) | ||
14 | if (payload.action === 'update-metadata') return doUpdateMetadataAction(payload) | ||
15 | } | ||
16 | |||
17 | // --------------------------------------------------------------------------- | ||
18 | |||
19 | export { | ||
20 | processManageVideoTorrent | ||
21 | } | ||
22 | |||
23 | // --------------------------------------------------------------------------- | ||
24 | |||
25 | async function doCreateAction (payload: ManageVideoTorrentPayload & { action: 'create' }) { | ||
26 | const [ video, file ] = await Promise.all([ | ||
27 | loadVideoOrLog(payload.videoId), | ||
28 | loadFileOrLog(payload.videoFileId) | ||
29 | ]) | ||
30 | |||
31 | await createTorrentAndSetInfoHash(video, file) | ||
32 | |||
33 | // Refresh videoFile because the createTorrentAndSetInfoHash could be long | ||
34 | const refreshedFile = await VideoFileModel.loadWithVideo(file.id) | ||
35 | // File does not exist anymore, remove the generated torrent | ||
36 | if (!refreshedFile) return file.removeTorrent() | ||
37 | |||
38 | refreshedFile.infoHash = file.infoHash | ||
39 | refreshedFile.torrentFilename = file.torrentFilename | ||
40 | |||
41 | return refreshedFile.save() | ||
42 | } | ||
43 | |||
44 | async function doUpdateMetadataAction (payload: ManageVideoTorrentPayload & { action: 'update-metadata' }) { | ||
45 | const [ video, streamingPlaylist, file ] = await Promise.all([ | ||
46 | loadVideoOrLog(payload.videoId), | ||
47 | loadStreamingPlaylistOrLog(payload.streamingPlaylistId), | ||
48 | loadFileOrLog(payload.videoFileId) | ||
49 | ]) | ||
50 | |||
51 | await updateTorrentMetadata(video || streamingPlaylist, file) | ||
52 | |||
53 | await file.save() | ||
54 | } | ||
55 | |||
56 | async function loadVideoOrLog (videoId: number) { | ||
57 | if (!videoId) return undefined | ||
58 | |||
59 | const video = await VideoModel.load(videoId) | ||
60 | if (!video) { | ||
61 | logger.debug('Do not process torrent for video %d: does not exist anymore.', videoId) | ||
62 | } | ||
63 | |||
64 | return video | ||
65 | } | ||
66 | |||
67 | async function loadStreamingPlaylistOrLog (streamingPlaylistId: number) { | ||
68 | if (!streamingPlaylistId) return undefined | ||
69 | |||
70 | const streamingPlaylist = await VideoStreamingPlaylistModel.loadWithVideo(streamingPlaylistId) | ||
71 | if (!streamingPlaylist) { | ||
72 | logger.debug('Do not process torrent for streaming playlist %d: does not exist anymore.', streamingPlaylistId) | ||
73 | } | ||
74 | |||
75 | return streamingPlaylist | ||
76 | } | ||
77 | |||
78 | async function loadFileOrLog (videoFileId: number) { | ||
79 | if (!videoFileId) return undefined | ||
80 | |||
81 | const file = await VideoFileModel.loadWithVideo(videoFileId) | ||
82 | |||
83 | if (!file) { | ||
84 | logger.debug('Do not process torrent for file %d: does not exist anymore.', videoFileId) | ||
85 | } | ||
86 | |||
87 | return file | ||
88 | } | ||