Files
parcer/server/src/worker.ts

59 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-10-07 01:16:49 +05:30
import { Queue, Worker } from 'bullmq';
import IORedis from 'ioredis';
2024-10-07 01:19:04 +05:30
import logger from './logger';
2024-10-07 01:21:19 +05:30
import { handleRunRecording } from "./workflow-management/scheduler";
2024-10-07 01:16:49 +05:30
2024-10-22 16:59:04 +05:30
const connection = new IORedis({
host: 'localhost',
port: 6379,
maxRetriesPerRequest: null,
2024-10-07 01:16:49 +05:30
});
2024-10-22 16:59:04 +05:30
connection.on('connect', () => {
console.log('Connected to Redis!');
});
connection.on('error', (err) => {
console.error('Redis connection error:', err);
});
2024-10-07 01:16:49 +05:30
const workflowQueue = new Queue('workflow', { connection });
2024-10-22 16:59:04 +05:30
const worker = new Worker('workflow', async job => {
const { runId, userId } = job.data;
try {
const result = await handleRunRecording(runId, userId);
2024-10-07 01:21:39 +05:30
return result;
} catch (error) {
2024-10-22 16:59:04 +05:30
logger.error('Error running workflow:', error);
throw error;
}
}, { connection });
2024-10-07 01:16:49 +05:30
2024-10-22 16:59:04 +05:30
worker.on('completed', async (job: any) => {
logger.log(`info`, `Job ${job.id} completed for ${job.data.runId}`);
});
2024-10-07 01:16:49 +05:30
2024-10-22 16:59:04 +05:30
worker.on('failed', async (job: any, err) => {
logger.log(`error`, `Job ${job.id} failed for ${job.data.runId}:`, err);
});
2024-10-07 01:16:49 +05:30
2024-10-22 16:59:04 +05:30
console.log('Worker is running...');
2024-10-07 01:28:48 +05:30
2024-10-22 16:59:04 +05:30
async function jobCounts() {
const jobCounts = await workflowQueue.getJobCounts();
console.log('Jobs:', jobCounts);
}
2024-10-07 01:16:49 +05:30
2024-10-22 16:59:04 +05:30
jobCounts();
2024-10-07 01:19:35 +05:30
2024-10-22 16:59:04 +05:30
process.on('SIGINT', () => {
console.log('Worker shutting down...');
process.exit();
});
2024-10-22 16:58:48 +05:30
2024-10-22 16:59:04 +05:30
export { workflowQueue, worker };
2024-10-07 01:42:13 +05:30
2024-10-22 16:58:48 +05:30
export const temp = () => {
console.log('temp');
}