Creating job to record monitor during reservation

This commit is contained in:
Collin Duncan 2024-03-14 08:06:51 +01:00
parent 2d106deb77
commit 203dea910b
No known key found for this signature in database
3 changed files with 28 additions and 6 deletions

View file

@ -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<MonitoringQueueData>) {
await this.monitorsService.performMonitor(job.data.type, job.data.data)
}
}

View file

@ -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<MonitoringQueueData>,
@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) {

View file

@ -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],
})