From 90ddea4bb5786153c8dabe66590e1e43d4a6f162 Mon Sep 17 00:00:00 2001 From: Collin Duncan <3679940+cgduncan7@users.noreply.github.com> Date: Thu, 14 Mar 2024 12:27:47 +0100 Subject: [PATCH] Allowing monitoring of reservations even if no daily reservations are found and updating some types for better constraints --- src/monitoring/config.ts | 6 +++++ src/reservations/config.ts | 6 +++++ src/reservations/controller.ts | 6 ++--- src/reservations/cron.ts | 42 ++++++++++++++++++++++++---------- src/reservations/module.ts | 4 ++++ src/waitingList/service.ts | 9 +++++--- 6 files changed, 54 insertions(+), 19 deletions(-) diff --git a/src/monitoring/config.ts b/src/monitoring/config.ts index 9d3b186..4650d61 100644 --- a/src/monitoring/config.ts +++ b/src/monitoring/config.ts @@ -1 +1,7 @@ +import type { Queue } from 'bull' + +import type { MonitoringQueueData } from './worker' + export const MONITORING_QUEUE_NAME = 'monitoring' + +export type MonitoringQueue = Queue diff --git a/src/reservations/config.ts b/src/reservations/config.ts index 2f3f2ed..44395e4 100644 --- a/src/reservations/config.ts +++ b/src/reservations/config.ts @@ -1 +1,7 @@ +import type { Queue } from 'bull' + +import type { Reservation } from './entity' + export const RESERVATIONS_QUEUE_NAME = 'reservations' + +export type ReservationsQueue = Queue diff --git a/src/reservations/controller.ts b/src/reservations/controller.ts index 8e5a8b9..dfca529 100644 --- a/src/reservations/controller.ts +++ b/src/reservations/controller.ts @@ -11,15 +11,13 @@ import { Query, UseInterceptors, } from '@nestjs/common' -import { Queue } from 'bull' import { Transform, TransformationType } from 'class-transformer' import { IsBoolean, IsOptional, IsString } from 'class-validator' import { Dayjs } from 'dayjs' import dayjs from '../common/dayjs' import { LoggerService } from '../logger/service.logger' -import { RESERVATIONS_QUEUE_NAME } from './config' -import { Reservation } from './entity' +import { RESERVATIONS_QUEUE_NAME, ReservationsQueue } from './config' import { ReservationsService } from './service' export class GetReservationsQueryParams { @@ -79,7 +77,7 @@ export class ReservationsController { private reservationsService: ReservationsService, @InjectQueue(RESERVATIONS_QUEUE_NAME) - private reservationsQueue: Queue, + private reservationsQueue: ReservationsQueue, @Inject(LoggerService) private loggerService: LoggerService, diff --git a/src/reservations/cron.ts b/src/reservations/cron.ts index 9e71961..e1bde10 100644 --- a/src/reservations/cron.ts +++ b/src/reservations/cron.ts @@ -1,11 +1,14 @@ import { InjectQueue } from '@nestjs/bull' import { Inject, Injectable } from '@nestjs/common' import { Cron, CronExpression } from '@nestjs/schedule' -import { Queue } from 'bull' +import dayjs from '../common/dayjs' import { LoggerService } from '../logger/service.logger' +import { MONITORING_QUEUE_NAME, MonitoringQueue } from '../monitoring/config' +import { MonitorType } from '../monitoring/entity' import { NtfyProvider } from '../ntfy/provider' -import { RESERVATIONS_QUEUE_NAME } from './config' +import { BaanReserverenService } from '../runner/baanreserveren/service' +import { RESERVATIONS_QUEUE_NAME, ReservationsQueue } from './config' import { ReservationsService } from './service' export const DAILY_RESERVATIONS_ATTEMPTS = 2 @@ -16,8 +19,14 @@ export class ReservationsCronService { @Inject(ReservationsService) private readonly reservationService: ReservationsService, + @Inject(BaanReserverenService) + private readonly brService: BaanReserverenService, + @InjectQueue(RESERVATIONS_QUEUE_NAME) - private readonly reservationsQueue: Queue, + private readonly reservationsQueue: ReservationsQueue, + + @InjectQueue(MONITORING_QUEUE_NAME) + private readonly monitoringQueue: MonitoringQueue, @Inject(NtfyProvider) private readonly ntfyProvider: NtfyProvider, @@ -34,15 +43,24 @@ export class ReservationsCronService { this.loggerService.log('handleDailyReservations beginning') await this.ntfyProvider.sendCronStartNotification('handleDailyReservations') const reservationsToPerform = await this.reservationService.getSchedulable() - this.loggerService.log( - `Found ${reservationsToPerform.length} reservations to perform`, - ) - await this.reservationsQueue.addBulk( - reservationsToPerform.map((r) => ({ - data: r, - opts: { attempts: DAILY_RESERVATIONS_ATTEMPTS }, - })), - ) + if (reservationsToPerform.length > 0) { + this.loggerService.log( + `Found ${reservationsToPerform.length} reservations to perform`, + ) + await this.reservationsQueue.addBulk( + reservationsToPerform.map((r) => ({ + data: r, + opts: { attempts: DAILY_RESERVATIONS_ATTEMPTS }, + })), + ) + } else { + this.loggerService.log('Monitoring reservations') + const monitorData = await this.brService.monitorCourtReservations(dayjs()) + await this.monitoringQueue.add({ + type: MonitorType.CourtReservations, + data: monitorData, + }) + } this.loggerService.log('handleDailyReservations ending') await this.ntfyProvider.sendCronStopNotification( 'handleDailyReservations', diff --git a/src/reservations/module.ts b/src/reservations/module.ts index d136ed3..86b8321 100644 --- a/src/reservations/module.ts +++ b/src/reservations/module.ts @@ -3,6 +3,8 @@ import { Module } from '@nestjs/common' import { TypeOrmModule } from '@nestjs/typeorm' import { LoggerModule } from '../logger/module' +import { MONITORING_QUEUE_NAME } from '../monitoring/config' +import { MonitoringModule } from '../monitoring/module' import { NtfyModule } from '../ntfy/module' import { RunnerModule } from '../runner/module' import { RESERVATIONS_QUEUE_NAME } from './config' @@ -16,9 +18,11 @@ import { ReservationsWorker } from './worker' imports: [ LoggerModule, TypeOrmModule.forFeature([Reservation]), + BullModule.registerQueueAsync({ name: MONITORING_QUEUE_NAME }), BullModule.registerQueueAsync({ name: RESERVATIONS_QUEUE_NAME }), RunnerModule, NtfyModule, + MonitoringModule, ], exports: [ReservationsService], controllers: [ReservationsController], diff --git a/src/waitingList/service.ts b/src/waitingList/service.ts index 65998b9..a211acf 100644 --- a/src/waitingList/service.ts +++ b/src/waitingList/service.ts @@ -1,6 +1,6 @@ import { InjectQueue, Process, Processor } from '@nestjs/bull' import { Inject, Injectable } from '@nestjs/common' -import { Job, Queue } from 'bull' +import { Job } from 'bull' import dayjs from '../common/dayjs' import { EMAILS_QUEUE_NAME } from '../email/config' @@ -8,7 +8,10 @@ import { EmailProvider } from '../email/provider' import { Email } from '../email/types' import { LoggerService } from '../logger/service.logger' import { NtfyProvider } from '../ntfy/provider' -import { RESERVATIONS_QUEUE_NAME } from '../reservations/config' +import { + RESERVATIONS_QUEUE_NAME, + ReservationsQueue, +} from '../reservations/config' import { ReservationsService } from '../reservations/service' import { WaitingListDetails } from './types' @@ -29,7 +32,7 @@ const EMAIL_END_TIME_REGEX = new RegExp(/^Eindtijd: ([0-9]{1,2}:[0-9]{1,2})$/im) export class WaitingListService { constructor( @InjectQueue(RESERVATIONS_QUEUE_NAME) - private readonly reservationsQueue: Queue, + private readonly reservationsQueue: ReservationsQueue, @Inject(ReservationsService) private readonly reservationsService: ReservationsService,