|
| 1 | +import { Job } from 'bullmq' |
| 2 | +import { RunDownloadJobParams } from '../../types/downloads.js' |
| 3 | +import { QueueService } from '#services/queue_service' |
| 4 | +import { doResumableDownload } from '../utils/downloads.js' |
| 5 | +import { createHash } from 'crypto' |
| 6 | +import { DockerService } from '#services/docker_service' |
| 7 | +import { ZimService } from '#services/zim_service' |
| 8 | + |
| 9 | +export class RunDownloadJob { |
| 10 | + static get queue() { |
| 11 | + return 'downloads' |
| 12 | + } |
| 13 | + |
| 14 | + static get key() { |
| 15 | + return 'run-download' |
| 16 | + } |
| 17 | + |
| 18 | + static getJobId(url: string): string { |
| 19 | + return createHash('sha256').update(url).digest('hex').slice(0, 16) |
| 20 | + } |
| 21 | + |
| 22 | + async handle(job: Job) { |
| 23 | + const { url, filepath, timeout, allowedMimeTypes, forceNew, filetype } = |
| 24 | + job.data as RunDownloadJobParams |
| 25 | + |
| 26 | + // console.log("Simulating delay for job for URL:", url) |
| 27 | + // await new Promise((resolve) => setTimeout(resolve, 30000)) // Simulate initial delay |
| 28 | + // console.log("Starting download for URL:", url) |
| 29 | + |
| 30 | + // // simulate progress updates for demonstration |
| 31 | + // for (let progress = 0; progress <= 100; progress += 10) { |
| 32 | + // await new Promise((resolve) => setTimeout(resolve, 20000)) // Simulate time taken for each progress step |
| 33 | + // job.updateProgress(progress) |
| 34 | + // console.log(`Job progress for URL ${url}: ${progress}%`) |
| 35 | + // } |
| 36 | + |
| 37 | + await doResumableDownload({ |
| 38 | + url, |
| 39 | + filepath, |
| 40 | + timeout, |
| 41 | + allowedMimeTypes, |
| 42 | + forceNew, |
| 43 | + onProgress(progress) { |
| 44 | + const progressPercent = (progress.downloadedBytes / (progress.totalBytes || 1)) * 100 |
| 45 | + job.updateProgress(Math.floor(progressPercent)) |
| 46 | + }, |
| 47 | + async onComplete(url) { |
| 48 | + if (filetype === 'zim') { |
| 49 | + try { |
| 50 | + const dockerService = new DockerService() |
| 51 | + const zimService = new ZimService(dockerService) |
| 52 | + await zimService.downloadRemoteSuccessCallback([url], true) |
| 53 | + } catch (error) { |
| 54 | + console.error( |
| 55 | + `[RunDownloadJob] Error in ZIM download success callback for URL ${url}:`, |
| 56 | + error |
| 57 | + ) |
| 58 | + } |
| 59 | + } |
| 60 | + job.updateProgress(100) |
| 61 | + }, |
| 62 | + }) |
| 63 | + |
| 64 | + return { |
| 65 | + url, |
| 66 | + filepath, |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + static async getByUrl(url: string): Promise<Job | undefined> { |
| 71 | + const queueService = new QueueService() |
| 72 | + const queue = queueService.getQueue(this.queue) |
| 73 | + const jobId = this.getJobId(url) |
| 74 | + return await queue.getJob(jobId) |
| 75 | + } |
| 76 | + |
| 77 | + static async dispatch(params: RunDownloadJobParams) { |
| 78 | + const queueService = new QueueService() |
| 79 | + const queue = queueService.getQueue(this.queue) |
| 80 | + const jobId = this.getJobId(params.url) |
| 81 | + |
| 82 | + try { |
| 83 | + const job = await queue.add(this.key, params, { |
| 84 | + jobId, |
| 85 | + attempts: 3, |
| 86 | + backoff: { type: 'exponential', delay: 2000 }, |
| 87 | + removeOnComplete: true, |
| 88 | + }) |
| 89 | + |
| 90 | + return { |
| 91 | + job, |
| 92 | + created: true, |
| 93 | + message: `Dispatched download job for URL ${params.url}`, |
| 94 | + } |
| 95 | + } catch (error) { |
| 96 | + if (error.message.includes('job already exists')) { |
| 97 | + const existing = await queue.getJob(jobId) |
| 98 | + return { |
| 99 | + job: existing, |
| 100 | + created: false, |
| 101 | + message: `Job already exists for URL ${params.url}`, |
| 102 | + } |
| 103 | + } |
| 104 | + throw error |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments