diff --git a/src/email/client.ts b/src/email/client.ts index e1b7409..00254c9 100644 --- a/src/email/client.ts +++ b/src/email/client.ts @@ -78,7 +78,7 @@ export class EmailClient { private async handleNewMail( numMessages: number, - callback: (emails: Email[]) => void, + callback: (emails: Email[]) => Promise, ) { this.loggerService.log(`Received ${numMessages} emails`) const mailbox = await new Promise((res) => this.getMailbox(res)) @@ -96,11 +96,11 @@ export class EmailClient { totalMessages - (numMessages - 1), ) this.loggerService.debug(`Fetched ${emails.length} emails`) - callback(emails) + await callback(emails) } } - private _listen(callback: (emails: Email[]) => void) { + private _listen(callback: (emails: Email[]) => Promise) { // Don't start listening until we are ready if (this.status === EmailClientStatus.NotReady) { throw new Error('Not ready to listen') @@ -116,10 +116,13 @@ export class EmailClient { this.mailbox = mailbox }) - this.imapClient.on('mail', (n: number) => this.handleNewMail(n, callback)) + this.imapClient.on( + 'mail', + (n: number) => this.handleNewMail(n, callback), + ) } - public listen(callback: (emails: Email[]) => void) { + public listen(callback: (emails: Email[]) => Promise) { this.attempt('listen', 0, 5, 1000, () => { this._listen(callback) }) @@ -198,7 +201,7 @@ export class EmailClient { } public async markMailsSeen(messageIds: string[]) { - this.loggerService.debug('Marking mails as seen', { messageIds }) + this.loggerService.debug(`Marking mails as seen (${messageIds.join(',')})`) return new Promise((res, rej) => { this.imapClient.addFlags(messageIds.join(','), ['\\Seen'], (error) => { if (error != null) { diff --git a/src/email/module.ts b/src/email/module.ts index 2416009..c7d2536 100644 --- a/src/email/module.ts +++ b/src/email/module.ts @@ -9,7 +9,7 @@ import { EmailProvider } from './provider' @Module({ imports: [ LoggerModule, - BullModule.registerQueue({ name: EMAILS_QUEUE_NAME }), + BullModule.registerQueueAsync({ name: EMAILS_QUEUE_NAME }), ], providers: [EmailClient, EmailProvider], exports: [EmailClient, EmailProvider], diff --git a/src/email/provider.ts b/src/email/provider.ts index b1d6d57..664e316 100644 --- a/src/email/provider.ts +++ b/src/email/provider.ts @@ -20,7 +20,7 @@ export class EmailProvider { private async handleReceivedEmails(emails: Email[]) { await this.emailsQueue.addBulk( - emails.map((email) => ({ name: email.id, data: email })), + emails.map((email) => ({ data: email })), ) } diff --git a/src/ntfy/module.ts b/src/ntfy/module.ts index 4ed8cc9..8b39d2c 100644 --- a/src/ntfy/module.ts +++ b/src/ntfy/module.ts @@ -8,7 +8,7 @@ import { NTFY_PUBLISH_QUEUE_NAME } from './types' @Module({ imports: [ - BullModule.registerQueue({ name: NTFY_PUBLISH_QUEUE_NAME }), + BullModule.registerQueueAsync({ name: NTFY_PUBLISH_QUEUE_NAME }), LoggerModule, ], providers: [NtfyProvider, NtfyClient], diff --git a/src/ntfy/provider.ts b/src/ntfy/provider.ts index de476ee..0d76390 100644 --- a/src/ntfy/provider.ts +++ b/src/ntfy/provider.ts @@ -5,12 +5,7 @@ import { Job, JobOptions, Queue } from 'bull' import { Dayjs } from 'dayjs' import { NtfyClient } from './client' -import { - MessageConfig, - MessagePriority, - MessageTags, - NTFY_PUBLISH_QUEUE_NAME, -} from './types' +import { MessageConfig, MessageTags, NTFY_PUBLISH_QUEUE_NAME } from './types' @Processor(NTFY_PUBLISH_QUEUE_NAME) @Injectable() diff --git a/src/reservations/module.ts b/src/reservations/module.ts index 0d2de11..d136ed3 100644 --- a/src/reservations/module.ts +++ b/src/reservations/module.ts @@ -16,7 +16,7 @@ import { ReservationsWorker } from './worker' imports: [ LoggerModule, TypeOrmModule.forFeature([Reservation]), - BullModule.registerQueue({ name: RESERVATIONS_QUEUE_NAME }), + BullModule.registerQueueAsync({ name: RESERVATIONS_QUEUE_NAME }), RunnerModule, NtfyModule, ], diff --git a/src/runner/module.ts b/src/runner/module.ts index 85b5d38..754384d 100644 --- a/src/runner/module.ts +++ b/src/runner/module.ts @@ -8,7 +8,10 @@ import { RunnerService } from './service' @Module({ providers: [RunnerService, BaanReserverenService, EmptyPageFactory], - imports: [LoggerModule, BullModule.registerQueue({ name: 'reservations' })], + imports: [ + LoggerModule, + BullModule.registerQueueAsync({ name: 'reservations' }), + ], exports: [EmptyPageFactory, BaanReserverenService], }) export class RunnerModule {} diff --git a/src/waitingList/module.ts b/src/waitingList/module.ts index 2e2db5b..31d611c 100644 --- a/src/waitingList/module.ts +++ b/src/waitingList/module.ts @@ -13,8 +13,8 @@ import { WaitingListService } from './service' imports: [ LoggerModule, ReservationsModule, - BullModule.registerQueue({ name: EMAILS_QUEUE_NAME }), - BullModule.registerQueue({ name: RESERVATIONS_QUEUE_NAME }), + BullModule.registerQueueAsync({ name: EMAILS_QUEUE_NAME }), + BullModule.registerQueueAsync({ name: RESERVATIONS_QUEUE_NAME }), EmailModule, NtfyModule, ], diff --git a/src/waitingList/service.ts b/src/waitingList/service.ts index 043648b..47fe075 100644 --- a/src/waitingList/service.ts +++ b/src/waitingList/service.ts @@ -1,5 +1,5 @@ import { InjectQueue, Process, Processor } from '@nestjs/bull' -import { Inject } from '@nestjs/common' +import { Inject, Injectable } from '@nestjs/common' import { Job, Queue } from 'bull' import { NtfyProvider } from 'src/ntfy/provider' @@ -25,6 +25,7 @@ const EMAIL_START_TIME_REGEX = new RegExp( const EMAIL_END_TIME_REGEX = new RegExp(/^Eindtijd: ([0-9]{1,2}:[0-9]{1,2})$/im) @Processor(EMAILS_QUEUE_NAME) +@Injectable() export class WaitingListService { constructor( @InjectQueue(RESERVATIONS_QUEUE_NAME)