From d39d6f8ed7458c61bdf3324acea194e2fedf2e58 Mon Sep 17 00:00:00 2001 From: Izan Gil <66965250+SrIzan10@users.noreply.github.com> Date: Mon, 31 Mar 2025 21:54:14 +0200 Subject: [PATCH] refactor: some code refactoring --- apps/web/src/lib/workers/index.ts | 19 ------ apps/web/src/lib/workers/register.ts | 1 - apps/web/src/lib/workers/types.ts | 64 ------------------- .../src/lib/workers/worker/notification.ts | 12 ---- 4 files changed, 96 deletions(-) delete mode 100644 apps/web/src/lib/workers/types.ts diff --git a/apps/web/src/lib/workers/index.ts b/apps/web/src/lib/workers/index.ts index e508ba2..06ea381 100644 --- a/apps/web/src/lib/workers/index.ts +++ b/apps/web/src/lib/workers/index.ts @@ -1,7 +1,5 @@ -import type { ChatPostMessageArguments } from '@slack/web-api'; import { Queue, Worker } from 'bullmq'; import { getRedisConnection } from '@/lib/services/redis'; -import snClient from '../services/slackNotifier'; // Singleton instances for notifier const globalForNotifier = global as unknown as { @@ -30,21 +28,4 @@ export function getNotificationQueue(): Queue { }); } return globalForNotifier.notificationQueue; -} - -// Cleanup function for notification resources -export async function closeNotificationResources(): Promise { - // Close worker - if (globalForNotifier.notificationWorker) { - await globalForNotifier.notificationWorker.close(); - globalForNotifier.notificationWorker = null; - console.log('Notification worker closed'); - } - - // Close queue - if (globalForNotifier.notificationQueue) { - await globalForNotifier.notificationQueue.close(); - globalForNotifier.notificationQueue = null; - console.log('Notification queue closed'); - } } \ No newline at end of file diff --git a/apps/web/src/lib/workers/register.ts b/apps/web/src/lib/workers/register.ts index 8020905..fc64759 100644 --- a/apps/web/src/lib/workers/register.ts +++ b/apps/web/src/lib/workers/register.ts @@ -1,6 +1,5 @@ import { registerNotificationWorker } from './worker/notification'; -// Register all workers in one place export async function registerWorkers(): Promise { await registerNotificationWorker(); console.log('All workers registered successfully'); diff --git a/apps/web/src/lib/workers/types.ts b/apps/web/src/lib/workers/types.ts deleted file mode 100644 index 76b69ab..0000000 --- a/apps/web/src/lib/workers/types.ts +++ /dev/null @@ -1,64 +0,0 @@ -// Define a union type of all possible job names -export type JobName = - | 'email:send' - | 'video:process' - | 'notification:push' - | 'user:sync' - // Add more job names as needed - -// Define payload and result types for each job -export interface JobDefinitions { - 'email:send': { - payload: { - to: string; - subject: string; - body: string; - attachments?: Array<{name: string, content: string}>; - }; - result: { - sent: boolean; - messageId?: string; - error?: string; - }; - }; - - 'video:process': { - payload: { - videoId: string; - formats: string[]; - resolution?: string; - }; - result: { - success: boolean; - processedFormats: string[]; - duration: number; - }; - }; - - 'notification:push': { - payload: { - userId: string; - message: string; - data?: Record; - }; - result: { - delivered: boolean; - deviceCount: number; - }; - }; - - 'user:sync': { - payload: { - userId: string; - externalSystems: string[]; - }; - result: { - syncedSystems: string[]; - failedSystems: string[]; - }; - }; -} - -export type PayloadFor = JobDefinitions[T]['payload']; - -export type ResultFor = JobDefinitions[T]['result']; \ No newline at end of file diff --git a/apps/web/src/lib/workers/worker/notification.ts b/apps/web/src/lib/workers/worker/notification.ts index dfe8764..361a3b9 100644 --- a/apps/web/src/lib/workers/worker/notification.ts +++ b/apps/web/src/lib/workers/worker/notification.ts @@ -10,7 +10,6 @@ if (!globalForWorker.notificationWorker) { globalForWorker.notificationWorker = null; } -// Register the Slack notification worker export async function registerNotificationWorker(): Promise { if (globalForWorker.notificationWorker) { console.log('Notification worker already registered'); @@ -40,17 +39,7 @@ export async function registerNotificationWorker(): Promise { } }); - // Set up event handlers - worker.on('completed', job => { - console.log(`Job ${job.id} completed successfully`); - }); - - worker.on('failed', (job, error) => { - console.error(`Job ${job?.id} failed:`, error); - }); - globalForWorker.notificationWorker = worker; - console.log('Notification worker registered successfully'); } // Close the worker @@ -58,6 +47,5 @@ export async function closeNotificationWorker(): Promise { if (globalForWorker.notificationWorker) { await globalForWorker.notificationWorker.close(); globalForWorker.notificationWorker = null; - console.log('Notification worker closed'); } } \ No newline at end of file