]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/video-state.ts
Typo
[github/Chocobozzz/PeerTube.git] / server / lib / video-state.ts
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
13 function buildNextVideoState (currentState?: VideoState) {
14 if (currentState === VideoState.PUBLISHED) {
15 throw new Error('Video is already in its final state')
16 }
17
18 if (
19 currentState !== VideoState.TO_EDIT &&
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
37 function moveToNextState (options: {
38 video: MVideoUUID
39 previousVideoState?: VideoState
40 isNewVideo?: boolean // Default true
41 }) {
42 const { video, previousVideoState, isNewVideo = true } = options
43
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) {
58 return moveToPublishedState({ video: videoDatabase, previousVideoState, isNewVideo, transaction: t })
59 }
60
61 if (newState === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) {
62 return moveToExternalStorageState({ video: videoDatabase, isNewVideo, transaction: t })
63 }
64 })
65 }
66
67 async function moveToExternalStorageState (options: {
68 video: MVideoFullLight
69 isNewVideo: boolean
70 transaction: Transaction
71 }) {
72 const { video, isNewVideo, transaction } = options
73
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
80 const previousVideoState = video.state
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 {
86 await addMoveToObjectStorageJob({ video, previousVideoState, isNewVideo })
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
96 function moveToFailedTranscodingState (video: MVideo) {
97 if (video.state === VideoState.TRANSCODING_FAILED) return
98
99 return video.setNewState(VideoState.TRANSCODING_FAILED, false, undefined)
100 }
101
102 function 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
108 // ---------------------------------------------------------------------------
109
110 export {
111 buildNextVideoState,
112 moveToExternalStorageState,
113 moveToFailedTranscodingState,
114 moveToFailedMoveToObjectStorageState,
115 moveToNextState
116 }
117
118 // ---------------------------------------------------------------------------
119
120 async 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 ] })
130
131 await video.setNewState(VideoState.PUBLISHED, isNewVideo, transaction)
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
137 if (previousState === VideoState.TO_EDIT) {
138 Notifier.Instance.notifyOfFinishedVideoStudioEdition(video)
139 return
140 }
141
142 if (isNewVideo) {
143 Notifier.Instance.notifyOnNewVideoIfNeeded(video)
144
145 if (previousState === VideoState.TO_TRANSCODE) {
146 Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(video)
147 }
148 }
149 }