]>
Commit | Line | Data |
---|---|---|
0305db28 | 1 | import { Transaction } from 'sequelize' |
bd911b54 | 2 | import { retryTransactionWrapper } from '@server/helpers/database-utils' |
0305db28 JB |
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' | |
dbd9fb44 | 8 | import { MVideo, MVideoFullLight, MVideoUUID } from '@server/types/models' |
0305db28 JB |
9 | import { VideoState } from '@shared/models' |
10 | import { federateVideoIfNeeded } from './activitypub/videos' | |
bd911b54 | 11 | import { JobQueue } from './job-queue' |
0305db28 | 12 | import { Notifier } from './notifier' |
bd911b54 | 13 | import { buildMoveToObjectStorageJob } from './video' |
0305db28 JB |
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 ( | |
1808a1f8 | 21 | currentState !== VideoState.TO_EDIT && |
0305db28 JB |
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 | ||
1808a1f8 C |
39 | function moveToNextState (options: { |
40 | video: MVideoUUID | |
41 | previousVideoState?: VideoState | |
42 | isNewVideo?: boolean // Default true | |
43 | }) { | |
44 | const { video, previousVideoState, isNewVideo = true } = options | |
45 | ||
4ddb53f6 C |
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 | |
0305db28 | 52 | |
4ddb53f6 C |
53 | // Already in its final state |
54 | if (videoDatabase.state === VideoState.PUBLISHED) { | |
55 | return federateVideoIfNeeded(videoDatabase, false, t) | |
56 | } | |
0305db28 | 57 | |
4ddb53f6 | 58 | const newState = buildNextVideoState(videoDatabase.state) |
0305db28 | 59 | |
4ddb53f6 C |
60 | if (newState === VideoState.PUBLISHED) { |
61 | return moveToPublishedState({ video: videoDatabase, previousVideoState, isNewVideo, transaction: t }) | |
62 | } | |
0305db28 | 63 | |
4ddb53f6 C |
64 | if (newState === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) { |
65 | return moveToExternalStorageState({ video: videoDatabase, isNewVideo, transaction: t }) | |
66 | } | |
67 | }) | |
0305db28 JB |
68 | }) |
69 | } | |
70 | ||
1808a1f8 C |
71 | async function moveToExternalStorageState (options: { |
72 | video: MVideoFullLight | |
73 | isNewVideo: boolean | |
74 | transaction: Transaction | |
75 | }) { | |
76 | const { video, isNewVideo, transaction } = options | |
77 | ||
e1ab52d7 | 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 | ||
1808a1f8 | 84 | const previousVideoState = video.state |
690bad52 C |
85 | |
86 | if (video.state !== VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) { | |
87 | await video.setNewState(VideoState.TO_MOVE_TO_EXTERNAL_STORAGE, isNewVideo, transaction) | |
88 | } | |
e1ab52d7 | 89 | |
90 | logger.info('Creating external storage move job for video %s.', video.uuid, { tags: [ video.uuid ] }) | |
91 | ||
92 | try { | |
bd911b54 | 93 | await JobQueue.Instance.createJob(await buildMoveToObjectStorageJob({ video, previousVideoState, isNewVideo })) |
e1ab52d7 | 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 | ||
dbd9fb44 | 103 | function moveToFailedTranscodingState (video: MVideo) { |
ad5db104 C |
104 | if (video.state === VideoState.TRANSCODING_FAILED) return |
105 | ||
221ee1ad | 106 | return video.setNewState(VideoState.TRANSCODING_FAILED, false, undefined) |
4e29f4fe | 107 | } |
108 | ||
dbd9fb44 C |
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 | ||
0305db28 JB |
115 | // --------------------------------------------------------------------------- |
116 | ||
117 | export { | |
118 | buildNextVideoState, | |
e1ab52d7 | 119 | moveToExternalStorageState, |
221ee1ad | 120 | moveToFailedTranscodingState, |
dbd9fb44 | 121 | moveToFailedMoveToObjectStorageState, |
0305db28 JB |
122 | moveToNextState |
123 | } | |
124 | ||
125 | // --------------------------------------------------------------------------- | |
126 | ||
1808a1f8 C |
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 | ||
26e3e98f | 136 | logger.info('Publishing video %s.', video.uuid, { isNewVideo, previousState, tags: [ video.uuid ] }) |
0305db28 | 137 | |
9db2330e | 138 | await video.setNewState(VideoState.PUBLISHED, isNewVideo, transaction) |
0305db28 | 139 | |
0305db28 JB |
140 | await federateVideoIfNeeded(video, isNewVideo, transaction) |
141 | ||
1808a1f8 | 142 | if (previousState === VideoState.TO_EDIT) { |
92e66e04 | 143 | Notifier.Instance.notifyOfFinishedVideoStudioEdition(video) |
1808a1f8 C |
144 | return |
145 | } | |
1da843ee | 146 | |
1808a1f8 C |
147 | if (isNewVideo) { |
148 | Notifier.Instance.notifyOnNewVideoIfNeeded(video) | |
0305db28 | 149 | |
1808a1f8 C |
150 | if (previousState === VideoState.TO_TRANSCODE) { |
151 | Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(video) | |
152 | } | |
0305db28 JB |
153 | } |
154 | } |