diff options
Diffstat (limited to 'server/lib/video-state.ts')
-rw-r--r-- | server/lib/video-state.ts | 50 |
1 files changed, 38 insertions, 12 deletions
diff --git a/server/lib/video-state.ts b/server/lib/video-state.ts index 97ff540ed..f75f81704 100644 --- a/server/lib/video-state.ts +++ b/server/lib/video-state.ts | |||
@@ -16,6 +16,7 @@ function buildNextVideoState (currentState?: VideoState) { | |||
16 | } | 16 | } |
17 | 17 | ||
18 | if ( | 18 | if ( |
19 | currentState !== VideoState.TO_EDIT && | ||
19 | currentState !== VideoState.TO_TRANSCODE && | 20 | currentState !== VideoState.TO_TRANSCODE && |
20 | currentState !== VideoState.TO_MOVE_TO_EXTERNAL_STORAGE && | 21 | currentState !== VideoState.TO_MOVE_TO_EXTERNAL_STORAGE && |
21 | CONFIG.TRANSCODING.ENABLED | 22 | CONFIG.TRANSCODING.ENABLED |
@@ -33,7 +34,13 @@ function buildNextVideoState (currentState?: VideoState) { | |||
33 | return VideoState.PUBLISHED | 34 | return VideoState.PUBLISHED |
34 | } | 35 | } |
35 | 36 | ||
36 | function moveToNextState (video: MVideoUUID, isNewVideo = true) { | 37 | function moveToNextState (options: { |
38 | video: MVideoUUID | ||
39 | previousVideoState?: VideoState | ||
40 | isNewVideo?: boolean // Default true | ||
41 | }) { | ||
42 | const { video, previousVideoState, isNewVideo = true } = options | ||
43 | |||
37 | return sequelizeTypescript.transaction(async t => { | 44 | return sequelizeTypescript.transaction(async t => { |
38 | // Maybe the video changed in database, refresh it | 45 | // Maybe the video changed in database, refresh it |
39 | const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t) | 46 | const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t) |
@@ -48,28 +55,35 @@ function moveToNextState (video: MVideoUUID, isNewVideo = true) { | |||
48 | const newState = buildNextVideoState(videoDatabase.state) | 55 | const newState = buildNextVideoState(videoDatabase.state) |
49 | 56 | ||
50 | if (newState === VideoState.PUBLISHED) { | 57 | if (newState === VideoState.PUBLISHED) { |
51 | return moveToPublishedState(videoDatabase, isNewVideo, t) | 58 | return moveToPublishedState({ video: videoDatabase, previousVideoState, isNewVideo, transaction: t }) |
52 | } | 59 | } |
53 | 60 | ||
54 | if (newState === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) { | 61 | if (newState === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) { |
55 | return moveToExternalStorageState(videoDatabase, isNewVideo, t) | 62 | return moveToExternalStorageState({ video: videoDatabase, isNewVideo, transaction: t }) |
56 | } | 63 | } |
57 | }) | 64 | }) |
58 | } | 65 | } |
59 | 66 | ||
60 | async function moveToExternalStorageState (video: MVideoFullLight, isNewVideo: boolean, transaction: Transaction) { | 67 | async function moveToExternalStorageState (options: { |
68 | video: MVideoFullLight | ||
69 | isNewVideo: boolean | ||
70 | transaction: Transaction | ||
71 | }) { | ||
72 | const { video, isNewVideo, transaction } = options | ||
73 | |||
61 | const videoJobInfo = await VideoJobInfoModel.load(video.id, transaction) | 74 | const videoJobInfo = await VideoJobInfoModel.load(video.id, transaction) |
62 | const pendingTranscode = videoJobInfo?.pendingTranscode || 0 | 75 | const pendingTranscode = videoJobInfo?.pendingTranscode || 0 |
63 | 76 | ||
64 | // We want to wait all transcoding jobs before moving the video on an external storage | 77 | // We want to wait all transcoding jobs before moving the video on an external storage |
65 | if (pendingTranscode !== 0) return false | 78 | if (pendingTranscode !== 0) return false |
66 | 79 | ||
80 | const previousVideoState = video.state | ||
67 | await video.setNewState(VideoState.TO_MOVE_TO_EXTERNAL_STORAGE, isNewVideo, transaction) | 81 | await video.setNewState(VideoState.TO_MOVE_TO_EXTERNAL_STORAGE, isNewVideo, transaction) |
68 | 82 | ||
69 | logger.info('Creating external storage move job for video %s.', video.uuid, { tags: [ video.uuid ] }) | 83 | logger.info('Creating external storage move job for video %s.', video.uuid, { tags: [ video.uuid ] }) |
70 | 84 | ||
71 | try { | 85 | try { |
72 | await addMoveToObjectStorageJob(video, isNewVideo) | 86 | await addMoveToObjectStorageJob({ video, previousVideoState, isNewVideo }) |
73 | 87 | ||
74 | return true | 88 | return true |
75 | } catch (err) { | 89 | } catch (err) { |
@@ -103,21 +117,33 @@ export { | |||
103 | 117 | ||
104 | // --------------------------------------------------------------------------- | 118 | // --------------------------------------------------------------------------- |
105 | 119 | ||
106 | async function moveToPublishedState (video: MVideoFullLight, isNewVideo: boolean, transaction: Transaction) { | 120 | async function moveToPublishedState (options: { |
107 | logger.info('Publishing video %s.', video.uuid, { tags: [ video.uuid ] }) | 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 ] }) | ||
108 | 130 | ||
109 | const previousState = video.state | ||
110 | await video.setNewState(VideoState.PUBLISHED, isNewVideo, transaction) | 131 | await video.setNewState(VideoState.PUBLISHED, isNewVideo, transaction) |
111 | 132 | ||
112 | // If the video was not published, we consider it is a new one for other instances | 133 | // If the video was not published, we consider it is a new one for other instances |
113 | // Live videos are always federated, so it's not a new video | 134 | // Live videos are always federated, so it's not a new video |
114 | await federateVideoIfNeeded(video, isNewVideo, transaction) | 135 | await federateVideoIfNeeded(video, isNewVideo, transaction) |
115 | 136 | ||
116 | if (!isNewVideo) return | 137 | if (previousState === VideoState.TO_EDIT) { |
138 | Notifier.Instance.notifyOfFinishedVideoEdition(video) | ||
139 | return | ||
140 | } | ||
117 | 141 | ||
118 | Notifier.Instance.notifyOnNewVideoIfNeeded(video) | 142 | if (isNewVideo) { |
143 | Notifier.Instance.notifyOnNewVideoIfNeeded(video) | ||
119 | 144 | ||
120 | if (previousState === VideoState.TO_TRANSCODE) { | 145 | if (previousState === VideoState.TO_TRANSCODE) { |
121 | Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(video) | 146 | Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(video) |
147 | } | ||
122 | } | 148 | } |
123 | } | 149 | } |