diff options
author | Chocobozzz <me@florianbigard.com> | 2022-03-16 15:34:21 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2022-03-16 15:45:58 +0100 |
commit | f012319a644fe8d9d33f2f567fa828442a3b39fd (patch) | |
tree | 8fc7aeff10749ed8088e3f89745433b59bb62c20 /server/controllers/api/videos | |
parent | 83664918901564830f3b7d1bd9879411a1b857a8 (diff) | |
download | PeerTube-f012319a644fe8d9d33f2f567fa828442a3b39fd.tar.gz PeerTube-f012319a644fe8d9d33f2f567fa828442a3b39fd.tar.zst PeerTube-f012319a644fe8d9d33f2f567fa828442a3b39fd.zip |
Process video torrents in order
Prevent update before video torrent generation for example
Diffstat (limited to 'server/controllers/api/videos')
-rw-r--r-- | server/controllers/api/videos/update.ts | 34 | ||||
-rw-r--r-- | server/controllers/api/videos/upload.ts | 64 |
2 files changed, 47 insertions, 51 deletions
diff --git a/server/controllers/api/videos/update.ts b/server/controllers/api/videos/update.ts index 15899307d..2cf8a5883 100644 --- a/server/controllers/api/videos/update.ts +++ b/server/controllers/api/videos/update.ts | |||
@@ -1,12 +1,12 @@ | |||
1 | import express from 'express' | 1 | import express from 'express' |
2 | import { Transaction } from 'sequelize/types' | 2 | import { Transaction } from 'sequelize/types' |
3 | import { updateTorrentMetadata } from '@server/helpers/webtorrent' | ||
4 | import { changeVideoChannelShare } from '@server/lib/activitypub/share' | 3 | import { changeVideoChannelShare } from '@server/lib/activitypub/share' |
4 | import { JobQueue } from '@server/lib/job-queue' | ||
5 | import { buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video' | 5 | import { buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video' |
6 | import { openapiOperationDoc } from '@server/middlewares/doc' | 6 | import { openapiOperationDoc } from '@server/middlewares/doc' |
7 | import { FilteredModelAttributes } from '@server/types' | 7 | import { FilteredModelAttributes } from '@server/types' |
8 | import { MVideoFullLight } from '@server/types/models' | 8 | import { MVideoFullLight } from '@server/types/models' |
9 | import { HttpStatusCode, VideoUpdate } from '@shared/models' | 9 | import { HttpStatusCode, ManageVideoTorrentPayload, VideoUpdate } from '@shared/models' |
10 | import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' | 10 | import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' |
11 | import { resetSequelizeInstance } from '../../../helpers/database-utils' | 11 | import { resetSequelizeInstance } from '../../../helpers/database-utils' |
12 | import { createReqFiles } from '../../../helpers/express-utils' | 12 | import { createReqFiles } from '../../../helpers/express-utils' |
@@ -139,15 +139,13 @@ async function updateVideo (req: express.Request, res: express.Response) { | |||
139 | return { videoInstanceUpdated, isNewVideo } | 139 | return { videoInstanceUpdated, isNewVideo } |
140 | }) | 140 | }) |
141 | 141 | ||
142 | if (videoInstanceUpdated.isLive !== true && videoInfoToUpdate.name) { | 142 | const refreshedVideo = await updateTorrentsMetadataIfNeeded(videoInstanceUpdated, videoInfoToUpdate) |
143 | await updateTorrentsMetadata(videoInstanceUpdated) | ||
144 | } | ||
145 | 143 | ||
146 | await sequelizeTypescript.transaction(t => federateVideoIfNeeded(videoInstanceUpdated, isNewVideo, t)) | 144 | await sequelizeTypescript.transaction(t => federateVideoIfNeeded(refreshedVideo, isNewVideo, t)) |
147 | 145 | ||
148 | if (wasConfidentialVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(videoInstanceUpdated) | 146 | if (wasConfidentialVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(refreshedVideo) |
149 | 147 | ||
150 | Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body, req, res }) | 148 | Hooks.runAction('action:api.video.updated', { video: refreshedVideo, body: req.body, req, res }) |
151 | } catch (err) { | 149 | } catch (err) { |
152 | // Force fields we want to update | 150 | // Force fields we want to update |
153 | // If the transaction is retried, sequelize will think the object has not changed | 151 | // If the transaction is retried, sequelize will think the object has not changed |
@@ -194,19 +192,25 @@ function updateSchedule (videoInstance: MVideoFullLight, videoInfoToUpdate: Vide | |||
194 | } | 192 | } |
195 | } | 193 | } |
196 | 194 | ||
197 | async function updateTorrentsMetadata (video: MVideoFullLight) { | 195 | async function updateTorrentsMetadataIfNeeded (video: MVideoFullLight, videoInfoToUpdate: VideoUpdate) { |
196 | if (video.isLive || !videoInfoToUpdate.name) return video | ||
197 | |||
198 | for (const file of (video.VideoFiles || [])) { | 198 | for (const file of (video.VideoFiles || [])) { |
199 | await updateTorrentMetadata(video, file) | 199 | const payload: ManageVideoTorrentPayload = { action: 'update-metadata', videoId: video.id, videoFileId: file.id } |
200 | 200 | ||
201 | await file.save() | 201 | const job = await JobQueue.Instance.createJobWithPromise({ type: 'manage-video-torrent', payload }) |
202 | await job.finished() | ||
202 | } | 203 | } |
203 | 204 | ||
204 | const hls = video.getHLSPlaylist() | 205 | const hls = video.getHLSPlaylist() |
205 | if (!hls) return | ||
206 | 206 | ||
207 | for (const file of (hls.VideoFiles || [])) { | 207 | for (const file of (hls?.VideoFiles || [])) { |
208 | await updateTorrentMetadata(hls, file) | 208 | const payload: ManageVideoTorrentPayload = { action: 'update-metadata', streamingPlaylistId: hls.id, videoFileId: file.id } |
209 | 209 | ||
210 | await file.save() | 210 | const job = await JobQueue.Instance.createJobWithPromise({ type: 'manage-video-torrent', payload }) |
211 | await job.finished() | ||
211 | } | 212 | } |
213 | |||
214 | // Refresh video since files have changed | ||
215 | return VideoModel.loadAndPopulateAccountAndServerAndTags(video.id) | ||
212 | } | 216 | } |
diff --git a/server/controllers/api/videos/upload.ts b/server/controllers/api/videos/upload.ts index dd69cf238..14ae9d920 100644 --- a/server/controllers/api/videos/upload.ts +++ b/server/controllers/api/videos/upload.ts | |||
@@ -2,8 +2,8 @@ import express from 'express' | |||
2 | import { move } from 'fs-extra' | 2 | import { move } from 'fs-extra' |
3 | import { basename } from 'path' | 3 | import { basename } from 'path' |
4 | import { getResumableUploadPath } from '@server/helpers/upload' | 4 | import { getResumableUploadPath } from '@server/helpers/upload' |
5 | import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' | ||
6 | import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url' | 5 | import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url' |
6 | import { JobQueue } from '@server/lib/job-queue' | ||
7 | import { generateWebTorrentVideoFilename } from '@server/lib/paths' | 7 | import { generateWebTorrentVideoFilename } from '@server/lib/paths' |
8 | import { Redis } from '@server/lib/redis' | 8 | import { Redis } from '@server/lib/redis' |
9 | import { uploadx } from '@server/lib/uploadx' | 9 | import { uploadx } from '@server/lib/uploadx' |
@@ -17,10 +17,10 @@ import { | |||
17 | import { VideoPathManager } from '@server/lib/video-path-manager' | 17 | import { VideoPathManager } from '@server/lib/video-path-manager' |
18 | import { buildNextVideoState } from '@server/lib/video-state' | 18 | import { buildNextVideoState } from '@server/lib/video-state' |
19 | import { openapiOperationDoc } from '@server/middlewares/doc' | 19 | import { openapiOperationDoc } from '@server/middlewares/doc' |
20 | import { MVideo, MVideoFile, MVideoFullLight } from '@server/types/models' | 20 | import { MVideoFile, MVideoFullLight } from '@server/types/models' |
21 | import { getLowercaseExtension } from '@shared/core-utils' | 21 | import { getLowercaseExtension } from '@shared/core-utils' |
22 | import { isAudioFile, uuidToShort } from '@shared/extra-utils' | 22 | import { isAudioFile, uuidToShort } from '@shared/extra-utils' |
23 | import { HttpStatusCode, VideoCreate, VideoResolution, VideoState } from '@shared/models' | 23 | import { HttpStatusCode, ManageVideoTorrentPayload, VideoCreate, VideoResolution, VideoState } from '@shared/models' |
24 | import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' | 24 | import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' |
25 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | 25 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
26 | import { createReqFiles } from '../../../helpers/express-utils' | 26 | import { createReqFiles } from '../../../helpers/express-utils' |
@@ -209,17 +209,22 @@ async function addVideo (options: { | |||
209 | // Channel has a new content, set as updated | 209 | // Channel has a new content, set as updated |
210 | await videoCreated.VideoChannel.setAsUpdated() | 210 | await videoCreated.VideoChannel.setAsUpdated() |
211 | 211 | ||
212 | createTorrentFederate(video, videoFile) | 212 | createTorrentFederate(videoCreated, videoFile) |
213 | .then(() => { | 213 | .catch(err => { |
214 | if (video.state === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) { | 214 | logger.error('Cannot create torrent or federate video for %s.', videoCreated.uuid, { err, ...lTags(videoCreated.uuid) }) |
215 | return addMoveToObjectStorageJob(video) | 215 | |
216 | return videoCreated | ||
217 | }).then(refreshedVideo => { | ||
218 | if (!refreshedVideo) return | ||
219 | |||
220 | if (refreshedVideo.state === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) { | ||
221 | return addMoveToObjectStorageJob(refreshedVideo) | ||
216 | } | 222 | } |
217 | 223 | ||
218 | if (video.state === VideoState.TO_TRANSCODE) { | 224 | if (refreshedVideo.state === VideoState.TO_TRANSCODE) { |
219 | return addOptimizeOrMergeAudioJob(videoCreated, videoFile, user) | 225 | return addOptimizeOrMergeAudioJob(refreshedVideo, videoFile, user) |
220 | } | 226 | } |
221 | }) | 227 | }).catch(err => logger.error('Cannot add optimize/merge audio job for %s.', videoCreated.uuid, { err, ...lTags(videoCreated.uuid) })) |
222 | .catch(err => logger.error('Cannot add optimize/merge audio job for %s.', videoCreated.uuid, { err, ...lTags(videoCreated.uuid) })) | ||
223 | 228 | ||
224 | Hooks.runAction('action:api.video.uploaded', { video: videoCreated, req, res }) | 229 | Hooks.runAction('action:api.video.uploaded', { video: videoCreated, req, res }) |
225 | 230 | ||
@@ -254,36 +259,23 @@ async function buildNewFile (videoPhysicalFile: express.VideoUploadFile) { | |||
254 | return videoFile | 259 | return videoFile |
255 | } | 260 | } |
256 | 261 | ||
257 | async function createTorrentAndSetInfoHashAsync (video: MVideo, fileArg: MVideoFile) { | 262 | async function createTorrentFederate (video: MVideoFullLight, videoFile: MVideoFile) { |
258 | await createTorrentAndSetInfoHash(video, fileArg) | 263 | const payload: ManageVideoTorrentPayload = { videoId: video.id, videoFileId: videoFile.id, action: 'create' } |
259 | 264 | ||
260 | // Refresh videoFile because the createTorrentAndSetInfoHash could be long | 265 | const job = await JobQueue.Instance.createJobWithPromise({ type: 'manage-video-torrent', payload }) |
261 | const refreshedFile = await VideoFileModel.loadWithVideo(fileArg.id) | 266 | await job.finished() |
262 | // File does not exist anymore, remove the generated torrent | ||
263 | if (!refreshedFile) return fileArg.removeTorrent() | ||
264 | 267 | ||
265 | refreshedFile.infoHash = fileArg.infoHash | 268 | const refreshedVideo = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.id) |
266 | refreshedFile.torrentFilename = fileArg.torrentFilename | 269 | if (!refreshedVideo) return |
267 | 270 | ||
268 | return refreshedFile.save() | 271 | // Only federate and notify after the torrent creation |
269 | } | 272 | Notifier.Instance.notifyOnNewVideoIfNeeded(refreshedVideo) |
270 | 273 | ||
271 | function createTorrentFederate (video: MVideoFullLight, videoFile: MVideoFile) { | 274 | await retryTransactionWrapper(() => { |
272 | // Create the torrent file in async way because it could be long | 275 | return sequelizeTypescript.transaction(t => federateVideoIfNeeded(refreshedVideo, true, t)) |
273 | return createTorrentAndSetInfoHashAsync(video, videoFile) | 276 | }) |
274 | .catch(err => logger.error('Cannot create torrent file for video %s', video.url, { err, ...lTags(video.uuid) })) | ||
275 | .then(() => VideoModel.loadAndPopulateAccountAndServerAndTags(video.id)) | ||
276 | .then(refreshedVideo => { | ||
277 | if (!refreshedVideo) return | ||
278 | |||
279 | // Only federate and notify after the torrent creation | ||
280 | Notifier.Instance.notifyOnNewVideoIfNeeded(refreshedVideo) | ||
281 | 277 | ||
282 | return retryTransactionWrapper(() => { | 278 | return refreshedVideo |
283 | return sequelizeTypescript.transaction(t => federateVideoIfNeeded(refreshedVideo, true, t)) | ||
284 | }) | ||
285 | }) | ||
286 | .catch(err => logger.error('Cannot federate or notify video creation %s', video.url, { err, ...lTags(video.uuid) })) | ||
287 | } | 279 | } |
288 | 280 | ||
289 | async function deleteUploadResumableCache (req: express.Request, res: express.Response, next: express.NextFunction) { | 281 | async function deleteUploadResumableCache (req: express.Request, res: express.Response, next: express.NextFunction) { |