From f5028693a896a3076dd286ac0030e3d8f78f5ebf Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 25 Oct 2017 16:03:33 +0200 Subject: Use async/await in lib and initializers --- server/lib/jobs/handlers/video-file-optimizer.ts | 88 +++++++++++------------ server/lib/jobs/handlers/video-file-transcoder.ts | 21 +++--- 2 files changed, 52 insertions(+), 57 deletions(-) (limited to 'server/lib/jobs/handlers') diff --git a/server/lib/jobs/handlers/video-file-optimizer.ts b/server/lib/jobs/handlers/video-file-optimizer.ts index 63a51064c..799ba8b01 100644 --- a/server/lib/jobs/handlers/video-file-optimizer.ts +++ b/server/lib/jobs/handlers/video-file-optimizer.ts @@ -1,4 +1,4 @@ -import * as Promise from 'bluebird' +import * as Bluebird from 'bluebird' import { database as db } from '../../../initializers/database' import { logger, computeResolutionsToTranscode } from '../../../helpers' @@ -6,16 +6,17 @@ import { VideoInstance } from '../../../models' import { addVideoToFriends } from '../../friends' import { JobScheduler } from '../job-scheduler' -function process (data: { videoUUID: string }, jobId: number) { - return db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(data.videoUUID).then(video => { - // No video, maybe deleted? - if (!video) { - logger.info('Do not process job %d, video does not exist.', jobId, { videoUUID: video.uuid }) - return undefined - } +async function process (data: { videoUUID: string }, jobId: number) { + const video = await db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(data.videoUUID) + // No video, maybe deleted? + if (!video) { + logger.info('Do not process job %d, video does not exist.', jobId, { videoUUID: video.uuid }) + return undefined + } + + await video.optimizeOriginalVideofile() - return video.optimizeOriginalVideofile().then(() => video) - }) + return video } function onError (err: Error, jobId: number) { @@ -23,33 +24,31 @@ function onError (err: Error, jobId: number) { return Promise.resolve() } -function onSuccess (jobId: number, video: VideoInstance) { +async function onSuccess (jobId: number, video: VideoInstance) { if (video === undefined) return undefined logger.info('Job %d is a success.', jobId) - video.toAddRemoteJSON() - .then(remoteVideo => { - // Now we'll add the video's meta data to our friends - return addVideoToFriends(remoteVideo, null) - }) - .then(() => { - return video.getOriginalFileHeight() - }) - .then(originalFileHeight => { - // Create transcoding jobs if there are enabled resolutions - const resolutionsEnabled = computeResolutionsToTranscode(originalFileHeight) - logger.info( - 'Resolutions computed for video %s and origin file height of %d.', video.uuid, originalFileHeight, - { resolutions: resolutionsEnabled } - ) - - if (resolutionsEnabled.length === 0) return undefined - - return db.sequelize.transaction(t => { - const tasks: Promise[] = [] - - resolutionsEnabled.forEach(resolution => { + const remoteVideo = await video.toAddRemoteJSON() + + // Now we'll add the video's meta data to our friends + await addVideoToFriends(remoteVideo, null) + + const originalFileHeight = await video.getOriginalFileHeight() + // Create transcoding jobs if there are enabled resolutions + + const resolutionsEnabled = computeResolutionsToTranscode(originalFileHeight) + logger.info( + 'Resolutions computed for video %s and origin file height of %d.', video.uuid, originalFileHeight, + { resolutions: resolutionsEnabled } + ) + + if (resolutionsEnabled.length !== 0) { + try { + await db.sequelize.transaction(async t => { + const tasks: Bluebird[] = [] + + for (const resolution of resolutionsEnabled) { const dataInput = { videoUUID: video.uuid, resolution @@ -57,24 +56,19 @@ function onSuccess (jobId: number, video: VideoInstance) { const p = JobScheduler.Instance.createJob(t, 'videoFileTranscoder', dataInput) tasks.push(p) - }) + } - return Promise.all(tasks).then(() => resolutionsEnabled) + await Promise.all(tasks) }) - }) - .then(resolutionsEnabled => { - if (resolutionsEnabled === undefined) { - logger.info('No transcoding jobs created for video %s (no resolutions enabled).') - return undefined - } logger.info('Transcoding jobs created for uuid %s.', video.uuid, { resolutionsEnabled }) - }) - .catch((err: Error) => { - logger.debug('Cannot transcode the video.', err) - throw err - }) - + } catch (err) { + logger.warn('Cannot transcode the video.', err) + } + } else { + logger.info('No transcoding jobs created for video %s (no resolutions enabled).') + return undefined + } } // --------------------------------------------------------------------------- diff --git a/server/lib/jobs/handlers/video-file-transcoder.ts b/server/lib/jobs/handlers/video-file-transcoder.ts index 0dafee566..b240ff58a 100644 --- a/server/lib/jobs/handlers/video-file-transcoder.ts +++ b/server/lib/jobs/handlers/video-file-transcoder.ts @@ -4,16 +4,17 @@ import { logger } from '../../../helpers' import { VideoInstance } from '../../../models' import { VideoResolution } from '../../../../shared' -function process (data: { videoUUID: string, resolution: VideoResolution }, jobId: number) { - return db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(data.videoUUID).then(video => { - // No video, maybe deleted? - if (!video) { - logger.info('Do not process job %d, video does not exist.', jobId, { videoUUID: video.uuid }) - return undefined - } - - return video.transcodeOriginalVideofile(data.resolution).then(() => video) - }) +async function process (data: { videoUUID: string, resolution: VideoResolution }, jobId: number) { + const video = await db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(data.videoUUID) + // No video, maybe deleted? + if (!video) { + logger.info('Do not process job %d, video does not exist.', jobId, { videoUUID: video.uuid }) + return undefined + } + + await video.transcodeOriginalVideofile(data.resolution) + + return video } function onError (err: Error, jobId: number) { -- cgit v1.2.3