File size: 1,079 Bytes
a6b6c66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { Queue, Worker } from 'bullmq';
import { redisConnection } from '../config/redis';
import { automationEngine } from '../automation/engine';

export const incomingQueue = new Queue('incoming-messages', {
  connection: redisConnection,
});

export const initWorkers = () => {
  const worker = new Worker(
    'incoming-messages',
    async (job) => {
      console.log(`Processing job ${job.id} of type ${job.name}`);
      try {
        if (job.name === 'webhook-event') {
          await automationEngine.processEvent(job.data);
        } else if (job.name === 'process-queued-event') {
          await automationEngine.processQueuedEvent(job.data.eventId);
        }
      } catch (error) {

        console.error(`Error processing job ${job.id}:`, error);
        throw error;
      }
    },
    {
      connection: redisConnection,
      concurrency: 5,
    }
  );

  worker.on('completed', (job) => {
    console.log(`Job ${job.id} completed`);
  });

  worker.on('failed', (job, err) => {
    console.error(`Job ${job?.id} failed:`, err);
  });

  return worker;
};