Allowing monitoring of reservations even if no daily reservations are found and updating some types for better constraints

This commit is contained in:
Collin Duncan 2024-03-14 12:27:47 +01:00
parent 6d35307cd6
commit 90ddea4bb5
No known key found for this signature in database
6 changed files with 54 additions and 19 deletions

View file

@ -1 +1,7 @@
import type { Queue } from 'bull'
import type { MonitoringQueueData } from './worker'
export const MONITORING_QUEUE_NAME = 'monitoring' export const MONITORING_QUEUE_NAME = 'monitoring'
export type MonitoringQueue = Queue<MonitoringQueueData>

View file

@ -1 +1,7 @@
import type { Queue } from 'bull'
import type { Reservation } from './entity'
export const RESERVATIONS_QUEUE_NAME = 'reservations' export const RESERVATIONS_QUEUE_NAME = 'reservations'
export type ReservationsQueue = Queue<Reservation>

View file

@ -11,15 +11,13 @@ import {
Query, Query,
UseInterceptors, UseInterceptors,
} from '@nestjs/common' } from '@nestjs/common'
import { Queue } from 'bull'
import { Transform, TransformationType } from 'class-transformer' import { Transform, TransformationType } from 'class-transformer'
import { IsBoolean, IsOptional, IsString } from 'class-validator' import { IsBoolean, IsOptional, IsString } from 'class-validator'
import { Dayjs } from 'dayjs' import { Dayjs } from 'dayjs'
import dayjs from '../common/dayjs' import dayjs from '../common/dayjs'
import { LoggerService } from '../logger/service.logger' import { LoggerService } from '../logger/service.logger'
import { RESERVATIONS_QUEUE_NAME } from './config' import { RESERVATIONS_QUEUE_NAME, ReservationsQueue } from './config'
import { Reservation } from './entity'
import { ReservationsService } from './service' import { ReservationsService } from './service'
export class GetReservationsQueryParams { export class GetReservationsQueryParams {
@ -79,7 +77,7 @@ export class ReservationsController {
private reservationsService: ReservationsService, private reservationsService: ReservationsService,
@InjectQueue(RESERVATIONS_QUEUE_NAME) @InjectQueue(RESERVATIONS_QUEUE_NAME)
private reservationsQueue: Queue<Reservation>, private reservationsQueue: ReservationsQueue,
@Inject(LoggerService) @Inject(LoggerService)
private loggerService: LoggerService, private loggerService: LoggerService,

View file

@ -1,11 +1,14 @@
import { InjectQueue } from '@nestjs/bull' import { InjectQueue } from '@nestjs/bull'
import { Inject, Injectable } from '@nestjs/common' import { Inject, Injectable } from '@nestjs/common'
import { Cron, CronExpression } from '@nestjs/schedule' import { Cron, CronExpression } from '@nestjs/schedule'
import { Queue } from 'bull'
import dayjs from '../common/dayjs'
import { LoggerService } from '../logger/service.logger' 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 { 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' import { ReservationsService } from './service'
export const DAILY_RESERVATIONS_ATTEMPTS = 2 export const DAILY_RESERVATIONS_ATTEMPTS = 2
@ -16,8 +19,14 @@ export class ReservationsCronService {
@Inject(ReservationsService) @Inject(ReservationsService)
private readonly reservationService: ReservationsService, private readonly reservationService: ReservationsService,
@Inject(BaanReserverenService)
private readonly brService: BaanReserverenService,
@InjectQueue(RESERVATIONS_QUEUE_NAME) @InjectQueue(RESERVATIONS_QUEUE_NAME)
private readonly reservationsQueue: Queue, private readonly reservationsQueue: ReservationsQueue,
@InjectQueue(MONITORING_QUEUE_NAME)
private readonly monitoringQueue: MonitoringQueue,
@Inject(NtfyProvider) @Inject(NtfyProvider)
private readonly ntfyProvider: NtfyProvider, private readonly ntfyProvider: NtfyProvider,
@ -34,15 +43,24 @@ export class ReservationsCronService {
this.loggerService.log('handleDailyReservations beginning') this.loggerService.log('handleDailyReservations beginning')
await this.ntfyProvider.sendCronStartNotification('handleDailyReservations') await this.ntfyProvider.sendCronStartNotification('handleDailyReservations')
const reservationsToPerform = await this.reservationService.getSchedulable() const reservationsToPerform = await this.reservationService.getSchedulable()
this.loggerService.log( if (reservationsToPerform.length > 0) {
`Found ${reservationsToPerform.length} reservations to perform`, this.loggerService.log(
) `Found ${reservationsToPerform.length} reservations to perform`,
await this.reservationsQueue.addBulk( )
reservationsToPerform.map((r) => ({ await this.reservationsQueue.addBulk(
data: r, reservationsToPerform.map((r) => ({
opts: { attempts: DAILY_RESERVATIONS_ATTEMPTS }, 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') this.loggerService.log('handleDailyReservations ending')
await this.ntfyProvider.sendCronStopNotification( await this.ntfyProvider.sendCronStopNotification(
'handleDailyReservations', 'handleDailyReservations',

View file

@ -3,6 +3,8 @@ import { Module } from '@nestjs/common'
import { TypeOrmModule } from '@nestjs/typeorm' import { TypeOrmModule } from '@nestjs/typeorm'
import { LoggerModule } from '../logger/module' import { LoggerModule } from '../logger/module'
import { MONITORING_QUEUE_NAME } from '../monitoring/config'
import { MonitoringModule } from '../monitoring/module'
import { NtfyModule } from '../ntfy/module' import { NtfyModule } from '../ntfy/module'
import { RunnerModule } from '../runner/module' import { RunnerModule } from '../runner/module'
import { RESERVATIONS_QUEUE_NAME } from './config' import { RESERVATIONS_QUEUE_NAME } from './config'
@ -16,9 +18,11 @@ import { ReservationsWorker } from './worker'
imports: [ imports: [
LoggerModule, LoggerModule,
TypeOrmModule.forFeature([Reservation]), TypeOrmModule.forFeature([Reservation]),
BullModule.registerQueueAsync({ name: MONITORING_QUEUE_NAME }),
BullModule.registerQueueAsync({ name: RESERVATIONS_QUEUE_NAME }), BullModule.registerQueueAsync({ name: RESERVATIONS_QUEUE_NAME }),
RunnerModule, RunnerModule,
NtfyModule, NtfyModule,
MonitoringModule,
], ],
exports: [ReservationsService], exports: [ReservationsService],
controllers: [ReservationsController], controllers: [ReservationsController],

View file

@ -1,6 +1,6 @@
import { InjectQueue, Process, Processor } from '@nestjs/bull' import { InjectQueue, Process, Processor } from '@nestjs/bull'
import { Inject, Injectable } from '@nestjs/common' import { Inject, Injectable } from '@nestjs/common'
import { Job, Queue } from 'bull' import { Job } from 'bull'
import dayjs from '../common/dayjs' import dayjs from '../common/dayjs'
import { EMAILS_QUEUE_NAME } from '../email/config' import { EMAILS_QUEUE_NAME } from '../email/config'
@ -8,7 +8,10 @@ import { EmailProvider } from '../email/provider'
import { Email } from '../email/types' import { Email } from '../email/types'
import { LoggerService } from '../logger/service.logger' import { LoggerService } from '../logger/service.logger'
import { NtfyProvider } from '../ntfy/provider' 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 { ReservationsService } from '../reservations/service'
import { WaitingListDetails } from './types' 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 { export class WaitingListService {
constructor( constructor(
@InjectQueue(RESERVATIONS_QUEUE_NAME) @InjectQueue(RESERVATIONS_QUEUE_NAME)
private readonly reservationsQueue: Queue, private readonly reservationsQueue: ReservationsQueue,
@Inject(ReservationsService) @Inject(ReservationsService)
private readonly reservationsService: ReservationsService, private readonly reservationsService: ReservationsService,