From 533080b0ad69ef343de19597cbcb8f7b4cd36206 Mon Sep 17 00:00:00 2001 From: Collin Duncan <3679940+cgduncan7@users.noreply.github.com> Date: Fri, 23 Feb 2024 07:35:35 -0600 Subject: [PATCH] Changing daily reservations to have 2 attempts and then finally go to waitlist --- src/reservations/cron.ts | 7 +++- src/reservations/worker.ts | 50 +++++++++++++--------------- src/runner/baanreserveren/service.ts | 12 ++++--- src/runner/pages/empty.ts | 2 +- 4 files changed, 39 insertions(+), 32 deletions(-) diff --git a/src/reservations/cron.ts b/src/reservations/cron.ts index c4d333b..9e71961 100644 --- a/src/reservations/cron.ts +++ b/src/reservations/cron.ts @@ -8,6 +8,8 @@ import { NtfyProvider } from '../ntfy/provider' import { RESERVATIONS_QUEUE_NAME } from './config' import { ReservationsService } from './service' +export const DAILY_RESERVATIONS_ATTEMPTS = 2 + @Injectable() export class ReservationsCronService { constructor( @@ -36,7 +38,10 @@ export class ReservationsCronService { `Found ${reservationsToPerform.length} reservations to perform`, ) await this.reservationsQueue.addBulk( - reservationsToPerform.map((r) => ({ data: r, opts: { attempts: 1 } })), + reservationsToPerform.map((r) => ({ + data: r, + opts: { attempts: DAILY_RESERVATIONS_ATTEMPTS }, + })), ) this.loggerService.log('handleDailyReservations ending') await this.ntfyProvider.sendCronStopNotification( diff --git a/src/reservations/worker.ts b/src/reservations/worker.ts index 63039b8..7b8de6a 100644 --- a/src/reservations/worker.ts +++ b/src/reservations/worker.ts @@ -12,6 +12,7 @@ import { import { RESERVATIONS_QUEUE_NAME } from './config' import { Reservation } from './entity' import { ReservationsService } from './service' +import { DAILY_RESERVATIONS_ATTEMPTS } from './cron' @Processor(RESERVATIONS_QUEUE_NAME) export class ReservationsWorker { @@ -42,45 +43,42 @@ export class ReservationsWorker { reservation.dateRangeStart, reservation.dateRangeEnd, ) - await this.performReservation(reservation) + await this.performReservation(reservation, job.attemptsMade) } private async handleReservationErrors( error: Error, reservation: Reservation, + attemptsMade: number, ) { - switch (true) { - case error instanceof NoCourtAvailableError: { - this.loggerService.warn('No court available') - if (!reservation.waitListed) { - this.loggerService.log('Adding reservation to waiting list') - await this.ntfyProvider.sendReservationWaitlistedNotification( - reservation.id, - reservation.dateRangeStart, - reservation.dateRangeEnd, - ) - await this.addReservationToWaitList(reservation) - } - return - } - default: - this.loggerService.error('Error while performing reservation', error) - await this.ntfyProvider.sendErrorPerformingReservationNotification( - reservation.id, - reservation.dateRangeStart, - reservation.dateRangeEnd, - error, - ) - throw error + if (error instanceof NoCourtAvailableError) { + this.loggerService.warn('No court available') + } + this.loggerService.error('Error while performing reservation', error) + if ( + attemptsMade === DAILY_RESERVATIONS_ATTEMPTS && + !reservation.waitListed + ) { + this.loggerService.log('Adding reservation to waiting list') + await this.ntfyProvider.sendReservationWaitlistedNotification( + reservation.id, + reservation.dateRangeStart, + reservation.dateRangeEnd, + ) + await this.brService.addReservationToWaitList(reservation) } } - async performReservation(reservation: Reservation) { + async performReservation(reservation: Reservation, attemptsMade: number) { try { await this.brService.performReservation(reservation) await this.reservationsService.deleteById(reservation.id) } catch (error: unknown) { - await this.handleReservationErrors(error as Error, reservation) + await this.handleReservationErrors( + error as Error, + reservation, + attemptsMade, + ) } } diff --git a/src/runner/baanreserveren/service.ts b/src/runner/baanreserveren/service.ts index 95c1dd5..d83d4e0 100644 --- a/src/runner/baanreserveren/service.ts +++ b/src/runner/baanreserveren/service.ts @@ -83,10 +83,14 @@ export class BaanReserverenService { } private async handleError() { - await this.page.screenshot({ - type: 'png', - path: path.resolve('.', `${Date.now()}_error-screenshot.png`), - }).catch((reason: any) => this.loggerService.warn('Failed to take screenshot', { reason })) + await this.page + .screenshot({ + type: 'png', + path: path.resolve('.', `${Date.now()}_error-screenshot.png`), + }) + .catch((reason: any) => + this.loggerService.warn('Failed to take screenshot', { reason }), + ) } private async checkSession(username: string) { diff --git a/src/runner/pages/empty.ts b/src/runner/pages/empty.ts index d4ccb66..50f6beb 100644 --- a/src/runner/pages/empty.ts +++ b/src/runner/pages/empty.ts @@ -10,7 +10,7 @@ export const EmptyPageFactory: FactoryProvider = { useFactory: async (runnerService: RunnerService) => { const browser = await runnerService.getBrowser() const page = await browser.newPage() - + // Default navigation timeout and loading timeout of 5s page.setDefaultNavigationTimeout(5_000) page.setDefaultTimeout(5_000)