From 419b520ca4434d17f3505013174e195c3a316716 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 19 Jan 2022 14:23:00 +0100 Subject: Add ability to cancel & delete video imports --- server/lib/job-queue/handlers/video-import.ts | 29 ++++++++++++++------------- server/lib/job-queue/job-queue.ts | 12 +++++++++++ 2 files changed, 27 insertions(+), 14 deletions(-) (limited to 'server/lib') diff --git a/server/lib/job-queue/handlers/video-import.ts b/server/lib/job-queue/handlers/video-import.ts index 2f74e9fbd..cb79725aa 100644 --- a/server/lib/job-queue/handlers/video-import.ts +++ b/server/lib/job-queue/handlers/video-import.ts @@ -42,8 +42,17 @@ import { generateVideoMiniature } from '../../thumbnail' async function processVideoImport (job: Job) { const payload = job.data as VideoImportPayload - if (payload.type === 'youtube-dl') return processYoutubeDLImport(job, payload) - if (payload.type === 'magnet-uri' || payload.type === 'torrent-file') return processTorrentImport(job, payload) + const videoImport = await getVideoImportOrDie(payload.videoImportId) + if (videoImport.state === VideoImportState.CANCELLED) { + logger.info('Do not process import since it has been cancelled', { payload }) + return + } + + videoImport.state = VideoImportState.PROCESSING + await videoImport.save() + + if (payload.type === 'youtube-dl') return processYoutubeDLImport(job, videoImport, payload) + if (payload.type === 'magnet-uri' || payload.type === 'torrent-file') return processTorrentImport(job, videoImport, payload) } // --------------------------------------------------------------------------- @@ -54,15 +63,11 @@ export { // --------------------------------------------------------------------------- -async function processTorrentImport (job: Job, payload: VideoImportTorrentPayload) { +async function processTorrentImport (job: Job, videoImport: MVideoImportDefault, payload: VideoImportTorrentPayload) { logger.info('Processing torrent video import in job %d.', job.id) - const videoImport = await getVideoImportOrDie(payload.videoImportId) + const options = { type: payload.type, videoImportId: payload.videoImportId } - const options = { - type: payload.type, - videoImportId: payload.videoImportId - } const target = { torrentName: videoImport.torrentName ? getSecureTorrentName(videoImport.torrentName) : undefined, uri: videoImport.magnetUri @@ -70,14 +75,10 @@ async function processTorrentImport (job: Job, payload: VideoImportTorrentPayloa return processFile(() => downloadWebTorrentVideo(target, VIDEO_IMPORT_TIMEOUT), videoImport, options) } -async function processYoutubeDLImport (job: Job, payload: VideoImportYoutubeDLPayload) { +async function processYoutubeDLImport (job: Job, videoImport: MVideoImportDefault, payload: VideoImportYoutubeDLPayload) { logger.info('Processing youtubeDL video import in job %d.', job.id) - const videoImport = await getVideoImportOrDie(payload.videoImportId) - const options = { - type: payload.type, - videoImportId: videoImport.id - } + const options = { type: payload.type, videoImportId: videoImport.id } const youtubeDL = new YoutubeDLWrapper(videoImport.targetUrl, ServerConfigManager.Instance.getEnabledResolutions('vod')) diff --git a/server/lib/job-queue/job-queue.ts b/server/lib/job-queue/job-queue.ts index fbc599f12..22bd1f5d2 100644 --- a/server/lib/job-queue/job-queue.ts +++ b/server/lib/job-queue/job-queue.ts @@ -162,6 +162,18 @@ class JobQueue { } } + async pause () { + for (const handler of Object.keys(this.queues)) { + await this.queues[handler].pause(true) + } + } + + async resume () { + for (const handler of Object.keys(this.queues)) { + await this.queues[handler].resume(true) + } + } + createJob (obj: CreateJobArgument, options: CreateJobOptions = {}): void { this.createJobWithPromise(obj, options) .catch(err => logger.error('Cannot create job.', { err, obj })) -- cgit v1.2.3