From e11f68a3562d2468480c396f47f1bdd2a306e17a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 8 Dec 2017 10:08:36 +0100 Subject: Optimise transaction for video upload --- server/controllers/api/videos/index.ts | 87 +++++++++++++++++----------------- 1 file changed, 43 insertions(+), 44 deletions(-) (limited to 'server/controllers') diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index f427a25c0..0f71a7f7f 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts @@ -164,52 +164,54 @@ async function addVideoRetryWrapper (req: express.Request, res: express.Response }).end() } -function addVideo (req: express.Request, res: express.Response, videoPhysicalFile: Express.Multer.File) { +async function addVideo (req: express.Request, res: express.Response, videoPhysicalFile: Express.Multer.File) { const videoInfo: VideoCreate = req.body - return db.sequelize.transaction(async t => { - const sequelizeOptions = { transaction: t } + // Prepare data so we don't block the transaction + const videoData = { + name: videoInfo.name, + remote: false, + extname: extname(videoPhysicalFile.filename), + category: videoInfo.category, + licence: videoInfo.licence, + language: videoInfo.language, + nsfw: videoInfo.nsfw, + description: videoInfo.description, + privacy: videoInfo.privacy, + duration: videoPhysicalFile['duration'], // duration was added by a previous middleware + channelId: res.locals.videoChannel.id + } + const video = db.Video.build(videoData) + video.url = getVideoActivityPubUrl(video) - const videoData = { - name: videoInfo.name, - remote: false, - extname: extname(videoPhysicalFile.filename), - category: videoInfo.category, - licence: videoInfo.licence, - language: videoInfo.language, - nsfw: videoInfo.nsfw, - description: videoInfo.description, - privacy: videoInfo.privacy, - duration: videoPhysicalFile['duration'], // duration was added by a previous middleware - channelId: res.locals.videoChannel.id - } - const video = db.Video.build(videoData) - video.url = getVideoActivityPubUrl(video) + const videoFilePath = join(CONFIG.STORAGE.VIDEOS_DIR, videoPhysicalFile.filename) + const videoFileHeight = await getVideoFileHeight(videoFilePath) - const videoFilePath = join(CONFIG.STORAGE.VIDEOS_DIR, videoPhysicalFile.filename) - const videoFileHeight = await getVideoFileHeight(videoFilePath) + const videoFileData = { + extname: extname(videoPhysicalFile.filename), + resolution: videoFileHeight, + size: videoPhysicalFile.size + } + const videoFile = db.VideoFile.build(videoFileData) + const videoDir = CONFIG.STORAGE.VIDEOS_DIR + const source = join(videoDir, videoPhysicalFile.filename) + const destination = join(videoDir, video.getVideoFilename(videoFile)) - const videoFileData = { - extname: extname(videoPhysicalFile.filename), - resolution: videoFileHeight, - size: videoPhysicalFile.size - } - const videoFile = db.VideoFile.build(videoFileData) - const videoDir = CONFIG.STORAGE.VIDEOS_DIR - const source = join(videoDir, videoPhysicalFile.filename) - const destination = join(videoDir, video.getVideoFilename(videoFile)) + await renamePromise(source, destination) + // This is important in case if there is another attempt in the retry process + videoPhysicalFile.filename = video.getVideoFilename(videoFile) - await renamePromise(source, destination) - // This is important in case if there is another attempt in the retry process - videoPhysicalFile.filename = video.getVideoFilename(videoFile) + const tasks = [] - const tasks = [] + tasks.push( + video.createTorrentAndSetInfoHash(videoFile), + video.createThumbnail(videoFile), + video.createPreview(videoFile) + ) + await Promise.all(tasks) - tasks.push( - video.createTorrentAndSetInfoHash(videoFile), - video.createThumbnail(videoFile), - video.createPreview(videoFile) - ) + return db.sequelize.transaction(async t => { + const sequelizeOptions = { transaction: t } if (CONFIG.TRANSCODING.ENABLED === true) { // Put uuid because we don't have id auto incremented for now @@ -217,20 +219,17 @@ function addVideo (req: express.Request, res: express.Response, videoPhysicalFil videoUUID: video.uuid } - tasks.push( - transcodingJobScheduler.createJob(t, 'videoFileOptimizer', dataInput) - ) + await transcodingJobScheduler.createJob(t, 'videoFileOptimizer', dataInput) } - await Promise.all(tasks) const videoCreated = await video.save(sequelizeOptions) // Do not forget to add video channel information to the created video videoCreated.VideoChannel = res.locals.videoChannel videoFile.videoId = video.id - await videoFile.save(sequelizeOptions) - video.VideoFiles = [videoFile] + + video.VideoFiles = [ videoFile ] if (videoInfo.tags) { const tagInstances = await db.Tag.findOrCreateTags(videoInfo.tags, t) -- cgit v1.2.3