diff options
author | Chocobozzz <me@florianbigard.com> | 2018-06-13 14:27:40 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-06-13 14:27:40 +0200 |
commit | 90d4bb8125e80c8060416d4d135ddeaf0a622ede (patch) | |
tree | b3b7181329a08ecc930b54fe7b48095c4155393c /server/controllers/api/videos/index.ts | |
parent | 3cd0734fd9b0ff21aaef02317a874e8f1c06e027 (diff) | |
download | PeerTube-90d4bb8125e80c8060416d4d135ddeaf0a622ede.tar.gz PeerTube-90d4bb8125e80c8060416d4d135ddeaf0a622ede.tar.zst PeerTube-90d4bb8125e80c8060416d4d135ddeaf0a622ede.zip |
Refractor retry transaction function
Diffstat (limited to 'server/controllers/api/videos/index.ts')
-rw-r--r-- | server/controllers/api/videos/index.ts | 62 |
1 files changed, 16 insertions, 46 deletions
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index 9d9b2b0e1..78963d89b 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts | |||
@@ -2,7 +2,6 @@ import * as express from 'express' | |||
2 | import { extname, join } from 'path' | 2 | import { extname, join } from 'path' |
3 | import { VideoCreate, VideoPrivacy, VideoState, VideoUpdate } from '../../../../shared' | 3 | import { VideoCreate, VideoPrivacy, VideoState, VideoUpdate } from '../../../../shared' |
4 | import { renamePromise } from '../../../helpers/core-utils' | 4 | import { renamePromise } from '../../../helpers/core-utils' |
5 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | ||
6 | import { getVideoFileResolution } from '../../../helpers/ffmpeg-utils' | 5 | import { getVideoFileResolution } from '../../../helpers/ffmpeg-utils' |
7 | import { processImage } from '../../../helpers/image-utils' | 6 | import { processImage } from '../../../helpers/image-utils' |
8 | import { logger } from '../../../helpers/logger' | 7 | import { logger } from '../../../helpers/logger' |
@@ -30,6 +29,7 @@ import { JobQueue } from '../../../lib/job-queue' | |||
30 | import { Redis } from '../../../lib/redis' | 29 | import { Redis } from '../../../lib/redis' |
31 | import { | 30 | import { |
32 | asyncMiddleware, | 31 | asyncMiddleware, |
32 | asyncRetryTransactionMiddleware, | ||
33 | authenticate, | 33 | authenticate, |
34 | optionalAuthenticate, | 34 | optionalAuthenticate, |
35 | paginationValidator, | 35 | paginationValidator, |
@@ -104,13 +104,13 @@ videosRouter.put('/:id', | |||
104 | authenticate, | 104 | authenticate, |
105 | reqVideoFileUpdate, | 105 | reqVideoFileUpdate, |
106 | asyncMiddleware(videosUpdateValidator), | 106 | asyncMiddleware(videosUpdateValidator), |
107 | asyncMiddleware(updateVideoRetryWrapper) | 107 | asyncRetryTransactionMiddleware(updateVideo) |
108 | ) | 108 | ) |
109 | videosRouter.post('/upload', | 109 | videosRouter.post('/upload', |
110 | authenticate, | 110 | authenticate, |
111 | reqVideoFileAdd, | 111 | reqVideoFileAdd, |
112 | asyncMiddleware(videosAddValidator), | 112 | asyncMiddleware(videosAddValidator), |
113 | asyncMiddleware(addVideoRetryWrapper) | 113 | asyncRetryTransactionMiddleware(addVideo) |
114 | ) | 114 | ) |
115 | 115 | ||
116 | videosRouter.get('/:id/description', | 116 | videosRouter.get('/:id/description', |
@@ -129,7 +129,7 @@ videosRouter.post('/:id/views', | |||
129 | videosRouter.delete('/:id', | 129 | videosRouter.delete('/:id', |
130 | authenticate, | 130 | authenticate, |
131 | asyncMiddleware(videosRemoveValidator), | 131 | asyncMiddleware(videosRemoveValidator), |
132 | asyncMiddleware(removeVideoRetryWrapper) | 132 | asyncRetryTransactionMiddleware(removeVideo) |
133 | ) | 133 | ) |
134 | 134 | ||
135 | // --------------------------------------------------------------------------- | 135 | // --------------------------------------------------------------------------- |
@@ -156,25 +156,8 @@ function listVideoPrivacies (req: express.Request, res: express.Response) { | |||
156 | res.json(VIDEO_PRIVACIES) | 156 | res.json(VIDEO_PRIVACIES) |
157 | } | 157 | } |
158 | 158 | ||
159 | // Wrapper to video add that retry the function if there is a database error | 159 | async function addVideo (req: express.Request, res: express.Response) { |
160 | // We need this because we run the transaction in SERIALIZABLE isolation that can fail | 160 | const videoPhysicalFile = req.files['videofile'][0] |
161 | async function addVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
162 | const options = { | ||
163 | arguments: [ req, res, req.files['videofile'][0] ], | ||
164 | errorMessage: 'Cannot insert the video with many retries.' | ||
165 | } | ||
166 | |||
167 | const video = await retryTransactionWrapper(addVideo, options) | ||
168 | |||
169 | res.json({ | ||
170 | video: { | ||
171 | id: video.id, | ||
172 | uuid: video.uuid | ||
173 | } | ||
174 | }).end() | ||
175 | } | ||
176 | |||
177 | async function addVideo (req: express.Request, res: express.Response, videoPhysicalFile: Express.Multer.File) { | ||
178 | const videoInfo: VideoCreate = req.body | 161 | const videoInfo: VideoCreate = req.body |
179 | 162 | ||
180 | // Prepare data so we don't block the transaction | 163 | // Prepare data so we don't block the transaction |
@@ -272,18 +255,12 @@ async function addVideo (req: express.Request, res: express.Response, videoPhysi | |||
272 | await JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput }) | 255 | await JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput }) |
273 | } | 256 | } |
274 | 257 | ||
275 | return videoCreated | 258 | return res.json({ |
276 | } | 259 | video: { |
277 | 260 | id: videoCreated.id, | |
278 | async function updateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { | 261 | uuid: videoCreated.uuid |
279 | const options = { | 262 | } |
280 | arguments: [ req, res ], | 263 | }).end() |
281 | errorMessage: 'Cannot update the video with many retries.' | ||
282 | } | ||
283 | |||
284 | await retryTransactionWrapper(updateVideo, options) | ||
285 | |||
286 | return res.type('json').status(204).end() | ||
287 | } | 264 | } |
288 | 265 | ||
289 | async function updateVideo (req: express.Request, res: express.Response) { | 266 | async function updateVideo (req: express.Request, res: express.Response) { |
@@ -360,6 +337,8 @@ async function updateVideo (req: express.Request, res: express.Response) { | |||
360 | 337 | ||
361 | throw err | 338 | throw err |
362 | } | 339 | } |
340 | |||
341 | return res.type('json').status(204).end() | ||
363 | } | 342 | } |
364 | 343 | ||
365 | function getVideo (req: express.Request, res: express.Response) { | 344 | function getVideo (req: express.Request, res: express.Response) { |
@@ -414,17 +393,6 @@ async function listVideos (req: express.Request, res: express.Response, next: ex | |||
414 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | 393 | return res.json(getFormattedObjects(resultList.data, resultList.total)) |
415 | } | 394 | } |
416 | 395 | ||
417 | async function removeVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
418 | const options = { | ||
419 | arguments: [ req, res ], | ||
420 | errorMessage: 'Cannot remove the video with many retries.' | ||
421 | } | ||
422 | |||
423 | await retryTransactionWrapper(removeVideo, options) | ||
424 | |||
425 | return res.type('json').status(204).end() | ||
426 | } | ||
427 | |||
428 | async function removeVideo (req: express.Request, res: express.Response) { | 396 | async function removeVideo (req: express.Request, res: express.Response) { |
429 | const videoInstance: VideoModel = res.locals.video | 397 | const videoInstance: VideoModel = res.locals.video |
430 | 398 | ||
@@ -433,6 +401,8 @@ async function removeVideo (req: express.Request, res: express.Response) { | |||
433 | }) | 401 | }) |
434 | 402 | ||
435 | logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid) | 403 | logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid) |
404 | |||
405 | return res.type('json').status(204).end() | ||
436 | } | 406 | } |
437 | 407 | ||
438 | async function searchVideos (req: express.Request, res: express.Response, next: express.NextFunction) { | 408 | async function searchVideos (req: express.Request, res: express.Response, next: express.NextFunction) { |