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