refactor: some code refactoring

This commit is contained in:
2025-03-31 21:54:14 +02:00
parent 18d69a6aab
commit d39d6f8ed7
4 changed files with 0 additions and 96 deletions

View File

@@ -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<void> {
// 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');
}
}

View File

@@ -1,6 +1,5 @@
import { registerNotificationWorker } from './worker/notification';
// Register all workers in one place
export async function registerWorkers(): Promise<void> {
await registerNotificationWorker();
console.log('All workers registered successfully');

View File

@@ -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<string, any>;
};
result: {
delivered: boolean;
deviceCount: number;
};
};
'user:sync': {
payload: {
userId: string;
externalSystems: string[];
};
result: {
syncedSystems: string[];
failedSystems: string[];
};
};
}
export type PayloadFor<T extends JobName> = JobDefinitions[T]['payload'];
export type ResultFor<T extends JobName> = JobDefinitions[T]['result'];

View File

@@ -10,7 +10,6 @@ if (!globalForWorker.notificationWorker) {
globalForWorker.notificationWorker = null;
}
// Register the Slack notification worker
export async function registerNotificationWorker(): Promise<void> {
if (globalForWorker.notificationWorker) {
console.log('Notification worker already registered');
@@ -40,17 +39,7 @@ export async function registerNotificationWorker(): Promise<void> {
}
});
// 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<void> {
if (globalForWorker.notificationWorker) {
await globalForWorker.notificationWorker.close();
globalForWorker.notificationWorker = null;
console.log('Notification worker closed');
}
}