]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/job-queue/handlers/move-to-object-storage.ts
Remove old migration files
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / move-to-object-storage.ts
1 import { Job } from 'bull'
2 import { remove } from 'fs-extra'
3 import { join } from 'path'
4 import { logger, loggerTagsFactory } from '@server/helpers/logger'
5 import { updateTorrentMetadata } from '@server/helpers/webtorrent'
6 import { CONFIG } from '@server/initializers/config'
7 import { P2P_MEDIA_LOADER_PEER_VERSION } from '@server/initializers/constants'
8 import { storeHLSFile, storeWebTorrentFile } from '@server/lib/object-storage'
9 import { getHLSDirectory, getHlsResolutionPlaylistFilename } from '@server/lib/paths'
10 import { moveToFailedMoveToObjectStorageState, moveToNextState } from '@server/lib/video-state'
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'
14 import { MoveObjectStoragePayload, VideoStorage } from '@shared/models'
15
16 const lTagsBase = loggerTagsFactory('move-object-storage')
17
18 export async function processMoveToObjectStorage (job: Job) {
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) {
25 logger.info('Can\'t process job %d, video does not exist.', job.id, lTagsBase(payload.videoUUID))
26 return undefined
27 }
28
29 const lTags = lTagsBase(video.uuid, video.url)
30
31 try {
32 if (video.VideoFiles) {
33 logger.debug('Moving %d webtorrent files for video %s.', video.VideoFiles.length, video.uuid, lTags)
34
35 await moveWebTorrentFiles(video)
36 }
37
38 if (video.VideoStreamingPlaylists) {
39 logger.debug('Moving HLS playlist of %s.', video.uuid)
40
41 await moveHLSFiles(video)
42 }
43
44 const pendingMove = await VideoJobInfoModel.decrease(video.uuid, 'pendingMove')
45 if (pendingMove === 0) {
46 logger.info('Running cleanup after moving files to object storage (video %s in job %d)', video.uuid, job.id, lTags)
47
48 await doAfterLastJob(video, payload.isNewVideo)
49 }
50 } catch (err) {
51 logger.error('Cannot move video %s to object storage.', video.url, { err, ...lTags })
52
53 await moveToFailedMoveToObjectStorageState(video)
54 await VideoJobInfoModel.abortAllTasks(video.uuid, 'pendingMove')
55 }
56
57 return payload.videoUUID
58 }
59
60 // ---------------------------------------------------------------------------
61
62 async 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
73 async function moveHLSFiles (video: MVideoWithAllFiles) {
74 for (const playlist of video.VideoStreamingPlaylists) {
75 const playlistWithVideo = playlist.withVideo(video)
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)
82 await storeHLSFile(playlistWithVideo, playlistFilename)
83
84 // Resolution fragmented file
85 const fileUrl = await storeHLSFile(playlistWithVideo, file.filename)
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
94 async function doAfterLastJob (video: MVideoWithAllFiles, isNewVideo: boolean) {
95 for (const playlist of video.VideoStreamingPlaylists) {
96 if (playlist.storage === VideoStorage.OBJECT_STORAGE) continue
97
98 const playlistWithVideo = playlist.withVideo(video)
99
100 // Master playlist
101 playlist.playlistUrl = await storeHLSFile(playlistWithVideo, playlist.playlistFilename)
102 // Sha256 segments file
103 playlist.segmentsSha256Url = await storeHLSFile(playlistWithVideo, playlist.segmentsSha256Filename)
104
105 playlist.storage = VideoStorage.OBJECT_STORAGE
106
107 playlist.assignP2PMediaLoaderInfoHashes(video, playlist.VideoFiles)
108 playlist.p2pMediaLoaderPeerVersion = P2P_MEDIA_LOADER_PEER_VERSION
109
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
121 async 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
132 await updateTorrentMetadata(videoOrPlaylist, file)
133 await file.save()
134
135 logger.debug('Removing %s because it\'s now on object storage', oldPath)
136 await remove(oldPath)
137 }