Files
parcer/server/src/worker.ts

55 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
const connection = new IORedis({
2024-10-07 01:21:39 +05:30
host: 'localhost',
port: 6379,
maxRetriesPerRequest: null,
2024-10-07 01:16:49 +05:30
});
connection.on('connect', () => {
2024-10-07 01:21:39 +05:30
console.log('Connected to Redis!');
2024-10-07 01:16:49 +05:30
});
connection.on('error', (err) => {
2024-10-07 01:21:39 +05:30
console.error('Redis connection error:', err);
2024-10-07 01:16:49 +05:30
});
const workflowQueue = new Queue('workflow', { connection });
2024-10-07 01:20:24 +05:30
const worker = new Worker('workflow', async job => {
2024-10-10 03:24:19 +05:30
const { runId, userId } = job.data;
2024-10-07 01:21:39 +05:30
try {
2024-10-10 03:24:04 +05:30
const result = await handleRunRecording(runId, userId);
2024-10-07 01:21:39 +05:30
return result;
} catch (error) {
logger.error('Error running workflow:', error);
throw error;
}
2024-10-07 01:16:49 +05:30
}, { connection });
worker.on('completed', async (job: any) => {
2024-10-10 03:21:32 +05:30
logger.log(`info`, `Job ${job.id} completed for ${job.data.runId}`);
2024-10-07 01:16:49 +05:30
});
worker.on('failed', async (job: any, err) => {
2024-10-10 03:21:32 +05:30
logger.log(`error`, `Job ${job.id} failed for ${job.data.runId}:`, err);
2024-10-07 01:16:49 +05:30
});
2024-10-07 01:28:48 +05:30
console.log('Worker is running...');
2024-10-07 01:16:49 +05:30
async function jobCounts() {
2024-10-07 01:21:39 +05:30
const jobCounts = await workflowQueue.getJobCounts();
console.log('Jobs:', jobCounts);
2024-10-07 01:16:49 +05:30
}
2024-10-07 01:19:35 +05:30
jobCounts();
2024-10-07 01:42:13 +05:30
process.on('SIGINT', () => {
console.log('Worker shutting down...');
process.exit();
});
2024-10-07 01:20:24 +05:30
export { workflowQueue, worker };