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