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/video-state.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/video-state.ts')
-rw-r--r-- | server/lib/video-state.ts | 154 |
1 files changed, 0 insertions, 154 deletions
diff --git a/server/lib/video-state.ts b/server/lib/video-state.ts deleted file mode 100644 index 893725d85..000000000 --- a/server/lib/video-state.ts +++ /dev/null | |||
@@ -1,154 +0,0 @@ | |||
1 | import { Transaction } from 'sequelize' | ||
2 | import { retryTransactionWrapper } from '@server/helpers/database-utils' | ||
3 | import { logger } from '@server/helpers/logger' | ||
4 | import { CONFIG } from '@server/initializers/config' | ||
5 | import { sequelizeTypescript } from '@server/initializers/database' | ||
6 | import { VideoModel } from '@server/models/video/video' | ||
7 | import { VideoJobInfoModel } from '@server/models/video/video-job-info' | ||
8 | import { MVideo, MVideoFullLight, MVideoUUID } from '@server/types/models' | ||
9 | import { VideoState } from '@shared/models' | ||
10 | import { federateVideoIfNeeded } from './activitypub/videos' | ||
11 | import { JobQueue } from './job-queue' | ||
12 | import { Notifier } from './notifier' | ||
13 | import { buildMoveToObjectStorageJob } from './video' | ||
14 | |||
15 | function buildNextVideoState (currentState?: VideoState) { | ||
16 | if (currentState === VideoState.PUBLISHED) { | ||
17 | throw new Error('Video is already in its final state') | ||
18 | } | ||
19 | |||
20 | if ( | ||
21 | currentState !== VideoState.TO_EDIT && | ||
22 | currentState !== VideoState.TO_TRANSCODE && | ||
23 | currentState !== VideoState.TO_MOVE_TO_EXTERNAL_STORAGE && | ||
24 | CONFIG.TRANSCODING.ENABLED | ||
25 | ) { | ||
26 | return VideoState.TO_TRANSCODE | ||
27 | } | ||
28 | |||
29 | if ( | ||
30 | currentState !== VideoState.TO_MOVE_TO_EXTERNAL_STORAGE && | ||
31 | CONFIG.OBJECT_STORAGE.ENABLED | ||
32 | ) { | ||
33 | return VideoState.TO_MOVE_TO_EXTERNAL_STORAGE | ||
34 | } | ||
35 | |||
36 | return VideoState.PUBLISHED | ||
37 | } | ||
38 | |||
39 | function moveToNextState (options: { | ||
40 | video: MVideoUUID | ||
41 | previousVideoState?: VideoState | ||
42 | isNewVideo?: boolean // Default true | ||
43 | }) { | ||
44 | const { video, previousVideoState, isNewVideo = true } = options | ||
45 | |||
46 | return retryTransactionWrapper(() => { | ||
47 | return sequelizeTypescript.transaction(async t => { | ||
48 | // Maybe the video changed in database, refresh it | ||
49 | const videoDatabase = await VideoModel.loadFull(video.uuid, t) | ||
50 | // Video does not exist anymore | ||
51 | if (!videoDatabase) return undefined | ||
52 | |||
53 | // Already in its final state | ||
54 | if (videoDatabase.state === VideoState.PUBLISHED) { | ||
55 | return federateVideoIfNeeded(videoDatabase, false, t) | ||
56 | } | ||
57 | |||
58 | const newState = buildNextVideoState(videoDatabase.state) | ||
59 | |||
60 | if (newState === VideoState.PUBLISHED) { | ||
61 | return moveToPublishedState({ video: videoDatabase, previousVideoState, isNewVideo, transaction: t }) | ||
62 | } | ||
63 | |||
64 | if (newState === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) { | ||
65 | return moveToExternalStorageState({ video: videoDatabase, isNewVideo, transaction: t }) | ||
66 | } | ||
67 | }) | ||
68 | }) | ||
69 | } | ||
70 | |||
71 | async function moveToExternalStorageState (options: { | ||
72 | video: MVideoFullLight | ||
73 | isNewVideo: boolean | ||
74 | transaction: Transaction | ||
75 | }) { | ||
76 | const { video, isNewVideo, transaction } = options | ||
77 | |||
78 | const videoJobInfo = await VideoJobInfoModel.load(video.id, transaction) | ||
79 | const pendingTranscode = videoJobInfo?.pendingTranscode || 0 | ||
80 | |||
81 | // We want to wait all transcoding jobs before moving the video on an external storage | ||
82 | if (pendingTranscode !== 0) return false | ||
83 | |||
84 | const previousVideoState = video.state | ||
85 | |||
86 | if (video.state !== VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) { | ||
87 | await video.setNewState(VideoState.TO_MOVE_TO_EXTERNAL_STORAGE, isNewVideo, transaction) | ||
88 | } | ||
89 | |||
90 | logger.info('Creating external storage move job for video %s.', video.uuid, { tags: [ video.uuid ] }) | ||
91 | |||
92 | try { | ||
93 | await JobQueue.Instance.createJob(await buildMoveToObjectStorageJob({ video, previousVideoState, isNewVideo })) | ||
94 | |||
95 | return true | ||
96 | } catch (err) { | ||
97 | logger.error('Cannot add move to object storage job', { err }) | ||
98 | |||
99 | return false | ||
100 | } | ||
101 | } | ||
102 | |||
103 | function moveToFailedTranscodingState (video: MVideo) { | ||
104 | if (video.state === VideoState.TRANSCODING_FAILED) return | ||
105 | |||
106 | return video.setNewState(VideoState.TRANSCODING_FAILED, false, undefined) | ||
107 | } | ||
108 | |||
109 | function moveToFailedMoveToObjectStorageState (video: MVideo) { | ||
110 | if (video.state === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE_FAILED) return | ||
111 | |||
112 | return video.setNewState(VideoState.TO_MOVE_TO_EXTERNAL_STORAGE_FAILED, false, undefined) | ||
113 | } | ||
114 | |||
115 | // --------------------------------------------------------------------------- | ||
116 | |||
117 | export { | ||
118 | buildNextVideoState, | ||
119 | moveToExternalStorageState, | ||
120 | moveToFailedTranscodingState, | ||
121 | moveToFailedMoveToObjectStorageState, | ||
122 | moveToNextState | ||
123 | } | ||
124 | |||
125 | // --------------------------------------------------------------------------- | ||
126 | |||
127 | async function moveToPublishedState (options: { | ||
128 | video: MVideoFullLight | ||
129 | isNewVideo: boolean | ||
130 | transaction: Transaction | ||
131 | previousVideoState?: VideoState | ||
132 | }) { | ||
133 | const { video, isNewVideo, transaction, previousVideoState } = options | ||
134 | const previousState = previousVideoState ?? video.state | ||
135 | |||
136 | logger.info('Publishing video %s.', video.uuid, { isNewVideo, previousState, tags: [ video.uuid ] }) | ||
137 | |||
138 | await video.setNewState(VideoState.PUBLISHED, isNewVideo, transaction) | ||
139 | |||
140 | await federateVideoIfNeeded(video, isNewVideo, transaction) | ||
141 | |||
142 | if (previousState === VideoState.TO_EDIT) { | ||
143 | Notifier.Instance.notifyOfFinishedVideoStudioEdition(video) | ||
144 | return | ||
145 | } | ||
146 | |||
147 | if (isNewVideo) { | ||
148 | Notifier.Instance.notifyOnNewVideoIfNeeded(video) | ||
149 | |||
150 | if (previousState === VideoState.TO_TRANSCODE) { | ||
151 | Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(video) | ||
152 | } | ||
153 | } | ||
154 | } | ||