/opt/mawid/apps/api/src/waitlist
Edit: /opt/mawid/apps/api/src/waitlist/waitlist.worker.ts (1197B)
import { Injectable, Logger, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Worker, type Job } from 'bullmq';
import IORedis from 'ioredis';
import { WAITLIST_QUEUE_NAME, type WaitlistQueueJob } from './waitlist-queue';
import { WaitlistService } from './waitlist.service';
@Injectable()
export class WaitlistWorker implements OnModuleInit, OnModuleDestroy {
private readonly logger = new Logger(WaitlistWorker.name);
private worker?: Worker
;
constructor(
private readonly config: ConfigService,
private readonly waitlist: WaitlistService,
) {}
onModuleInit() {
const connection = new IORedis(this.config.getOrThrow('REDIS_URL'), {
maxRetriesPerRequest: null,
});
this.worker = new Worker(
WAITLIST_QUEUE_NAME,
(job: Job) => this.waitlist.expireHold(job.data.holdId),
{ connection, concurrency: 5 },
);
this.worker.on('failed', (job, err) => {
this.logger.warn(`hold expiry ${job?.data.holdId} failed: ${err.message}`);
});
}
async onModuleDestroy() {
await this.worker?.close();
}
}