]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/video-state.ts
Relax log level
[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'
4ddb53f6 12import { retryTransactionWrapper } from '@server/helpers/database-utils'
0305db28
JB
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 (
1808a1f8 20 currentState !== VideoState.TO_EDIT &&
0305db28
JB
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
1808a1f8
C
38function moveToNextState (options: {
39 video: MVideoUUID
40 previousVideoState?: VideoState
41 isNewVideo?: boolean // Default true
42}) {
43 const { video, previousVideoState, isNewVideo = true } = options
44
4ddb53f6
C
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
0305db28 51
4ddb53f6
C
52 // Already in its final state
53 if (videoDatabase.state === VideoState.PUBLISHED) {
54 return federateVideoIfNeeded(videoDatabase, false, t)
55 }
0305db28 56
4ddb53f6 57 const newState = buildNextVideoState(videoDatabase.state)
0305db28 58
4ddb53f6
C
59 if (newState === VideoState.PUBLISHED) {
60 return moveToPublishedState({ video: videoDatabase, previousVideoState, isNewVideo, transaction: t })
61 }
0305db28 62
4ddb53f6
C
63 if (newState === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) {
64 return moveToExternalStorageState({ video: videoDatabase, isNewVideo, transaction: t })
65 }
66 })
0305db28
JB
67 })
68}
69
1808a1f8
C
70async function moveToExternalStorageState (options: {
71 video: MVideoFullLight
72 isNewVideo: boolean
73 transaction: Transaction
74}) {
75 const { video, isNewVideo, transaction } = options
76
e1ab52d7 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
1808a1f8 83 const previousVideoState = video.state
e1ab52d7 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 {
1808a1f8 89 await addMoveToObjectStorageJob({ video, previousVideoState, isNewVideo })
e1ab52d7 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
dbd9fb44 99function moveToFailedTranscodingState (video: MVideo) {
ad5db104
C
100 if (video.state === VideoState.TRANSCODING_FAILED) return
101
221ee1ad 102 return video.setNewState(VideoState.TRANSCODING_FAILED, false, undefined)
4e29f4fe 103}
104
dbd9fb44
C
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
0305db28
JB
111// ---------------------------------------------------------------------------
112
113export {
114 buildNextVideoState,
e1ab52d7 115 moveToExternalStorageState,
221ee1ad 116 moveToFailedTranscodingState,
dbd9fb44 117 moveToFailedMoveToObjectStorageState,
0305db28
JB
118 moveToNextState
119}
120
121// ---------------------------------------------------------------------------
122
1808a1f8
C
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
26e3e98f 132 logger.info('Publishing video %s.', video.uuid, { isNewVideo, previousState, tags: [ video.uuid ] })
0305db28 133
9db2330e 134 await video.setNewState(VideoState.PUBLISHED, isNewVideo, transaction)
0305db28 135
0305db28
JB
136 await federateVideoIfNeeded(video, isNewVideo, transaction)
137
1808a1f8 138 if (previousState === VideoState.TO_EDIT) {
92e66e04 139 Notifier.Instance.notifyOfFinishedVideoStudioEdition(video)
1808a1f8
C
140 return
141 }
1da843ee 142
1808a1f8
C
143 if (isNewVideo) {
144 Notifier.Instance.notifyOnNewVideoIfNeeded(video)
0305db28 145
1808a1f8
C
146 if (previousState === VideoState.TO_TRANSCODE) {
147 Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(video)
148 }
0305db28
JB
149 }
150}