]>
Commit | Line | Data |
---|---|---|
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 | await video.setNewState(VideoState.TO_MOVE_TO_EXTERNAL_STORAGE, isNewVideo, transaction) | |
86 | ||
87 | logger.info('Creating external storage move job for video %s.', video.uuid, { tags: [ video.uuid ] }) | |
88 | ||
89 | try { | |
90 | await JobQueue.Instance.createJob(await buildMoveToObjectStorageJob({ video, previousVideoState, isNewVideo })) | |
91 | ||
92 | return true | |
93 | } catch (err) { | |
94 | logger.error('Cannot add move to object storage job', { err }) | |
95 | ||
96 | return false | |
97 | } | |
98 | } | |
99 | ||
100 | function moveToFailedTranscodingState (video: MVideo) { | |
101 | if (video.state === VideoState.TRANSCODING_FAILED) return | |
102 | ||
103 | return video.setNewState(VideoState.TRANSCODING_FAILED, false, undefined) | |
104 | } | |
105 | ||
106 | function moveToFailedMoveToObjectStorageState (video: MVideo) { | |
107 | if (video.state === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE_FAILED) return | |
108 | ||
109 | return video.setNewState(VideoState.TO_MOVE_TO_EXTERNAL_STORAGE_FAILED, false, undefined) | |
110 | } | |
111 | ||
112 | // --------------------------------------------------------------------------- | |
113 | ||
114 | export { | |
115 | buildNextVideoState, | |
116 | moveToExternalStorageState, | |
117 | moveToFailedTranscodingState, | |
118 | moveToFailedMoveToObjectStorageState, | |
119 | moveToNextState | |
120 | } | |
121 | ||
122 | // --------------------------------------------------------------------------- | |
123 | ||
124 | async function moveToPublishedState (options: { | |
125 | video: MVideoFullLight | |
126 | isNewVideo: boolean | |
127 | transaction: Transaction | |
128 | previousVideoState?: VideoState | |
129 | }) { | |
130 | const { video, isNewVideo, transaction, previousVideoState } = options | |
131 | const previousState = previousVideoState ?? video.state | |
132 | ||
133 | logger.info('Publishing video %s.', video.uuid, { isNewVideo, previousState, tags: [ video.uuid ] }) | |
134 | ||
135 | await video.setNewState(VideoState.PUBLISHED, isNewVideo, transaction) | |
136 | ||
137 | await federateVideoIfNeeded(video, isNewVideo, transaction) | |
138 | ||
139 | if (previousState === VideoState.TO_EDIT) { | |
140 | Notifier.Instance.notifyOfFinishedVideoStudioEdition(video) | |
141 | return | |
142 | } | |
143 | ||
144 | if (isNewVideo) { | |
145 | Notifier.Instance.notifyOnNewVideoIfNeeded(video) | |
146 | ||
147 | if (previousState === VideoState.TO_TRANSCODE) { | |
148 | Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(video) | |
149 | } | |
150 | } | |
151 | } |