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