]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/video-state.ts
Merge remote-tracking branch 'weblate/develop' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / video-state.ts
... / ...
CommitLineData
1import { Transaction } from 'sequelize'
2import { logger } from '@server/helpers/logger'
3import { CONFIG } from '@server/initializers/config'
4import { sequelizeTypescript } from '@server/initializers/database'
5import { VideoModel } from '@server/models/video/video'
6import { VideoJobInfoModel } from '@server/models/video/video-job-info'
7import { MVideo, MVideoFullLight, MVideoUUID } from '@server/types/models'
8import { VideoState } from '@shared/models'
9import { federateVideoIfNeeded } from './activitypub/videos'
10import { Notifier } from './notifier'
11import { addMoveToObjectStorageJob } from './video'
12import { retryTransactionWrapper } from '@server/helpers/database-utils'
13
14function 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
38function 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
70async 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
99function moveToFailedTranscodingState (video: MVideo) {
100 if (video.state === VideoState.TRANSCODING_FAILED) return
101
102 return video.setNewState(VideoState.TRANSCODING_FAILED, false, undefined)
103}
104
105function 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
113export {
114 buildNextVideoState,
115 moveToExternalStorageState,
116 moveToFailedTranscodingState,
117 moveToFailedMoveToObjectStorageState,
118 moveToNextState
119}
120
121// ---------------------------------------------------------------------------
122
123async 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}