1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-04 02:09:37 +02:00

Use bullmq job dependency

This commit is contained in:
Chocobozzz 2022-08-08 15:48:17 +02:00
parent 5a921e7b74
commit bd911b54b5
No known key found for this signature in database
GPG key ID: 583A612D890159BE
42 changed files with 314 additions and 152 deletions

View file

@ -8,9 +8,9 @@ import { generateWebTorrentVideoFilename } from '@server/lib/paths'
import { Redis } from '@server/lib/redis'
import { uploadx } from '@server/lib/uploadx'
import {
addMoveToObjectStorageJob,
addOptimizeOrMergeAudioJob,
buildLocalVideoFromReq,
buildMoveToObjectStorageJob,
buildOptimizeOrMergeAudioJob,
buildVideoThumbnailsFromReq,
setVideoTags
} from '@server/lib/video'
@ -18,19 +18,16 @@ import { VideoPathManager } from '@server/lib/video-path-manager'
import { buildNextVideoState } from '@server/lib/video-state'
import { openapiOperationDoc } from '@server/middlewares/doc'
import { VideoSourceModel } from '@server/models/video/video-source'
import { MVideoFile, MVideoFullLight } from '@server/types/models'
import { MUserId, MVideoFile, MVideoFullLight } from '@server/types/models'
import { getLowercaseExtension } from '@shared/core-utils'
import { isAudioFile, uuidToShort } from '@shared/extra-utils'
import { HttpStatusCode, ManageVideoTorrentPayload, VideoCreate, VideoResolution, VideoState } from '@shared/models'
import { HttpStatusCode, VideoCreate, VideoResolution, VideoState } from '@shared/models'
import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger'
import { retryTransactionWrapper } from '../../../helpers/database-utils'
import { createReqFiles } from '../../../helpers/express-utils'
import { buildFileMetadata, ffprobePromise, getVideoStreamDimensionsInfo, getVideoStreamFPS } from '../../../helpers/ffmpeg'
import { logger, loggerTagsFactory } from '../../../helpers/logger'
import { MIMETYPES } from '../../../initializers/constants'
import { sequelizeTypescript } from '../../../initializers/database'
import { federateVideoIfNeeded } from '../../../lib/activitypub/videos'
import { Notifier } from '../../../lib/notifier'
import { Hooks } from '../../../lib/plugins/hooks'
import { generateVideoMiniature } from '../../../lib/thumbnail'
import { autoBlacklistVideoIfNeeded } from '../../../lib/video-blacklist'
@ -216,22 +213,8 @@ async function addVideo (options: {
// Channel has a new content, set as updated
await videoCreated.VideoChannel.setAsUpdated()
createTorrentFederate(videoCreated, videoFile)
.catch(err => {
logger.error('Cannot create torrent or federate video for %s.', videoCreated.uuid, { err, ...lTags(videoCreated.uuid) })
return videoCreated
}).then(refreshedVideo => {
if (!refreshedVideo) return
if (refreshedVideo.state === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) {
return addMoveToObjectStorageJob({ video: refreshedVideo, previousVideoState: undefined })
}
if (refreshedVideo.state === VideoState.TO_TRANSCODE) {
return addOptimizeOrMergeAudioJob({ video: refreshedVideo, videoFile, user })
}
}).catch(err => logger.error('Cannot add optimize/merge audio job for %s.', videoCreated.uuid, { err, ...lTags(videoCreated.uuid) }))
addVideoJobsAfterUpload(videoCreated, videoFile, user)
.catch(err => logger.error('Cannot build new video jobs of %s.', videoCreated.uuid, { err, ...lTags(videoCreated.uuid) }))
Hooks.runAction('action:api.video.uploaded', { video: videoCreated, req, res })
@ -266,23 +249,32 @@ async function buildNewFile (videoPhysicalFile: express.VideoUploadFile) {
return videoFile
}
async function createTorrentFederate (video: MVideoFullLight, videoFile: MVideoFile) {
const payload: ManageVideoTorrentPayload = { videoId: video.id, videoFileId: videoFile.id, action: 'create' }
async function addVideoJobsAfterUpload (video: MVideoFullLight, videoFile: MVideoFile, user: MUserId) {
return JobQueue.Instance.createSequentialJobFlow(
{
type: 'manage-video-torrent' as 'manage-video-torrent',
payload: {
videoId: video.id,
videoFileId: videoFile.id,
action: 'create'
}
},
{
type: 'federate-video' as 'federate-video',
payload: {
videoUUID: video.uuid,
isNewVideo: true
}
},
const job = await JobQueue.Instance.createJobWithPromise({ type: 'manage-video-torrent', payload })
await JobQueue.Instance.waitJob(job)
video.state === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE
? await buildMoveToObjectStorageJob({ video, previousVideoState: undefined })
: undefined,
const refreshedVideo = await VideoModel.loadFull(video.id)
if (!refreshedVideo) return
// Only federate and notify after the torrent creation
Notifier.Instance.notifyOnNewVideoIfNeeded(refreshedVideo)
await retryTransactionWrapper(() => {
return sequelizeTypescript.transaction(t => federateVideoIfNeeded(refreshedVideo, true, t))
})
return refreshedVideo
video.state === VideoState.TO_TRANSCODE
? await buildOptimizeOrMergeAudioJob({ video, videoFile, user })
: undefined
)
}
async function deleteUploadResumableCache (req: express.Request, res: express.Response, next: express.NextFunction) {