aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/video-state.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/video-state.ts')
-rw-r--r--server/lib/video-state.ts154
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 @@
1import { Transaction } from 'sequelize'
2import { retryTransactionWrapper } from '@server/helpers/database-utils'
3import { logger } from '@server/helpers/logger'
4import { CONFIG } from '@server/initializers/config'
5import { sequelizeTypescript } from '@server/initializers/database'
6import { VideoModel } from '@server/models/video/video'
7import { VideoJobInfoModel } from '@server/models/video/video-job-info'
8import { MVideo, MVideoFullLight, MVideoUUID } from '@server/types/models'
9import { VideoState } from '@shared/models'
10import { federateVideoIfNeeded } from './activitypub/videos'
11import { JobQueue } from './job-queue'
12import { Notifier } from './notifier'
13import { buildMoveToObjectStorageJob } from './video'
14
15function 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
39function 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
71async 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
103function moveToFailedTranscodingState (video: MVideo) {
104 if (video.state === VideoState.TRANSCODING_FAILED) return
105
106 return video.setNewState(VideoState.TRANSCODING_FAILED, false, undefined)
107}
108
109function 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
117export {
118 buildNextVideoState,
119 moveToExternalStorageState,
120 moveToFailedTranscodingState,
121 moveToFailedMoveToObjectStorageState,
122 moveToNextState
123}
124
125// ---------------------------------------------------------------------------
126
127async 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}