]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/handlers/move-to-object-storage.ts
Fix videos history tests
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / move-to-object-storage.ts
CommitLineData
41fb13c3 1import { Job } from 'bull'
0305db28
JB
2import { remove } from 'fs-extra'
3import { join } from 'path'
a2caee9f 4import { logger, loggerTagsFactory } from '@server/helpers/logger'
9b293cd6 5import { updateTorrentMetadata } from '@server/helpers/webtorrent'
0305db28 6import { CONFIG } from '@server/initializers/config'
fb72d2e1 7import { P2P_MEDIA_LOADER_PEER_VERSION } from '@server/initializers/constants'
0305db28
JB
8import { storeHLSFile, storeWebTorrentFile } from '@server/lib/object-storage'
9import { getHLSDirectory, getHlsResolutionPlaylistFilename } from '@server/lib/paths'
dbd9fb44 10import { moveToFailedMoveToObjectStorageState, moveToNextState } from '@server/lib/video-state'
0305db28
JB
11import { VideoModel } from '@server/models/video/video'
12import { VideoJobInfoModel } from '@server/models/video/video-job-info'
13import { MStreamingPlaylistVideo, MVideo, MVideoFile, MVideoWithAllFiles } from '@server/types/models'
1808a1f8 14import { MoveObjectStoragePayload, VideoState, VideoStorage } from '@shared/models'
0305db28 15
a2caee9f
C
16const lTagsBase = loggerTagsFactory('move-object-storage')
17
41fb13c3 18export async function processMoveToObjectStorage (job: Job) {
0305db28
JB
19 const payload = job.data as MoveObjectStoragePayload
20 logger.info('Moving video %s in job %d.', payload.videoUUID, job.id)
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
dbd9fb44
C
31 try {
32 if (video.VideoFiles) {
a2caee9f
C
33 logger.debug('Moving %d webtorrent files for video %s.', video.VideoFiles.length, video.uuid, lTags)
34
dbd9fb44
C
35 await moveWebTorrentFiles(video)
36 }
0305db28 37
dbd9fb44 38 if (video.VideoStreamingPlaylists) {
a2caee9f
C
39 logger.debug('Moving HLS playlist of %s.', video.uuid)
40
dbd9fb44
C
41 await moveHLSFiles(video)
42 }
43
44 const pendingMove = await VideoJobInfoModel.decrease(video.uuid, 'pendingMove')
45 if (pendingMove === 0) {
a2caee9f
C
46 logger.info('Running cleanup after moving files to object storage (video %s in job %d)', video.uuid, job.id, lTags)
47
1808a1f8 48 await doAfterLastJob({ video, previousVideoState: payload.previousVideoState, isNewVideo: payload.isNewVideo })
dbd9fb44
C
49 }
50 } catch (err) {
a2caee9f 51 logger.error('Cannot move video %s to object storage.', video.url, { err, ...lTags })
0305db28 52
dbd9fb44
C
53 await moveToFailedMoveToObjectStorageState(video)
54 await VideoJobInfoModel.abortAllTasks(video.uuid, 'pendingMove')
0305db28
JB
55 }
56
57 return payload.videoUUID
58}
59
60// ---------------------------------------------------------------------------
61
62async function moveWebTorrentFiles (video: MVideoWithAllFiles) {
63 for (const file of video.VideoFiles) {
64 if (file.storage !== VideoStorage.FILE_SYSTEM) continue
65
66 const fileUrl = await storeWebTorrentFile(file.filename)
67
68 const oldPath = join(CONFIG.STORAGE.VIDEOS_DIR, file.filename)
69 await onFileMoved({ videoOrPlaylist: video, file, fileUrl, oldPath })
70 }
71}
72
73async function moveHLSFiles (video: MVideoWithAllFiles) {
74 for (const playlist of video.VideoStreamingPlaylists) {
ad5db104 75 const playlistWithVideo = playlist.withVideo(video)
0305db28
JB
76
77 for (const file of playlist.VideoFiles) {
78 if (file.storage !== VideoStorage.FILE_SYSTEM) continue
79
80 // Resolution playlist
81 const playlistFilename = getHlsResolutionPlaylistFilename(file.filename)
ad5db104 82 await storeHLSFile(playlistWithVideo, playlistFilename)
0305db28
JB
83
84 // Resolution fragmented file
ad5db104 85 const fileUrl = await storeHLSFile(playlistWithVideo, file.filename)
0305db28
JB
86
87 const oldPath = join(getHLSDirectory(video), file.filename)
88
89 await onFileMoved({ videoOrPlaylist: Object.assign(playlist, { Video: video }), file, fileUrl, oldPath })
90 }
91 }
92}
93
1808a1f8
C
94async function doAfterLastJob (options: {
95 video: MVideoWithAllFiles
96 previousVideoState: VideoState
97 isNewVideo: boolean
98}) {
99 const { video, previousVideoState, isNewVideo } = options
100
0305db28
JB
101 for (const playlist of video.VideoStreamingPlaylists) {
102 if (playlist.storage === VideoStorage.OBJECT_STORAGE) continue
103
ad5db104
C
104 const playlistWithVideo = playlist.withVideo(video)
105
0305db28 106 // Master playlist
ad5db104 107 playlist.playlistUrl = await storeHLSFile(playlistWithVideo, playlist.playlistFilename)
0305db28 108 // Sha256 segments file
ad5db104 109 playlist.segmentsSha256Url = await storeHLSFile(playlistWithVideo, playlist.segmentsSha256Filename)
0305db28
JB
110
111 playlist.storage = VideoStorage.OBJECT_STORAGE
112
fb72d2e1
C
113 playlist.assignP2PMediaLoaderInfoHashes(video, playlist.VideoFiles)
114 playlist.p2pMediaLoaderPeerVersion = P2P_MEDIA_LOADER_PEER_VERSION
115
0305db28
JB
116 await playlist.save()
117 }
118
119 // Remove empty hls video directory
120 if (video.VideoStreamingPlaylists) {
121 await remove(getHLSDirectory(video))
122 }
123
1808a1f8 124 await moveToNextState({ video, previousVideoState, isNewVideo })
0305db28
JB
125}
126
127async function onFileMoved (options: {
128 videoOrPlaylist: MVideo | MStreamingPlaylistVideo
129 file: MVideoFile
130 fileUrl: string
131 oldPath: string
132}) {
133 const { videoOrPlaylist, file, fileUrl, oldPath } = options
134
135 file.fileUrl = fileUrl
136 file.storage = VideoStorage.OBJECT_STORAGE
137
9b293cd6 138 await updateTorrentMetadata(videoOrPlaylist, file)
0305db28
JB
139 await file.save()
140
141 logger.debug('Removing %s because it\'s now on object storage', oldPath)
142 await remove(oldPath)
143}