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