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