]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/handlers/move-to-object-storage.ts
Add ability to run transcoding jobs
[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'
4import { logger } from '@server/helpers/logger'
1f6125be 5import { updateTorrentUrls } 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'
10import { moveToNextState } from '@server/lib/video-state'
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'
14import { MoveObjectStoragePayload, VideoStorage } from '../../../../shared'
15
41fb13c3 16export 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
27 if (video.VideoFiles) {
28 await moveWebTorrentFiles(video)
29 }
30
31 if (video.VideoStreamingPlaylists) {
32 await moveHLSFiles(video)
33 }
34
35 const pendingMove = await VideoJobInfoModel.decrease(video.uuid, 'pendingMove')
36 if (pendingMove === 0) {
37 logger.info('Running cleanup after moving files to object storage (video %s in job %d)', video.uuid, job.id)
38 await doAfterLastJob(video, payload.isNewVideo)
39 }
40
41 return payload.videoUUID
42}
43
44// ---------------------------------------------------------------------------
45
46async function moveWebTorrentFiles (video: MVideoWithAllFiles) {
47 for (const file of video.VideoFiles) {
48 if (file.storage !== VideoStorage.FILE_SYSTEM) continue
49
50 const fileUrl = await storeWebTorrentFile(file.filename)
51
52 const oldPath = join(CONFIG.STORAGE.VIDEOS_DIR, file.filename)
53 await onFileMoved({ videoOrPlaylist: video, file, fileUrl, oldPath })
54 }
55}
56
57async function moveHLSFiles (video: MVideoWithAllFiles) {
58 for (const playlist of video.VideoStreamingPlaylists) {
ad5db104 59 const playlistWithVideo = playlist.withVideo(video)
0305db28
JB
60
61 for (const file of playlist.VideoFiles) {
62 if (file.storage !== VideoStorage.FILE_SYSTEM) continue
63
64 // Resolution playlist
65 const playlistFilename = getHlsResolutionPlaylistFilename(file.filename)
ad5db104 66 await storeHLSFile(playlistWithVideo, playlistFilename)
0305db28
JB
67
68 // Resolution fragmented file
ad5db104 69 const fileUrl = await storeHLSFile(playlistWithVideo, file.filename)
0305db28
JB
70
71 const oldPath = join(getHLSDirectory(video), file.filename)
72
73 await onFileMoved({ videoOrPlaylist: Object.assign(playlist, { Video: video }), file, fileUrl, oldPath })
74 }
75 }
76}
77
78async function doAfterLastJob (video: MVideoWithAllFiles, isNewVideo: boolean) {
79 for (const playlist of video.VideoStreamingPlaylists) {
80 if (playlist.storage === VideoStorage.OBJECT_STORAGE) continue
81
ad5db104
C
82 const playlistWithVideo = playlist.withVideo(video)
83
0305db28 84 // Master playlist
ad5db104 85 playlist.playlistUrl = await storeHLSFile(playlistWithVideo, playlist.playlistFilename)
0305db28 86 // Sha256 segments file
ad5db104 87 playlist.segmentsSha256Url = await storeHLSFile(playlistWithVideo, playlist.segmentsSha256Filename)
0305db28
JB
88
89 playlist.storage = VideoStorage.OBJECT_STORAGE
90
fb72d2e1
C
91 playlist.assignP2PMediaLoaderInfoHashes(video, playlist.VideoFiles)
92 playlist.p2pMediaLoaderPeerVersion = P2P_MEDIA_LOADER_PEER_VERSION
93
0305db28
JB
94 await playlist.save()
95 }
96
97 // Remove empty hls video directory
98 if (video.VideoStreamingPlaylists) {
99 await remove(getHLSDirectory(video))
100 }
101
102 await moveToNextState(video, isNewVideo)
103}
104
105async function onFileMoved (options: {
106 videoOrPlaylist: MVideo | MStreamingPlaylistVideo
107 file: MVideoFile
108 fileUrl: string
109 oldPath: string
110}) {
111 const { videoOrPlaylist, file, fileUrl, oldPath } = options
112
113 file.fileUrl = fileUrl
114 file.storage = VideoStorage.OBJECT_STORAGE
115
1f6125be 116 await updateTorrentUrls(videoOrPlaylist, file)
0305db28
JB
117 await file.save()
118
119 logger.debug('Removing %s because it\'s now on object storage', oldPath)
120 await remove(oldPath)
121}