Fix concurrency error when deleting a video
This commit is contained in:
parent
6d33593a08
commit
91f6f169b1
|
@ -45,6 +45,7 @@ import { VideoCreate, VideoUpdate } from '../../../../shared'
|
||||||
import { abuseVideoRouter } from './abuse'
|
import { abuseVideoRouter } from './abuse'
|
||||||
import { blacklistRouter } from './blacklist'
|
import { blacklistRouter } from './blacklist'
|
||||||
import { rateVideoRouter } from './rate'
|
import { rateVideoRouter } from './rate'
|
||||||
|
import { VideoInstance } from '../../../models/video/video-interface'
|
||||||
|
|
||||||
const videosRouter = express.Router()
|
const videosRouter = express.Router()
|
||||||
|
|
||||||
|
@ -106,7 +107,7 @@ videosRouter.get('/:id',
|
||||||
videosRouter.delete('/:id',
|
videosRouter.delete('/:id',
|
||||||
authenticate,
|
authenticate,
|
||||||
videosRemoveValidator,
|
videosRemoveValidator,
|
||||||
removeVideo
|
removeVideoRetryWrapper
|
||||||
)
|
)
|
||||||
|
|
||||||
videosRouter.get('/search/:value',
|
videosRouter.get('/search/:value',
|
||||||
|
@ -291,7 +292,6 @@ function updateVideoRetryWrapper (req: express.Request, res: express.Response, n
|
||||||
|
|
||||||
retryTransactionWrapper(updateVideo, options)
|
retryTransactionWrapper(updateVideo, options)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
// TODO : include Location of the new video -> 201
|
|
||||||
return res.type('json').status(204).end()
|
return res.type('json').status(204).end()
|
||||||
})
|
})
|
||||||
.catch(err => next(err))
|
.catch(err => next(err))
|
||||||
|
@ -396,17 +396,31 @@ function listVideos (req: express.Request, res: express.Response, next: express.
|
||||||
.catch(err => next(err))
|
.catch(err => next(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeVideo (req: express.Request, res: express.Response, next: express.NextFunction) {
|
function removeVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
|
||||||
const videoInstance = res.locals.video
|
const options = {
|
||||||
|
arguments: [ req, res ],
|
||||||
|
errorMessage: 'Cannot remove the video with many retries.'
|
||||||
|
}
|
||||||
|
|
||||||
videoInstance.destroy()
|
retryTransactionWrapper(removeVideo, options)
|
||||||
|
.then(() => {
|
||||||
|
return res.type('json').status(204).end()
|
||||||
|
})
|
||||||
|
.catch(err => next(err))
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeVideo (req: express.Request, res: express.Response) {
|
||||||
|
const videoInstance: VideoInstance = res.locals.video
|
||||||
|
|
||||||
|
return db.sequelize.transaction(t => {
|
||||||
|
return videoInstance.destroy({ transaction: t })
|
||||||
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid)
|
logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid)
|
||||||
res.type('json').status(204).end()
|
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
logger.error('Errors when removed the video.', err)
|
logger.error('Errors when removed the video.', err)
|
||||||
return next(err)
|
throw err
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -80,12 +80,12 @@ function updateVideoToFriends (videoData: RemoteVideoUpdateData, transaction: Se
|
||||||
return createRequest(options)
|
return createRequest(options)
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeVideoToFriends (videoParams: RemoteVideoRemoveData) {
|
function removeVideoToFriends (videoParams: RemoteVideoRemoveData, transaction: Sequelize.Transaction) {
|
||||||
const options = {
|
const options = {
|
||||||
type: ENDPOINT_ACTIONS.REMOVE,
|
type: ENDPOINT_ACTIONS.REMOVE,
|
||||||
endpoint: REQUEST_ENDPOINTS.VIDEOS,
|
endpoint: REQUEST_ENDPOINTS.VIDEOS,
|
||||||
data: videoParams,
|
data: videoParams,
|
||||||
transaction: null
|
transaction
|
||||||
}
|
}
|
||||||
return createRequest(options)
|
return createRequest(options)
|
||||||
}
|
}
|
||||||
|
|
|
@ -300,7 +300,7 @@ function associate (models) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function afterDestroy (video: VideoInstance) {
|
function afterDestroy (video: VideoInstance, options: { transaction: Sequelize.Transaction }) {
|
||||||
const tasks = []
|
const tasks = []
|
||||||
|
|
||||||
tasks.push(
|
tasks.push(
|
||||||
|
@ -314,10 +314,10 @@ function afterDestroy (video: VideoInstance) {
|
||||||
|
|
||||||
tasks.push(
|
tasks.push(
|
||||||
video.removePreview(),
|
video.removePreview(),
|
||||||
removeVideoToFriends(removeVideoToFriendsParams)
|
removeVideoToFriends(removeVideoToFriendsParams, options.transaction)
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: check files is populated
|
// Remove physical files and torrents
|
||||||
video.VideoFiles.forEach(file => {
|
video.VideoFiles.forEach(file => {
|
||||||
video.removeFile(file),
|
video.removeFile(file),
|
||||||
video.removeTorrent(file)
|
video.removeTorrent(file)
|
||||||
|
|
Loading…
Reference in New Issue