From 031094f7992366d8d1f6583b205f984ffadf637a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 17 Oct 2017 15:37:40 +0200 Subject: More robust transcoding jobs --- server/lib/jobs/handlers/index.ts | 2 +- server/lib/jobs/handlers/video-file-optimizer.ts | 10 +++++++++- server/lib/jobs/handlers/video-file-transcoder.ts | 10 +++++++++- server/lib/jobs/job-scheduler.ts | 2 +- server/tests/api/multiple-pods.ts | 6 +++--- 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/server/lib/jobs/handlers/index.ts b/server/lib/jobs/handlers/index.ts index 5941427a1..cef1f89a9 100644 --- a/server/lib/jobs/handlers/index.ts +++ b/server/lib/jobs/handlers/index.ts @@ -2,7 +2,7 @@ import * as videoFileOptimizer from './video-file-optimizer' import * as videoFileTranscoder from './video-file-transcoder' export interface JobHandler { - process (data: object): T + process (data: object, jobId: number): T onError (err: Error, jobId: number) onSuccess (jobId: number, jobResult: T) } diff --git a/server/lib/jobs/handlers/video-file-optimizer.ts b/server/lib/jobs/handlers/video-file-optimizer.ts index a87ce52dc..63a51064c 100644 --- a/server/lib/jobs/handlers/video-file-optimizer.ts +++ b/server/lib/jobs/handlers/video-file-optimizer.ts @@ -6,8 +6,14 @@ import { VideoInstance } from '../../../models' import { addVideoToFriends } from '../../friends' import { JobScheduler } from '../job-scheduler' -function process (data: { videoUUID: string }) { +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 + } + return video.optimizeOriginalVideofile().then(() => video) }) } @@ -18,6 +24,8 @@ function onError (err: Error, jobId: number) { } function onSuccess (jobId: number, video: VideoInstance) { + if (video === undefined) return undefined + logger.info('Job %d is a success.', jobId) video.toAddRemoteJSON() diff --git a/server/lib/jobs/handlers/video-file-transcoder.ts b/server/lib/jobs/handlers/video-file-transcoder.ts index 0e45b4dca..0dafee566 100644 --- a/server/lib/jobs/handlers/video-file-transcoder.ts +++ b/server/lib/jobs/handlers/video-file-transcoder.ts @@ -4,8 +4,14 @@ import { logger } from '../../../helpers' import { VideoInstance } from '../../../models' import { VideoResolution } from '../../../../shared' -function process (data: { videoUUID: string, resolution: VideoResolution }) { +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) }) } @@ -16,6 +22,8 @@ function onError (err: Error, jobId: number) { } function onSuccess (jobId: number, video: VideoInstance) { + if (video === undefined) return undefined + logger.info('Job %d is a success.', jobId) const remoteVideo = video.toUpdateRemoteJSON() diff --git a/server/lib/jobs/job-scheduler.ts b/server/lib/jobs/job-scheduler.ts index 134d270c0..c2409d20c 100644 --- a/server/lib/jobs/job-scheduler.ts +++ b/server/lib/jobs/job-scheduler.ts @@ -87,7 +87,7 @@ class JobScheduler { job.state = JOB_STATES.PROCESSING return job.save() .then(() => { - return jobHandler.process(job.handlerInputData) + return jobHandler.process(job.handlerInputData, job.id) }) .then( result => { diff --git a/server/tests/api/multiple-pods.ts b/server/tests/api/multiple-pods.ts index 8b60ac0f4..6c11aace5 100644 --- a/server/tests/api/multiple-pods.ts +++ b/server/tests/api/multiple-pods.ts @@ -195,17 +195,17 @@ describe('Test multiple pods', function () { const file240p = video.files.find(f => f.resolution === 240) expect(file240p).not.to.be.undefined expect(file240p.resolutionLabel).to.equal('240p') - expect(file240p.size).to.be.above(130000).and.below(150000) + expect(file240p.size).to.be.above(180000).and.below(200000) const file360p = video.files.find(f => f.resolution === 360) expect(file360p).not.to.be.undefined expect(file360p.resolutionLabel).to.equal('360p') - expect(file360p.size).to.be.above(160000).and.below(180000) + expect(file360p.size).to.be.above(270000).and.below(290000) const file480p = video.files.find(f => f.resolution === 480) expect(file480p).not.to.be.undefined expect(file480p.resolutionLabel).to.equal('480p') - expect(file480p.size).to.be.above(200000).and.below(220000) + expect(file480p.size).to.be.above(380000).and.below(400000) const file720p = video.files.find(f => f.resolution === 720) expect(file720p).not.to.be.undefined -- cgit v1.2.3