]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/jobs/transcoding-job-scheduler/video-file-transcoder-handler.ts
Refractor activity pub lib/helpers
[github/Chocobozzz/PeerTube.git] / server / lib / jobs / transcoding-job-scheduler / video-file-transcoder-handler.ts
1 import { VideoResolution } from '../../../../shared'
2 import { logger } from '../../../helpers'
3 import { database as db } from '../../../initializers/database'
4 import { VideoInstance } from '../../../models'
5 import { sendUpdateVideo } from '../../activitypub/send/send-update'
6
7 async function process (data: { videoUUID: string, resolution: VideoResolution }, jobId: number) {
8 const video = await db.Video.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
20 function onError (err: Error, jobId: number) {
21 logger.error('Error when transcoding video file in job %d.', jobId, err)
22 return Promise.resolve()
23 }
24
25 async function onSuccess (jobId: number, video: VideoInstance) {
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 db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid)
32 // Video does not exist anymore
33 if (!videoDatabase) return undefined
34
35 await sendUpdateVideo(video, undefined)
36
37 return undefined
38 }
39
40 // ---------------------------------------------------------------------------
41
42 export {
43 process,
44 onError,
45 onSuccess
46 }