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