]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import { Job } from 'bull' |
0305db28 JB |
2 | import { remove } from 'fs-extra' |
3 | import { join } from 'path' | |
4 | import { logger } from '@server/helpers/logger' | |
9b293cd6 | 5 | import { updateTorrentMetadata } from '@server/helpers/webtorrent' |
0305db28 | 6 | import { CONFIG } from '@server/initializers/config' |
fb72d2e1 | 7 | import { P2P_MEDIA_LOADER_PEER_VERSION } from '@server/initializers/constants' |
0305db28 JB |
8 | import { storeHLSFile, storeWebTorrentFile } from '@server/lib/object-storage' |
9 | import { getHLSDirectory, getHlsResolutionPlaylistFilename } from '@server/lib/paths' | |
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' | |
d17c7b4e | 14 | import { MoveObjectStoragePayload, VideoStorage } from '@shared/models' |
0305db28 | 15 | |
41fb13c3 | 16 | export async function processMoveToObjectStorage (job: Job) { |
0305db28 JB |
17 | const payload = job.data as MoveObjectStoragePayload |
18 | logger.info('Moving video %s in job %d.', payload.videoUUID, job.id) | |
19 | ||
20 | const video = await VideoModel.loadWithFiles(payload.videoUUID) | |
21 | // No video, maybe deleted? | |
22 | if (!video) { | |
23 | logger.info('Can\'t process job %d, video does not exist.', job.id) | |
24 | return undefined | |
25 | } | |
26 | ||
dbd9fb44 C |
27 | try { |
28 | if (video.VideoFiles) { | |
29 | await moveWebTorrentFiles(video) | |
30 | } | |
0305db28 | 31 | |
dbd9fb44 C |
32 | if (video.VideoStreamingPlaylists) { |
33 | await moveHLSFiles(video) | |
34 | } | |
35 | ||
36 | const pendingMove = await VideoJobInfoModel.decrease(video.uuid, 'pendingMove') | |
37 | if (pendingMove === 0) { | |
38 | logger.info('Running cleanup after moving files to object storage (video %s in job %d)', video.uuid, job.id) | |
39 | await doAfterLastJob(video, payload.isNewVideo) | |
40 | } | |
41 | } catch (err) { | |
42 | logger.error('Cannot move video %s to object storage.', video.url, { err }) | |
0305db28 | 43 | |
dbd9fb44 C |
44 | await moveToFailedMoveToObjectStorageState(video) |
45 | await VideoJobInfoModel.abortAllTasks(video.uuid, 'pendingMove') | |
0305db28 JB |
46 | } |
47 | ||
48 | return payload.videoUUID | |
49 | } | |
50 | ||
51 | // --------------------------------------------------------------------------- | |
52 | ||
53 | async function moveWebTorrentFiles (video: MVideoWithAllFiles) { | |
54 | for (const file of video.VideoFiles) { | |
55 | if (file.storage !== VideoStorage.FILE_SYSTEM) continue | |
56 | ||
57 | const fileUrl = await storeWebTorrentFile(file.filename) | |
58 | ||
59 | const oldPath = join(CONFIG.STORAGE.VIDEOS_DIR, file.filename) | |
60 | await onFileMoved({ videoOrPlaylist: video, file, fileUrl, oldPath }) | |
61 | } | |
62 | } | |
63 | ||
64 | async function moveHLSFiles (video: MVideoWithAllFiles) { | |
65 | for (const playlist of video.VideoStreamingPlaylists) { | |
ad5db104 | 66 | const playlistWithVideo = playlist.withVideo(video) |
0305db28 JB |
67 | |
68 | for (const file of playlist.VideoFiles) { | |
69 | if (file.storage !== VideoStorage.FILE_SYSTEM) continue | |
70 | ||
71 | // Resolution playlist | |
72 | const playlistFilename = getHlsResolutionPlaylistFilename(file.filename) | |
ad5db104 | 73 | await storeHLSFile(playlistWithVideo, playlistFilename) |
0305db28 JB |
74 | |
75 | // Resolution fragmented file | |
ad5db104 | 76 | const fileUrl = await storeHLSFile(playlistWithVideo, file.filename) |
0305db28 JB |
77 | |
78 | const oldPath = join(getHLSDirectory(video), file.filename) | |
79 | ||
80 | await onFileMoved({ videoOrPlaylist: Object.assign(playlist, { Video: video }), file, fileUrl, oldPath }) | |
81 | } | |
82 | } | |
83 | } | |
84 | ||
85 | async function doAfterLastJob (video: MVideoWithAllFiles, isNewVideo: boolean) { | |
86 | for (const playlist of video.VideoStreamingPlaylists) { | |
87 | if (playlist.storage === VideoStorage.OBJECT_STORAGE) continue | |
88 | ||
ad5db104 C |
89 | const playlistWithVideo = playlist.withVideo(video) |
90 | ||
0305db28 | 91 | // Master playlist |
ad5db104 | 92 | playlist.playlistUrl = await storeHLSFile(playlistWithVideo, playlist.playlistFilename) |
0305db28 | 93 | // Sha256 segments file |
ad5db104 | 94 | playlist.segmentsSha256Url = await storeHLSFile(playlistWithVideo, playlist.segmentsSha256Filename) |
0305db28 JB |
95 | |
96 | playlist.storage = VideoStorage.OBJECT_STORAGE | |
97 | ||
fb72d2e1 C |
98 | playlist.assignP2PMediaLoaderInfoHashes(video, playlist.VideoFiles) |
99 | playlist.p2pMediaLoaderPeerVersion = P2P_MEDIA_LOADER_PEER_VERSION | |
100 | ||
0305db28 JB |
101 | await playlist.save() |
102 | } | |
103 | ||
104 | // Remove empty hls video directory | |
105 | if (video.VideoStreamingPlaylists) { | |
106 | await remove(getHLSDirectory(video)) | |
107 | } | |
108 | ||
109 | await moveToNextState(video, isNewVideo) | |
110 | } | |
111 | ||
112 | async function onFileMoved (options: { | |
113 | videoOrPlaylist: MVideo | MStreamingPlaylistVideo | |
114 | file: MVideoFile | |
115 | fileUrl: string | |
116 | oldPath: string | |
117 | }) { | |
118 | const { videoOrPlaylist, file, fileUrl, oldPath } = options | |
119 | ||
120 | file.fileUrl = fileUrl | |
121 | file.storage = VideoStorage.OBJECT_STORAGE | |
122 | ||
9b293cd6 | 123 | await updateTorrentMetadata(videoOrPlaylist, file) |
0305db28 JB |
124 | await file.save() |
125 | ||
126 | logger.debug('Removing %s because it\'s now on object storage', oldPath) | |
127 | await remove(oldPath) | |
128 | } |