]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/handlers/move-to-object-storage.ts
Fix import timeout inconsistency
[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'
d17c7b4e 14import { MoveObjectStoragePayload, 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
dbd9fb44
C
48 await doAfterLastJob(video, payload.isNewVideo)
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
94async function doAfterLastJob (video: MVideoWithAllFiles, isNewVideo: boolean) {
95 for (const playlist of video.VideoStreamingPlaylists) {
96 if (playlist.storage === VideoStorage.OBJECT_STORAGE) continue
97
ad5db104
C
98 const playlistWithVideo = playlist.withVideo(video)
99
0305db28 100 // Master playlist
ad5db104 101 playlist.playlistUrl = await storeHLSFile(playlistWithVideo, playlist.playlistFilename)
0305db28 102 // Sha256 segments file
ad5db104 103 playlist.segmentsSha256Url = await storeHLSFile(playlistWithVideo, playlist.segmentsSha256Filename)
0305db28
JB
104
105 playlist.storage = VideoStorage.OBJECT_STORAGE
106
fb72d2e1
C
107 playlist.assignP2PMediaLoaderInfoHashes(video, playlist.VideoFiles)
108 playlist.p2pMediaLoaderPeerVersion = P2P_MEDIA_LOADER_PEER_VERSION
109
0305db28
JB
110 await playlist.save()
111 }
112
113 // Remove empty hls video directory
114 if (video.VideoStreamingPlaylists) {
115 await remove(getHLSDirectory(video))
116 }
117
118 await moveToNextState(video, isNewVideo)
119}
120
121async function onFileMoved (options: {
122 videoOrPlaylist: MVideo | MStreamingPlaylistVideo
123 file: MVideoFile
124 fileUrl: string
125 oldPath: string
126}) {
127 const { videoOrPlaylist, file, fileUrl, oldPath } = options
128
129 file.fileUrl = fileUrl
130 file.storage = VideoStorage.OBJECT_STORAGE
131
9b293cd6 132 await updateTorrentMetadata(videoOrPlaylist, file)
0305db28
JB
133 await file.save()
134
135 logger.debug('Removing %s because it\'s now on object storage', oldPath)
136 await remove(oldPath)
137}