aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/jobs/transcoding-job-scheduler
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-01-25 15:05:18 +0100
committerChocobozzz <me@florianbigard.com>2018-01-25 18:41:17 +0100
commit94a5ff8a4a75d75bb9df542a39ce8769e7a7e6a4 (patch)
tree32a9148e0e4567f0c4ffae0412cbed20b84e8873 /server/lib/jobs/transcoding-job-scheduler
parentd765fafc3faf0db9818eb1a07161df1cb1bc0efa (diff)
downloadPeerTube-94a5ff8a4a75d75bb9df542a39ce8769e7a7e6a4.tar.gz
PeerTube-94a5ff8a4a75d75bb9df542a39ce8769e7a7e6a4.tar.zst
PeerTube-94a5ff8a4a75d75bb9df542a39ce8769e7a7e6a4.zip
Move job queue to redis
We'll use it as cache in the future. /!\ You'll loose your old jobs (pending jobs too) so upgrade only when you don't have pending job anymore.
Diffstat (limited to 'server/lib/jobs/transcoding-job-scheduler')
-rw-r--r--server/lib/jobs/transcoding-job-scheduler/index.ts1
-rw-r--r--server/lib/jobs/transcoding-job-scheduler/transcoding-job-scheduler.ts23
-rw-r--r--server/lib/jobs/transcoding-job-scheduler/video-file-optimizer-handler.ts90
-rw-r--r--server/lib/jobs/transcoding-job-scheduler/video-file-transcoder-handler.ts48
4 files changed, 0 insertions, 162 deletions
diff --git a/server/lib/jobs/transcoding-job-scheduler/index.ts b/server/lib/jobs/transcoding-job-scheduler/index.ts
deleted file mode 100644
index 73152a1be..000000000
--- a/server/lib/jobs/transcoding-job-scheduler/index.ts
+++ /dev/null
@@ -1 +0,0 @@
1export * from './transcoding-job-scheduler'
diff --git a/server/lib/jobs/transcoding-job-scheduler/transcoding-job-scheduler.ts b/server/lib/jobs/transcoding-job-scheduler/transcoding-job-scheduler.ts
deleted file mode 100644
index e5530a73c..000000000
--- a/server/lib/jobs/transcoding-job-scheduler/transcoding-job-scheduler.ts
+++ /dev/null
@@ -1,23 +0,0 @@
1import { JobCategory } from '../../../../shared'
2import { VideoModel } from '../../../models/video/video'
3import { JobHandler, JobScheduler } from '../job-scheduler'
4
5import * as videoFileOptimizer from './video-file-optimizer-handler'
6import * as videoFileTranscoder from './video-file-transcoder-handler'
7
8type TranscodingJobPayload = {
9 videoUUID: string
10 resolution?: number
11}
12const jobHandlers: { [ handlerName: string ]: JobHandler<TranscodingJobPayload, VideoModel> } = {
13 videoFileOptimizer,
14 videoFileTranscoder
15}
16const jobCategory: JobCategory = 'transcoding'
17
18const transcodingJobScheduler = new JobScheduler(jobCategory, jobHandlers)
19
20export {
21 TranscodingJobPayload,
22 transcodingJobScheduler
23}
diff --git a/server/lib/jobs/transcoding-job-scheduler/video-file-optimizer-handler.ts b/server/lib/jobs/transcoding-job-scheduler/video-file-optimizer-handler.ts
deleted file mode 100644
index f224a31b4..000000000
--- a/server/lib/jobs/transcoding-job-scheduler/video-file-optimizer-handler.ts
+++ /dev/null
@@ -1,90 +0,0 @@
1import * as Bluebird from 'bluebird'
2import { VideoPrivacy } from '../../../../shared/models/videos'
3import { logger } from '../../../helpers/logger'
4import { computeResolutionsToTranscode } from '../../../helpers/utils'
5import { sequelizeTypescript } from '../../../initializers'
6import { JobModel } from '../../../models/job/job'
7import { VideoModel } from '../../../models/video/video'
8import { shareVideoByServerAndChannel } from '../../activitypub'
9import { sendCreateVideo } from '../../activitypub/send'
10import { JobScheduler } from '../job-scheduler'
11import { TranscodingJobPayload } from './transcoding-job-scheduler'
12
13async function process (data: TranscodingJobPayload, jobId: number) {
14 const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(data.videoUUID)
15 // No video, maybe deleted?
16 if (!video) {
17 logger.info('Do not process job %d, video does not exist.', jobId, { videoUUID: video.uuid })
18 return undefined
19 }
20
21 await video.optimizeOriginalVideofile()
22
23 return video
24}
25
26function onError (err: Error, jobId: number) {
27 logger.error('Error when optimized video file in job %d.', jobId, err)
28 return Promise.resolve()
29}
30
31async function onSuccess (jobId: number, video: VideoModel, jobScheduler: JobScheduler<TranscodingJobPayload, VideoModel>) {
32 if (video === undefined) return undefined
33
34 logger.info('Job %d is a success.', jobId)
35
36 // Maybe the video changed in database, refresh it
37 const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid)
38 // Video does not exist anymore
39 if (!videoDatabase) return undefined
40
41 if (video.privacy !== VideoPrivacy.PRIVATE) {
42 // Now we'll add the video's meta data to our followers
43 await sendCreateVideo(video, undefined)
44 await shareVideoByServerAndChannel(video, undefined)
45 }
46
47 const originalFileHeight = await videoDatabase.getOriginalFileHeight()
48
49 // Create transcoding jobs if there are enabled resolutions
50 const resolutionsEnabled = computeResolutionsToTranscode(originalFileHeight)
51 logger.info(
52 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, originalFileHeight,
53 { resolutions: resolutionsEnabled }
54 )
55
56 if (resolutionsEnabled.length !== 0) {
57 try {
58 await sequelizeTypescript.transaction(async t => {
59 const tasks: Bluebird<JobModel>[] = []
60
61 for (const resolution of resolutionsEnabled) {
62 const dataInput = {
63 videoUUID: videoDatabase.uuid,
64 resolution
65 }
66
67 const p = jobScheduler.createJob(t, 'videoFileTranscoder', dataInput)
68 tasks.push(p)
69 }
70
71 await Promise.all(tasks)
72 })
73
74 logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled })
75 } catch (err) {
76 logger.warn('Cannot transcode the video.', err)
77 }
78 } else {
79 logger.info('No transcoding jobs created for video %s (no resolutions enabled).')
80 return undefined
81 }
82}
83
84// ---------------------------------------------------------------------------
85
86export {
87 process,
88 onError,
89 onSuccess
90}
diff --git a/server/lib/jobs/transcoding-job-scheduler/video-file-transcoder-handler.ts b/server/lib/jobs/transcoding-job-scheduler/video-file-transcoder-handler.ts
deleted file mode 100644
index 883d3eba8..000000000
--- a/server/lib/jobs/transcoding-job-scheduler/video-file-transcoder-handler.ts
+++ /dev/null
@@ -1,48 +0,0 @@
1import { VideoResolution } from '../../../../shared'
2import { VideoPrivacy } from '../../../../shared/models/videos'
3import { logger } from '../../../helpers/logger'
4import { VideoModel } from '../../../models/video/video'
5import { sendUpdateVideo } from '../../activitypub/send'
6
7async function process (data: { videoUUID: string, resolution: VideoResolution }, jobId: number) {
8 const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(data.videoUUID)
9 // No video, maybe deleted?
10 if (!video) {
11 logger.info('Do not process job %d, video does not exist.', jobId, { videoUUID: video.uuid })
12 return undefined
13 }
14
15 await video.transcodeOriginalVideofile(data.resolution)
16
17 return video
18}
19
20function onError (err: Error, jobId: number) {
21 logger.error('Error when transcoding video file in job %d.', jobId, err)
22 return Promise.resolve()
23}
24
25async function onSuccess (jobId: number, video: VideoModel) {
26 if (video === undefined) return undefined
27
28 logger.info('Job %d is a success.', jobId)
29
30 // Maybe the video changed in database, refresh it
31 const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid)
32 // Video does not exist anymore
33 if (!videoDatabase) return undefined
34
35 if (video.privacy !== VideoPrivacy.PRIVATE) {
36 await sendUpdateVideo(video, undefined)
37 }
38
39 return undefined
40}
41
42// ---------------------------------------------------------------------------
43
44export {
45 process,
46 onError,
47 onSuccess
48}