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/video-channel.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/video-channel.ts')
-rw-r--r-- | server/controllers/api/video-channel.ts | 55 |
1 files changed, 12 insertions, 43 deletions
diff --git a/server/controllers/api/video-channel.ts b/server/controllers/api/video-channel.ts index 263eb2a8a..61e72125f 100644 --- a/server/controllers/api/video-channel.ts +++ b/server/controllers/api/video-channel.ts | |||
@@ -2,6 +2,7 @@ import * as express from 'express' | |||
2 | import { getFormattedObjects, resetSequelizeInstance } from '../../helpers/utils' | 2 | import { getFormattedObjects, resetSequelizeInstance } from '../../helpers/utils' |
3 | import { | 3 | import { |
4 | asyncMiddleware, | 4 | asyncMiddleware, |
5 | asyncRetryTransactionMiddleware, | ||
5 | authenticate, | 6 | authenticate, |
6 | optionalAuthenticate, | 7 | optionalAuthenticate, |
7 | paginationValidator, | 8 | paginationValidator, |
@@ -20,7 +21,6 @@ import { VideoChannelCreate, VideoChannelUpdate } from '../../../shared' | |||
20 | import { createVideoChannel } from '../../lib/video-channel' | 21 | import { createVideoChannel } from '../../lib/video-channel' |
21 | import { isNSFWHidden } from '../../helpers/express-utils' | 22 | import { isNSFWHidden } from '../../helpers/express-utils' |
22 | import { setAsyncActorKeys } from '../../lib/activitypub' | 23 | import { setAsyncActorKeys } from '../../lib/activitypub' |
23 | import { retryTransactionWrapper } from '../../helpers/database-utils' | ||
24 | import { AccountModel } from '../../models/account/account' | 24 | import { AccountModel } from '../../models/account/account' |
25 | import { sequelizeTypescript } from '../../initializers' | 25 | import { sequelizeTypescript } from '../../initializers' |
26 | import { logger } from '../../helpers/logger' | 26 | import { logger } from '../../helpers/logger' |
@@ -39,19 +39,19 @@ videoChannelRouter.get('/', | |||
39 | videoChannelRouter.post('/', | 39 | videoChannelRouter.post('/', |
40 | authenticate, | 40 | authenticate, |
41 | videoChannelsAddValidator, | 41 | videoChannelsAddValidator, |
42 | asyncMiddleware(addVideoChannelRetryWrapper) | 42 | asyncRetryTransactionMiddleware(addVideoChannel) |
43 | ) | 43 | ) |
44 | 44 | ||
45 | videoChannelRouter.put('/:id', | 45 | videoChannelRouter.put('/:id', |
46 | authenticate, | 46 | authenticate, |
47 | asyncMiddleware(videoChannelsUpdateValidator), | 47 | asyncMiddleware(videoChannelsUpdateValidator), |
48 | updateVideoChannelRetryWrapper | 48 | asyncRetryTransactionMiddleware(updateVideoChannel) |
49 | ) | 49 | ) |
50 | 50 | ||
51 | videoChannelRouter.delete('/:id', | 51 | videoChannelRouter.delete('/:id', |
52 | authenticate, | 52 | authenticate, |
53 | asyncMiddleware(videoChannelsRemoveValidator), | 53 | asyncMiddleware(videoChannelsRemoveValidator), |
54 | asyncMiddleware(removeVideoChannelRetryWrapper) | 54 | asyncRetryTransactionMiddleware(removeVideoChannel) |
55 | ) | 55 | ) |
56 | 56 | ||
57 | videoChannelRouter.get('/:id', | 57 | videoChannelRouter.get('/:id', |
@@ -83,23 +83,6 @@ async function listVideoChannels (req: express.Request, res: express.Response, n | |||
83 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | 83 | return res.json(getFormattedObjects(resultList.data, resultList.total)) |
84 | } | 84 | } |
85 | 85 | ||
86 | // Wrapper to video channel add that retry the async function if there is a database error | ||
87 | // We need this because we run the transaction in SERIALIZABLE isolation that can fail | ||
88 | async function addVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
89 | const options = { | ||
90 | arguments: [ req, res ], | ||
91 | errorMessage: 'Cannot insert the video video channel with many retries.' | ||
92 | } | ||
93 | |||
94 | const videoChannel = await retryTransactionWrapper(addVideoChannel, options) | ||
95 | return res.json({ | ||
96 | videoChannel: { | ||
97 | id: videoChannel.id, | ||
98 | uuid: videoChannel.Actor.uuid | ||
99 | } | ||
100 | }).end() | ||
101 | } | ||
102 | |||
103 | async function addVideoChannel (req: express.Request, res: express.Response) { | 86 | async function addVideoChannel (req: express.Request, res: express.Response) { |
104 | const videoChannelInfo: VideoChannelCreate = req.body | 87 | const videoChannelInfo: VideoChannelCreate = req.body |
105 | const account: AccountModel = res.locals.oauth.token.User.Account | 88 | const account: AccountModel = res.locals.oauth.token.User.Account |
@@ -113,18 +96,12 @@ async function addVideoChannel (req: express.Request, res: express.Response) { | |||
113 | 96 | ||
114 | logger.info('Video channel with uuid %s created.', videoChannelCreated.Actor.uuid) | 97 | logger.info('Video channel with uuid %s created.', videoChannelCreated.Actor.uuid) |
115 | 98 | ||
116 | return videoChannelCreated | 99 | return res.json({ |
117 | } | 100 | videoChannel: { |
118 | 101 | id: videoChannelCreated.id, | |
119 | async function updateVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { | 102 | uuid: videoChannelCreated.Actor.uuid |
120 | const options = { | 103 | } |
121 | arguments: [ req, res ], | 104 | }).end() |
122 | errorMessage: 'Cannot update the video with many retries.' | ||
123 | } | ||
124 | |||
125 | await retryTransactionWrapper(updateVideoChannel, options) | ||
126 | |||
127 | return res.type('json').status(204).end() | ||
128 | } | 105 | } |
129 | 106 | ||
130 | async function updateVideoChannel (req: express.Request, res: express.Response) { | 107 | async function updateVideoChannel (req: express.Request, res: express.Response) { |
@@ -157,15 +134,6 @@ async function updateVideoChannel (req: express.Request, res: express.Response) | |||
157 | 134 | ||
158 | throw err | 135 | throw err |
159 | } | 136 | } |
160 | } | ||
161 | |||
162 | async function removeVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
163 | const options = { | ||
164 | arguments: [ req, res ], | ||
165 | errorMessage: 'Cannot remove the video channel with many retries.' | ||
166 | } | ||
167 | |||
168 | await retryTransactionWrapper(removeVideoChannel, options) | ||
169 | 137 | ||
170 | return res.type('json').status(204).end() | 138 | return res.type('json').status(204).end() |
171 | } | 139 | } |
@@ -173,12 +141,13 @@ async function removeVideoChannelRetryWrapper (req: express.Request, res: expres | |||
173 | async function removeVideoChannel (req: express.Request, res: express.Response) { | 141 | async function removeVideoChannel (req: express.Request, res: express.Response) { |
174 | const videoChannelInstance: VideoChannelModel = res.locals.videoChannel | 142 | const videoChannelInstance: VideoChannelModel = res.locals.videoChannel |
175 | 143 | ||
176 | return sequelizeTypescript.transaction(async t => { | 144 | await sequelizeTypescript.transaction(async t => { |
177 | await videoChannelInstance.destroy({ transaction: t }) | 145 | await videoChannelInstance.destroy({ transaction: t }) |
178 | 146 | ||
179 | logger.info('Video channel with name %s and uuid %s deleted.', videoChannelInstance.name, videoChannelInstance.Actor.uuid) | 147 | logger.info('Video channel with name %s and uuid %s deleted.', videoChannelInstance.name, videoChannelInstance.Actor.uuid) |
180 | }) | 148 | }) |
181 | 149 | ||
150 | return res.type('json').status(204).end() | ||
182 | } | 151 | } |
183 | 152 | ||
184 | async function getVideoChannel (req: express.Request, res: express.Response, next: express.NextFunction) { | 153 | async function getVideoChannel (req: express.Request, res: express.Response, next: express.NextFunction) { |