]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/job-queue/handlers/video-file.ts
f5ad076a6d9e0d720dd83cfe61fea2bb7b4599c3
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-file.ts
1 import * as kue from 'kue'
2 import { VideoResolution, VideoState } from '../../../../shared'
3 import { logger } from '../../../helpers/logger'
4 import { computeResolutionsToTranscode } from '../../../helpers/utils'
5 import { VideoModel } from '../../../models/video/video'
6 import { JobQueue } from '../job-queue'
7 import { federateVideoIfNeeded } from '../../activitypub'
8 import { retryTransactionWrapper } from '../../../helpers/database-utils'
9 import { sequelizeTypescript } from '../../../initializers'
10
11 export type VideoFilePayload = {
12 videoUUID: string
13 isNewVideo?: boolean
14 resolution?: VideoResolution
15 isPortraitMode?: boolean
16 }
17
18 export type VideoFileImportPayload = {
19 videoUUID: string,
20 filePath: string
21 }
22
23 async function processVideoFileImport (job: kue.Job) {
24 const payload = job.data as VideoFileImportPayload
25 logger.info('Processing video file import in job %d.', job.id)
26
27 const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(payload.videoUUID)
28 // No video, maybe deleted?
29 if (!video) {
30 logger.info('Do not process job %d, video does not exist.', job.id, { videoUUID: video.uuid })
31 return undefined
32 }
33
34 await video.importVideoFile(payload.filePath)
35
36 await onVideoFileTranscoderOrImportSuccess(video)
37 return video
38 }
39
40 async function processVideoFile (job: kue.Job) {
41 const payload = job.data as VideoFilePayload
42 logger.info('Processing video file in job %d.', job.id)
43
44 const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(payload.videoUUID)
45 // No video, maybe deleted?
46 if (!video) {
47 logger.info('Do not process job %d, video does not exist.', job.id, { videoUUID: video.uuid })
48 return undefined
49 }
50
51 // Transcoding in other resolution
52 if (payload.resolution) {
53 await video.transcodeOriginalVideofile(payload.resolution, payload.isPortraitMode)
54
55 const options = {
56 arguments: [ video ],
57 errorMessage: 'Cannot execute onVideoFileTranscoderOrImportSuccess with many retries.'
58 }
59 await retryTransactionWrapper(onVideoFileTranscoderOrImportSuccess, options)
60 } else {
61 await video.optimizeOriginalVideofile()
62
63 const options = {
64 arguments: [ video, payload.isNewVideo ],
65 errorMessage: 'Cannot execute onVideoFileOptimizerSuccess with many retries.'
66 }
67 await retryTransactionWrapper(onVideoFileOptimizerSuccess, options)
68 }
69
70 return video
71 }
72
73 async function onVideoFileTranscoderOrImportSuccess (video: VideoModel) {
74 if (video === undefined) return undefined
75
76 return sequelizeTypescript.transaction(async t => {
77 // Maybe the video changed in database, refresh it
78 let videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid, t)
79 // Video does not exist anymore
80 if (!videoDatabase) return undefined
81
82 // We transcoded the video file in another format, now we can publish it
83 const oldState = videoDatabase.state
84 videoDatabase.state = VideoState.PUBLISHED
85 videoDatabase = await videoDatabase.save({ transaction: t })
86
87 // If the video was not published, we consider it is a new one for other instances
88 const isNewVideo = oldState !== VideoState.PUBLISHED
89 await federateVideoIfNeeded(videoDatabase, isNewVideo, t)
90
91 return undefined
92 })
93 }
94
95 async function onVideoFileOptimizerSuccess (video: VideoModel, isNewVideo: boolean) {
96 if (video === undefined) return undefined
97
98 // Outside the transaction (IO on disk)
99 const { videoFileResolution } = await video.getOriginalFileResolution()
100
101 return sequelizeTypescript.transaction(async t => {
102 // Maybe the video changed in database, refresh it
103 const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid, t)
104 // Video does not exist anymore
105 if (!videoDatabase) return undefined
106
107 // Create transcoding jobs if there are enabled resolutions
108 const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution)
109 logger.info(
110 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, videoFileResolution,
111 { resolutions: resolutionsEnabled }
112 )
113
114 if (resolutionsEnabled.length !== 0) {
115 const tasks: Promise<any>[] = []
116
117 for (const resolution of resolutionsEnabled) {
118 const dataInput = {
119 videoUUID: videoDatabase.uuid,
120 resolution
121 }
122
123 const p = JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput })
124 tasks.push(p)
125 }
126
127 await Promise.all(tasks)
128
129 logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled })
130 } else {
131 // No transcoding to do, it's now published
132 video.state = VideoState.PUBLISHED
133 video = await video.save({ transaction: t })
134
135 logger.info('No transcoding jobs created for video %s (no resolutions).', video.uuid)
136 }
137
138 return federateVideoIfNeeded(video, isNewVideo, t)
139 })
140 }
141
142 // ---------------------------------------------------------------------------
143
144 export {
145 processVideoFile,
146 processVideoFileImport
147 }