Files
BarevClub/packages/services/src/queue.ts
T

22 lines
856 B
TypeScript

import { Queue } from 'bullmq';
import { EVENTS_QUEUE, type DomainEvent, type Notifier } from './context.ts';
export function redisConnection(url: string) {
const u = new URL(url);
return { host: u.hostname, port: Number(u.port || 6379), password: u.password || undefined, maxRetriesPerRequest: null };
}
/** Notifier, который кладёт доменные события в очередь воркера */
export function createQueueNotifier(redisUrl: string): Notifier & { queue: Queue<DomainEvent> } {
const queue = new Queue<DomainEvent>(EVENTS_QUEUE, {
connection: redisConnection(redisUrl),
defaultJobOptions: { attempts: 5, backoff: { type: 'exponential', delay: 2000 }, removeOnComplete: 1000, removeOnFail: 5000 },
});
return {
queue,
async emit(event) {
await queue.add(event.type, event);
},
};
}