]>
Commit | Line | Data |
---|---|---|
5a921e7b | 1 | import { Job } from 'bullmq' |
0305db28 JB |
2 | import { remove } from 'fs-extra' |
3 | import { join } from 'path' | |
a2caee9f | 4 | import { logger, loggerTagsFactory } from '@server/helpers/logger' |
9b293cd6 | 5 | import { updateTorrentMetadata } from '@server/helpers/webtorrent' |
fb72d2e1 | 6 | import { P2P_MEDIA_LOADER_PEER_VERSION } from '@server/initializers/constants' |
cfd57d2c | 7 | import { storeHLSFileFromFilename, storeWebTorrentFile } from '@server/lib/object-storage' |
0305db28 | 8 | import { getHLSDirectory, getHlsResolutionPlaylistFilename } from '@server/lib/paths' |
3545e72c | 9 | import { VideoPathManager } from '@server/lib/video-path-manager' |
dbd9fb44 | 10 | import { moveToFailedMoveToObjectStorageState, moveToNextState } from '@server/lib/video-state' |
0305db28 JB |
11 | import { VideoModel } from '@server/models/video/video' |
12 | import { VideoJobInfoModel } from '@server/models/video/video-job-info' | |
13 | import { MStreamingPlaylistVideo, MVideo, MVideoFile, MVideoWithAllFiles } from '@server/types/models' | |
1808a1f8 | 14 | import { MoveObjectStoragePayload, VideoState, VideoStorage } from '@shared/models' |
0305db28 | 15 | |
a2caee9f C |
16 | const lTagsBase = loggerTagsFactory('move-object-storage') |
17 | ||
41fb13c3 | 18 | export async function processMoveToObjectStorage (job: Job) { |
0305db28 | 19 | const payload = job.data as MoveObjectStoragePayload |
bd911b54 | 20 | logger.info('Moving video %s in job %s.', payload.videoUUID, job.id) |
0305db28 | 21 | |
a687879e C |
22 | const fileMutexReleaser = await VideoPathManager.Instance.lockFiles(payload.videoUUID) |
23 | ||
0305db28 JB |
24 | const video = await VideoModel.loadWithFiles(payload.videoUUID) |
25 | // No video, maybe deleted? | |
26 | if (!video) { | |
a2caee9f | 27 | logger.info('Can\'t process job %d, video does not exist.', job.id, lTagsBase(payload.videoUUID)) |
a687879e | 28 | fileMutexReleaser() |
0305db28 JB |
29 | return undefined |
30 | } | |
31 | ||
a2caee9f C |
32 | const lTags = lTagsBase(video.uuid, video.url) |
33 | ||
dbd9fb44 C |
34 | try { |
35 | if (video.VideoFiles) { | |
a2caee9f C |
36 | logger.debug('Moving %d webtorrent files for video %s.', video.VideoFiles.length, video.uuid, lTags) |
37 | ||
dbd9fb44 C |
38 | await moveWebTorrentFiles(video) |
39 | } | |
0305db28 | 40 | |
dbd9fb44 | 41 | if (video.VideoStreamingPlaylists) { |
a2caee9f C |
42 | logger.debug('Moving HLS playlist of %s.', video.uuid) |
43 | ||
dbd9fb44 C |
44 | await moveHLSFiles(video) |
45 | } | |
46 | ||
47 | const pendingMove = await VideoJobInfoModel.decrease(video.uuid, 'pendingMove') | |
48 | if (pendingMove === 0) { | |
bd911b54 | 49 | logger.info('Running cleanup after moving files to object storage (video %s in job %s)', video.uuid, job.id, lTags) |
a2caee9f | 50 | |
1808a1f8 | 51 | await doAfterLastJob({ video, previousVideoState: payload.previousVideoState, isNewVideo: payload.isNewVideo }) |
dbd9fb44 C |
52 | } |
53 | } catch (err) { | |
32567717 | 54 | await onMoveToObjectStorageFailure(job, err) |
849f0fd3 C |
55 | |
56 | throw err | |
57 | } finally { | |
58 | fileMutexReleaser() | |
0305db28 JB |
59 | } |
60 | ||
61 | return payload.videoUUID | |
62 | } | |
63 | ||
32567717 C |
64 | export async function onMoveToObjectStorageFailure (job: Job, err: any) { |
65 | const payload = job.data as MoveObjectStoragePayload | |
66 | ||
67 | const video = await VideoModel.loadWithFiles(payload.videoUUID) | |
68 | if (!video) return | |
69 | ||
70 | logger.error('Cannot move video %s to object storage.', video.url, { err, ...lTagsBase(video.uuid, video.url) }) | |
71 | ||
72 | await moveToFailedMoveToObjectStorageState(video) | |
73 | await VideoJobInfoModel.abortAllTasks(video.uuid, 'pendingMove') | |
74 | } | |
75 | ||
0305db28 JB |
76 | // --------------------------------------------------------------------------- |
77 | ||
78 | async function moveWebTorrentFiles (video: MVideoWithAllFiles) { | |
79 | for (const file of video.VideoFiles) { | |
80 | if (file.storage !== VideoStorage.FILE_SYSTEM) continue | |
81 | ||
3545e72c | 82 | const fileUrl = await storeWebTorrentFile(video, file) |
0305db28 | 83 | |
3545e72c | 84 | const oldPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, file) |
0305db28 JB |
85 | await onFileMoved({ videoOrPlaylist: video, file, fileUrl, oldPath }) |
86 | } | |
87 | } | |
88 | ||
89 | async function moveHLSFiles (video: MVideoWithAllFiles) { | |
90 | for (const playlist of video.VideoStreamingPlaylists) { | |
ad5db104 | 91 | const playlistWithVideo = playlist.withVideo(video) |
0305db28 JB |
92 | |
93 | for (const file of playlist.VideoFiles) { | |
94 | if (file.storage !== VideoStorage.FILE_SYSTEM) continue | |
95 | ||
96 | // Resolution playlist | |
97 | const playlistFilename = getHlsResolutionPlaylistFilename(file.filename) | |
cfd57d2c | 98 | await storeHLSFileFromFilename(playlistWithVideo, playlistFilename) |
0305db28 JB |
99 | |
100 | // Resolution fragmented file | |
cfd57d2c | 101 | const fileUrl = await storeHLSFileFromFilename(playlistWithVideo, file.filename) |
0305db28 JB |
102 | |
103 | const oldPath = join(getHLSDirectory(video), file.filename) | |
104 | ||
105 | await onFileMoved({ videoOrPlaylist: Object.assign(playlist, { Video: video }), file, fileUrl, oldPath }) | |
106 | } | |
107 | } | |
108 | } | |
109 | ||
1808a1f8 C |
110 | async function doAfterLastJob (options: { |
111 | video: MVideoWithAllFiles | |
112 | previousVideoState: VideoState | |
113 | isNewVideo: boolean | |
114 | }) { | |
115 | const { video, previousVideoState, isNewVideo } = options | |
116 | ||
0305db28 JB |
117 | for (const playlist of video.VideoStreamingPlaylists) { |
118 | if (playlist.storage === VideoStorage.OBJECT_STORAGE) continue | |
119 | ||
ad5db104 C |
120 | const playlistWithVideo = playlist.withVideo(video) |
121 | ||
0305db28 | 122 | // Master playlist |
cfd57d2c | 123 | playlist.playlistUrl = await storeHLSFileFromFilename(playlistWithVideo, playlist.playlistFilename) |
0305db28 | 124 | // Sha256 segments file |
cfd57d2c | 125 | playlist.segmentsSha256Url = await storeHLSFileFromFilename(playlistWithVideo, playlist.segmentsSha256Filename) |
0305db28 JB |
126 | |
127 | playlist.storage = VideoStorage.OBJECT_STORAGE | |
128 | ||
fb72d2e1 C |
129 | playlist.assignP2PMediaLoaderInfoHashes(video, playlist.VideoFiles) |
130 | playlist.p2pMediaLoaderPeerVersion = P2P_MEDIA_LOADER_PEER_VERSION | |
131 | ||
0305db28 JB |
132 | await playlist.save() |
133 | } | |
134 | ||
135 | // Remove empty hls video directory | |
136 | if (video.VideoStreamingPlaylists) { | |
137 | await remove(getHLSDirectory(video)) | |
138 | } | |
139 | ||
1808a1f8 | 140 | await moveToNextState({ video, previousVideoState, isNewVideo }) |
0305db28 JB |
141 | } |
142 | ||
143 | async function onFileMoved (options: { | |
144 | videoOrPlaylist: MVideo | MStreamingPlaylistVideo | |
145 | file: MVideoFile | |
146 | fileUrl: string | |
147 | oldPath: string | |
148 | }) { | |
149 | const { videoOrPlaylist, file, fileUrl, oldPath } = options | |
150 | ||
151 | file.fileUrl = fileUrl | |
152 | file.storage = VideoStorage.OBJECT_STORAGE | |
153 | ||
9b293cd6 | 154 | await updateTorrentMetadata(videoOrPlaylist, file) |
0305db28 JB |
155 | await file.save() |
156 | ||
157 | logger.debug('Removing %s because it\'s now on object storage', oldPath) | |
158 | await remove(oldPath) | |
159 | } |