aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/video-channel.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-06-13 14:27:40 +0200
committerChocobozzz <me@florianbigard.com>2018-06-13 14:27:40 +0200
commit90d4bb8125e80c8060416d4d135ddeaf0a622ede (patch)
treeb3b7181329a08ecc930b54fe7b48095c4155393c /server/controllers/api/video-channel.ts
parent3cd0734fd9b0ff21aaef02317a874e8f1c06e027 (diff)
downloadPeerTube-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.ts55
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'
2import { getFormattedObjects, resetSequelizeInstance } from '../../helpers/utils' 2import { getFormattedObjects, resetSequelizeInstance } from '../../helpers/utils'
3import { 3import {
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'
20import { createVideoChannel } from '../../lib/video-channel' 21import { createVideoChannel } from '../../lib/video-channel'
21import { isNSFWHidden } from '../../helpers/express-utils' 22import { isNSFWHidden } from '../../helpers/express-utils'
22import { setAsyncActorKeys } from '../../lib/activitypub' 23import { setAsyncActorKeys } from '../../lib/activitypub'
23import { retryTransactionWrapper } from '../../helpers/database-utils'
24import { AccountModel } from '../../models/account/account' 24import { AccountModel } from '../../models/account/account'
25import { sequelizeTypescript } from '../../initializers' 25import { sequelizeTypescript } from '../../initializers'
26import { logger } from '../../helpers/logger' 26import { logger } from '../../helpers/logger'
@@ -39,19 +39,19 @@ videoChannelRouter.get('/',
39videoChannelRouter.post('/', 39videoChannelRouter.post('/',
40 authenticate, 40 authenticate,
41 videoChannelsAddValidator, 41 videoChannelsAddValidator,
42 asyncMiddleware(addVideoChannelRetryWrapper) 42 asyncRetryTransactionMiddleware(addVideoChannel)
43) 43)
44 44
45videoChannelRouter.put('/:id', 45videoChannelRouter.put('/:id',
46 authenticate, 46 authenticate,
47 asyncMiddleware(videoChannelsUpdateValidator), 47 asyncMiddleware(videoChannelsUpdateValidator),
48 updateVideoChannelRetryWrapper 48 asyncRetryTransactionMiddleware(updateVideoChannel)
49) 49)
50 50
51videoChannelRouter.delete('/:id', 51videoChannelRouter.delete('/:id',
52 authenticate, 52 authenticate,
53 asyncMiddleware(videoChannelsRemoveValidator), 53 asyncMiddleware(videoChannelsRemoveValidator),
54 asyncMiddleware(removeVideoChannelRetryWrapper) 54 asyncRetryTransactionMiddleware(removeVideoChannel)
55) 55)
56 56
57videoChannelRouter.get('/:id', 57videoChannelRouter.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
88async 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
103async function addVideoChannel (req: express.Request, res: express.Response) { 86async 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,
119async 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
130async function updateVideoChannel (req: express.Request, res: express.Response) { 107async 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
162async 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
173async function removeVideoChannel (req: express.Request, res: express.Response) { 141async 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
184async function getVideoChannel (req: express.Request, res: express.Response, next: express.NextFunction) { 153async function getVideoChannel (req: express.Request, res: express.Response, next: express.NextFunction) {