From 6fcd19ba737f1f5614a56c6925adb882dea43b8d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 5 Jul 2017 13:26:25 +0200 Subject: Move to promises Closes https://github.com/Chocobozzz/PeerTube/issues/74 --- server/lib/jobs/handlers/index.ts | 8 +++----- server/lib/jobs/handlers/video-transcoder.ts | 22 ++++++++-------------- 2 files changed, 11 insertions(+), 19 deletions(-) (limited to 'server/lib/jobs/handlers') diff --git a/server/lib/jobs/handlers/index.ts b/server/lib/jobs/handlers/index.ts index 7d0263b15..8abddae35 100644 --- a/server/lib/jobs/handlers/index.ts +++ b/server/lib/jobs/handlers/index.ts @@ -1,11 +1,9 @@ import * as videoTranscoder from './video-transcoder' -import { VideoInstance } from '../../../models' - export interface JobHandler { - process (data: object, callback: (err: Error, videoInstance?: T) => void) - onError (err: Error, jobId: number, video: T, callback: (err: Error) => void) - onSuccess (data: any, jobId: number, video: T, callback: (err: Error) => void) + process (data: object): T + onError (err: Error, jobId: number) + onSuccess (jobId: number, jobResult: T) } const jobHandlers: { [ handlerName: string ]: JobHandler } = { diff --git a/server/lib/jobs/handlers/video-transcoder.ts b/server/lib/jobs/handlers/video-transcoder.ts index 6f606a7d3..e829ca813 100644 --- a/server/lib/jobs/handlers/video-transcoder.ts +++ b/server/lib/jobs/handlers/video-transcoder.ts @@ -3,29 +3,23 @@ import { logger } from '../../../helpers' import { addVideoToFriends } from '../../../lib' import { VideoInstance } from '../../../models' -function process (data: { id: string }, callback: (err: Error, videoInstance?: VideoInstance) => void) { - db.Video.loadAndPopulateAuthorAndPodAndTags(data.id, function (err, video) { - if (err) return callback(err) - - video.transcodeVideofile(function (err) { - return callback(err, video) - }) +function process (data: { id: string }) { + return db.Video.loadAndPopulateAuthorAndPodAndTags(data.id).then(video => { + return video.transcodeVideofile().then(() => video) }) } -function onError (err: Error, jobId: number, video: VideoInstance, callback: (err: Error) => void) { +function onError (err: Error, jobId: number) { logger.error('Error when transcoding video file in job %d.', jobId, { error: err }) - return callback(null) + return Promise.resolve() } -function onSuccess (data: any, jobId: number, video: VideoInstance, callback: (err: Error) => void) { +function onSuccess (jobId: number, video: VideoInstance) { logger.info('Job %d is a success.', jobId) - video.toAddRemoteJSON(function (err, remoteVideo) { - if (err) return callback(err) - + video.toAddRemoteJSON().then(remoteVideo => { // Now we'll add the video's meta data to our friends - addVideoToFriends(remoteVideo, null, callback) + return addVideoToFriends(remoteVideo, null) }) } -- cgit v1.2.3