From c6429c6d3d355f48a9d5f259a99c2402b4dab349 Mon Sep 17 00:00:00 2001 From: Collin Duncan <3679940+cgduncan7@users.noreply.github.com> Date: Tue, 14 Jan 2025 10:54:55 +0100 Subject: [PATCH] Adding a retry to warmup --- src/runner/baanreserveren/service.ts | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/runner/baanreserveren/service.ts b/src/runner/baanreserveren/service.ts index 3c43473..f7d528a 100644 --- a/src/runner/baanreserveren/service.ts +++ b/src/runner/baanreserveren/service.ts @@ -402,7 +402,7 @@ export class BaanReserverenService { await this.page .goto(`${BAAN_RESERVEREN_ROOT_URL}/${BaanReserverenUrls.Reservations}`) .catch((e) => { - throw new RunnerWaitingListNavigationError(e) + throw new RunningReservationsNavigationError(e) }) await this.page.waitForNetworkIdle() } @@ -660,6 +660,7 @@ export class BaanReserverenService { const classList = Object.values(classListObj) const rClass = classList.filter((cl) => /r-\d{2}/.test(cl))[0] const courtNumber = + // @ts-expect-error Can be null `${CourtSlotToNumber[rClass.replace(/r-/, '') as CourtSlot]}` ?? 'unknown court' const startTime = await court @@ -809,7 +810,21 @@ export class BaanReserverenService { } public async warmup() { - await this.init() + const attempts = 10 + const delay = 1000 + let currentAttempt = 1 + while (currentAttempt < attempts) { + try { + currentAttempt++ + await this.init() + } catch (err: unknown) { + if (err instanceof RunningReservationsNavigationError) { + await new Promise((res) => setTimeout(res, delay)) + } else { + throw err + } + } + } } } @@ -885,6 +900,12 @@ export class RunnerNavigationSelectionError extends RunnerError { } } +export class RunningReservationsNavigationError extends RunnerError { + constructor(error: Error) { + super(error, 'RunnerReservationsNavigationError') + } +} + export class RunnerWaitingListNavigationError extends RunnerError { constructor(error: Error) { super(error, 'RunnerWaitingListNavigationError')