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