From fb4d5a8fe516b1b43a2f8cac32f65b87e5f8bbad Mon Sep 17 00:00:00 2001 From: Martin Porwoll Date: Sat, 14 Feb 2026 13:30:07 +0000 Subject: [PATCH] feat(youtube): add upload queue job definition Add YOUTUBE_UPLOAD to QUEUE_NAMES and create the job definition with enqueue and status functions. Uses 2 retry attempts instead of the default 3 since uploads are resource-intensive. Co-Authored-By: Claude Opus 4.6 --- src/lib/queue/jobs/youtube-upload-job.ts | 79 ++++++++++++++++++++++++ src/lib/queue/queue-service.ts | 1 + 2 files changed, 80 insertions(+) create mode 100644 src/lib/queue/jobs/youtube-upload-job.ts diff --git a/src/lib/queue/jobs/youtube-upload-job.ts b/src/lib/queue/jobs/youtube-upload-job.ts new file mode 100644 index 0000000..f122f2d --- /dev/null +++ b/src/lib/queue/jobs/youtube-upload-job.ts @@ -0,0 +1,79 @@ +/** + * YouTube Upload Job Definition + * + * Definiert den YouTube-Upload-Job fuer die Queue. + * Uploads sind ressourcenintensiv, daher nur 2 Retry-Versuche. + */ + +import { Job } from 'bullmq' +import { getQueue, QUEUE_NAMES, defaultJobOptions } from '../queue-service' + +// Job-Daten Typen +export interface YouTubeUploadJobData { + contentId: number + channelId: number + mediaId: number + metadata: { + title: string + description: string + tags: string[] + visibility: 'public' | 'unlisted' | 'private' + categoryId?: string + } + scheduledPublishAt?: string + triggeredBy: number +} + +export interface YouTubeUploadJobResult { + success: boolean + youtubeVideoId?: string + youtubeUrl?: string + error?: string + timestamp: string +} + +/** + * Fuegt einen YouTube-Upload-Job zur Queue hinzu + */ +export async function enqueueYouTubeUpload( + data: YouTubeUploadJobData, +): Promise> { + const queue = getQueue(QUEUE_NAMES.YOUTUBE_UPLOAD) + + const jobOptions = { + ...defaultJobOptions, + attempts: 2, + } + + const job = await queue.add('youtube-upload', data, jobOptions) + + console.log( + `[YouTubeUploadQueue] Job ${job.id} queued for content ${data.contentId} on channel ${data.channelId}`, + ) + + return job +} + +/** + * Holt den Status eines YouTube-Upload-Jobs + */ +export async function getYouTubeUploadJobStatus(jobId: string): Promise<{ + state: string + progress: number + result?: YouTubeUploadJobResult + failedReason?: string +} | null> { + const queue = getQueue(QUEUE_NAMES.YOUTUBE_UPLOAD) + + const job = await queue.getJob(jobId) + if (!job) return null + + const [state, progress] = await Promise.all([job.getState(), job.progress]) + + return { + state, + progress: typeof progress === 'number' ? progress : 0, + result: job.returnvalue as YouTubeUploadJobResult | undefined, + failedReason: job.failedReason, + } +} diff --git a/src/lib/queue/queue-service.ts b/src/lib/queue/queue-service.ts index 6b35d0c..35513de 100644 --- a/src/lib/queue/queue-service.ts +++ b/src/lib/queue/queue-service.ts @@ -13,6 +13,7 @@ export const QUEUE_NAMES = { EMAIL: 'email', PDF: 'pdf', CLEANUP: 'cleanup', + YOUTUBE_UPLOAD: 'youtube-upload', } as const export type QueueName = (typeof QUEUE_NAMES)[keyof typeof QUEUE_NAMES]