diff options
author | Chocobozzz <me@florianbigard.com> | 2023-07-31 14:34:36 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-08-11 15:02:33 +0200 |
commit | 3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch) | |
tree | e4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/lib/job-queue/handlers/move-to-object-storage.ts | |
parent | 04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff) | |
download | PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip |
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge
conflicts, but it's a major step forward:
* Server can be faster at startup because imports() are async and we can
easily lazy import big modules
* Angular doesn't seem to support ES import (with .js extension), so we
had to correctly organize peertube into a monorepo:
* Use yarn workspace feature
* Use typescript reference projects for dependencies
* Shared projects have been moved into "packages", each one is now a
node module (with a dedicated package.json/tsconfig.json)
* server/tools have been moved into apps/ and is now a dedicated app
bundled and published on NPM so users don't have to build peertube
cli tools manually
* server/tests have been moved into packages/ so we don't compile
them every time we want to run the server
* Use isolatedModule option:
* Had to move from const enum to const
(https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums)
* Had to explictely specify "type" imports when used in decorators
* Prefer tsx (that uses esbuild under the hood) instead of ts-node to
load typescript files (tests with mocha or scripts):
* To reduce test complexity as esbuild doesn't support decorator
metadata, we only test server files that do not import server
models
* We still build tests files into js files for a faster CI
* Remove unmaintained peertube CLI import script
* Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'server/lib/job-queue/handlers/move-to-object-storage.ts')
-rw-r--r-- | server/lib/job-queue/handlers/move-to-object-storage.ts | 159 |
1 files changed, 0 insertions, 159 deletions
diff --git a/server/lib/job-queue/handlers/move-to-object-storage.ts b/server/lib/job-queue/handlers/move-to-object-storage.ts deleted file mode 100644 index 9a99b6722..000000000 --- a/server/lib/job-queue/handlers/move-to-object-storage.ts +++ /dev/null | |||
@@ -1,159 +0,0 @@ | |||
1 | import { Job } from 'bullmq' | ||
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 { P2P_MEDIA_LOADER_PEER_VERSION } from '@server/initializers/constants' | ||
7 | import { storeHLSFileFromFilename, storeWebVideoFile } from '@server/lib/object-storage' | ||
8 | import { getHLSDirectory, getHlsResolutionPlaylistFilename } from '@server/lib/paths' | ||
9 | import { VideoPathManager } from '@server/lib/video-path-manager' | ||
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, VideoState, 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 %s.', payload.videoUUID, job.id) | ||
21 | |||
22 | const fileMutexReleaser = await VideoPathManager.Instance.lockFiles(payload.videoUUID) | ||
23 | |||
24 | const video = await VideoModel.loadWithFiles(payload.videoUUID) | ||
25 | // No video, maybe deleted? | ||
26 | if (!video) { | ||
27 | logger.info('Can\'t process job %d, video does not exist.', job.id, lTagsBase(payload.videoUUID)) | ||
28 | fileMutexReleaser() | ||
29 | return undefined | ||
30 | } | ||
31 | |||
32 | const lTags = lTagsBase(video.uuid, video.url) | ||
33 | |||
34 | try { | ||
35 | if (video.VideoFiles) { | ||
36 | logger.debug('Moving %d web video files for video %s.', video.VideoFiles.length, video.uuid, lTags) | ||
37 | |||
38 | await moveWebVideoFiles(video) | ||
39 | } | ||
40 | |||
41 | if (video.VideoStreamingPlaylists) { | ||
42 | logger.debug('Moving HLS playlist of %s.', video.uuid) | ||
43 | |||
44 | await moveHLSFiles(video) | ||
45 | } | ||
46 | |||
47 | const pendingMove = await VideoJobInfoModel.decrease(video.uuid, 'pendingMove') | ||
48 | if (pendingMove === 0) { | ||
49 | logger.info('Running cleanup after moving files to object storage (video %s in job %s)', video.uuid, job.id, lTags) | ||
50 | |||
51 | await doAfterLastJob({ video, previousVideoState: payload.previousVideoState, isNewVideo: payload.isNewVideo }) | ||
52 | } | ||
53 | } catch (err) { | ||
54 | await onMoveToObjectStorageFailure(job, err) | ||
55 | |||
56 | throw err | ||
57 | } finally { | ||
58 | fileMutexReleaser() | ||
59 | } | ||
60 | |||
61 | return payload.videoUUID | ||
62 | } | ||
63 | |||
64 | export async function onMoveToObjectStorageFailure (job: Job, err: any) { | ||
65 | const payload = job.data as MoveObjectStoragePayload | ||
66 | |||
67 | const video = await VideoModel.loadWithFiles(payload.videoUUID) | ||
68 | if (!video) return | ||
69 | |||
70 | logger.error('Cannot move video %s to object storage.', video.url, { err, ...lTagsBase(video.uuid, video.url) }) | ||
71 | |||
72 | await moveToFailedMoveToObjectStorageState(video) | ||
73 | await VideoJobInfoModel.abortAllTasks(video.uuid, 'pendingMove') | ||
74 | } | ||
75 | |||
76 | // --------------------------------------------------------------------------- | ||
77 | |||
78 | async function moveWebVideoFiles (video: MVideoWithAllFiles) { | ||
79 | for (const file of video.VideoFiles) { | ||
80 | if (file.storage !== VideoStorage.FILE_SYSTEM) continue | ||
81 | |||
82 | const fileUrl = await storeWebVideoFile(video, file) | ||
83 | |||
84 | const oldPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, file) | ||
85 | await onFileMoved({ videoOrPlaylist: video, file, fileUrl, oldPath }) | ||
86 | } | ||
87 | } | ||
88 | |||
89 | async function moveHLSFiles (video: MVideoWithAllFiles) { | ||
90 | for (const playlist of video.VideoStreamingPlaylists) { | ||
91 | const playlistWithVideo = playlist.withVideo(video) | ||
92 | |||
93 | for (const file of playlist.VideoFiles) { | ||
94 | if (file.storage !== VideoStorage.FILE_SYSTEM) continue | ||
95 | |||
96 | // Resolution playlist | ||
97 | const playlistFilename = getHlsResolutionPlaylistFilename(file.filename) | ||
98 | await storeHLSFileFromFilename(playlistWithVideo, playlistFilename) | ||
99 | |||
100 | // Resolution fragmented file | ||
101 | const fileUrl = await storeHLSFileFromFilename(playlistWithVideo, file.filename) | ||
102 | |||
103 | const oldPath = join(getHLSDirectory(video), file.filename) | ||
104 | |||
105 | await onFileMoved({ videoOrPlaylist: Object.assign(playlist, { Video: video }), file, fileUrl, oldPath }) | ||
106 | } | ||
107 | } | ||
108 | } | ||
109 | |||
110 | async function doAfterLastJob (options: { | ||
111 | video: MVideoWithAllFiles | ||
112 | previousVideoState: VideoState | ||
113 | isNewVideo: boolean | ||
114 | }) { | ||
115 | const { video, previousVideoState, isNewVideo } = options | ||
116 | |||
117 | for (const playlist of video.VideoStreamingPlaylists) { | ||
118 | if (playlist.storage === VideoStorage.OBJECT_STORAGE) continue | ||
119 | |||
120 | const playlistWithVideo = playlist.withVideo(video) | ||
121 | |||
122 | // Master playlist | ||
123 | playlist.playlistUrl = await storeHLSFileFromFilename(playlistWithVideo, playlist.playlistFilename) | ||
124 | // Sha256 segments file | ||
125 | playlist.segmentsSha256Url = await storeHLSFileFromFilename(playlistWithVideo, playlist.segmentsSha256Filename) | ||
126 | |||
127 | playlist.storage = VideoStorage.OBJECT_STORAGE | ||
128 | |||
129 | playlist.assignP2PMediaLoaderInfoHashes(video, playlist.VideoFiles) | ||
130 | playlist.p2pMediaLoaderPeerVersion = P2P_MEDIA_LOADER_PEER_VERSION | ||
131 | |||
132 | await playlist.save() | ||
133 | } | ||
134 | |||
135 | // Remove empty hls video directory | ||
136 | if (video.VideoStreamingPlaylists) { | ||
137 | await remove(getHLSDirectory(video)) | ||
138 | } | ||
139 | |||
140 | await moveToNextState({ video, previousVideoState, isNewVideo }) | ||
141 | } | ||
142 | |||
143 | async function onFileMoved (options: { | ||
144 | videoOrPlaylist: MVideo | MStreamingPlaylistVideo | ||
145 | file: MVideoFile | ||
146 | fileUrl: string | ||
147 | oldPath: string | ||
148 | }) { | ||
149 | const { videoOrPlaylist, file, fileUrl, oldPath } = options | ||
150 | |||
151 | file.fileUrl = fileUrl | ||
152 | file.storage = VideoStorage.OBJECT_STORAGE | ||
153 | |||
154 | await updateTorrentMetadata(videoOrPlaylist, file) | ||
155 | await file.save() | ||
156 | |||
157 | logger.debug('Removing %s because it\'s now on object storage', oldPath) | ||
158 | await remove(oldPath) | ||
159 | } | ||