Expanding the reservations job payload to dictate if it should run in speedy mode or not as waiting lists are actually hindered by speedy mode

This commit is contained in:
Collin Duncan 2024-06-20 13:50:01 +02:00
parent d4eb34057c
commit 0a3e712838
No known key found for this signature in database
6 changed files with 25 additions and 11 deletions

View file

@ -1,7 +1,13 @@
import type { Queue } from 'bull'
import type { Job, Queue } from 'bull'
import type { Reservation } from './entity'
export const RESERVATIONS_QUEUE_NAME = 'reservations'
export type ReservationsQueue = Queue<Reservation>
interface ReservationsQueueJobPayload {
reservation: Reservation
speedyMode: boolean
}
export type ReservationsQueue = Queue<ReservationsQueueJobPayload>
export type ReservationsJob = Job<ReservationsQueueJobPayload>

View file

@ -120,7 +120,7 @@ export class ReservationsController {
return 'Reservation saved'
}
this.loggerService.debug('Reservation is available for reservation')
await this.reservationsQueue.add(reservation)
await this.reservationsQueue.add({ reservation, speedyMode: true })
return 'Reservation queued'
}
@ -132,7 +132,7 @@ export class ReservationsController {
if (!reservation.isAvailableForReservation())
throw new HttpException('Not available', 400)
await this.reservationsQueue.add(reservation)
await this.reservationsQueue.add({ reservation, speedyMode: true })
return 'Reservation queued'
}

View file

@ -73,7 +73,7 @@ export class ReservationsCronService {
this.loggerService.debug(`It's go-time`)
await this.reservationsQueue.addBulk(
reservationsToPerform.map((res) => ({
data: res,
data: { reservation: res, speedyMode: true },
opts: { attempts: DAILY_RESERVATIONS_ATTEMPTS },
})),
)

View file

@ -9,7 +9,7 @@ import {
BaanReserverenService,
NoCourtAvailableError,
} from '../runner/baanreserveren/service'
import { RESERVATIONS_QUEUE_NAME } from './config'
import { RESERVATIONS_QUEUE_NAME, ReservationsJob } from './config'
import { DAILY_RESERVATIONS_ATTEMPTS } from './cron'
import { Reservation } from './entity'
import { ReservationsService } from './service'
@ -31,8 +31,8 @@ export class ReservationsWorker {
) {}
@Process()
async handleReservationJob(job: Job<Reservation>) {
const reservation = plainToInstance(Reservation, job.data, {
async handleReservationJob(job: ReservationsJob) {
const reservation = plainToInstance(Reservation, job.data.reservation, {
groups: ['password'],
})
this.loggerService.log('Handling reservation', {
@ -43,7 +43,12 @@ export class ReservationsWorker {
reservation.dateRangeStart,
reservation.dateRangeEnd,
)
await this.performReservation(reservation, job.attemptsMade, true, true)
await this.performReservation(
reservation,
job.attemptsMade,
true,
job.data.speedyMode,
)
}
private async handleReservationErrors(

View file

@ -95,7 +95,10 @@ export class WaitingListService {
)
await this.reservationsQueue.addBulk(
reservations.map((r) => ({ data: r, opts: { attempts: 1 } })),
reservations.map((r) => ({
data: { reservation: r, speedyMode: false },
opts: { attempts: 1 },
})),
)
}

View file

@ -119,7 +119,7 @@ describe('reservations.cron', () => {
it('should perform reservations', () => {
expect(reservationsQueueSpy).toHaveBeenCalledWith([
{
data: stubbedReservation,
data: { reservation: stubbedReservation, speedyMode: true },
opts: { attempts: DAILY_RESERVATIONS_ATTEMPTS },
},
])