diff --git a/src/monitoring/worker.ts b/src/monitoring/worker.ts index 0ef8bb4..e7909d1 100644 --- a/src/monitoring/worker.ts +++ b/src/monitoring/worker.ts @@ -6,6 +6,11 @@ import { MONITORING_QUEUE_NAME } from './config' import { MonitorType } from './entity' import { MonitorsService } from './service' +export interface MonitoringQueueData { + type: MonitorType + data: unknown +} + @Processor(MONITORING_QUEUE_NAME) export class MonitoringWorker { constructor( @@ -14,9 +19,7 @@ export class MonitoringWorker { ) {} @Process() - async handleMonitoringCourtsJob( - job: Job<{ type: MonitorType; data: unknown }>, - ) { + async handleMonitoringCourtsJob(job: Job) { await this.monitorsService.performMonitor(job.data.type, job.data.data) } } diff --git a/src/runner/baanreserveren/service.ts b/src/runner/baanreserveren/service.ts index 47eabdd..d230d58 100644 --- a/src/runner/baanreserveren/service.ts +++ b/src/runner/baanreserveren/service.ts @@ -1,5 +1,7 @@ +import { InjectQueue } from '@nestjs/bull' import { Inject, Injectable } from '@nestjs/common' import { ConfigService } from '@nestjs/config' +import { Queue } from 'bull' import { instanceToPlain } from 'class-transformer' import { Dayjs } from 'dayjs' import path from 'path' @@ -7,6 +9,9 @@ import { ElementHandle, Page } from 'puppeteer' import dayjs from '../../common/dayjs' import { LoggerService } from '../../logger/service.logger' +import { MONITORING_QUEUE_NAME } from '../../monitoring/config' +import { MonitorType } from '../../monitoring/entity' +import { MonitoringQueueData } from '../../monitoring/worker' import { Reservation } from '../../reservations/entity' import { EmptyPage } from '../pages/empty' @@ -90,6 +95,9 @@ export class BaanReserverenService { private password: string constructor( + @InjectQueue(MONITORING_QUEUE_NAME) + private readonly monitoringQueue: Queue, + @Inject(LoggerService) private readonly loggerService: LoggerService, @@ -598,7 +606,11 @@ export class BaanReserverenService { await this.init() await this.navigateToDay(date) } - return await this.getAllCourtStatuses() + const statuses = await this.getAllCourtStatuses() + await this.monitoringQueue.add({ + type: MonitorType.CourtReservations, + data: statuses, + }) } catch (error: unknown) { this.loggerService.error('Failed to monitor court reservations') if (!swallowError) { diff --git a/src/runner/module.ts b/src/runner/module.ts index 754384d..73f9ec2 100644 --- a/src/runner/module.ts +++ b/src/runner/module.ts @@ -2,15 +2,22 @@ import { BullModule } from '@nestjs/bull' import { Module } from '@nestjs/common' import { LoggerModule } from '../logger/module' +import { MONITORING_QUEUE_NAME } from '../monitoring/config' +import { MonitoringModule } from '../monitoring/module' import { BaanReserverenService } from './baanreserveren/service' import { EmptyPageFactory } from './pages/empty' import { RunnerService } from './service' @Module({ - providers: [RunnerService, BaanReserverenService, EmptyPageFactory], + providers: [ + RunnerService, + BaanReserverenService, + MonitoringModule, + EmptyPageFactory, + ], imports: [ LoggerModule, - BullModule.registerQueueAsync({ name: 'reservations' }), + BullModule.registerQueueAsync({ name: MONITORING_QUEUE_NAME }), ], exports: [EmptyPageFactory, BaanReserverenService], })