Changing daily reservations to have 2 attempts and then finally go to waitlist

This commit is contained in:
Collin Duncan 2024-02-23 07:35:35 -06:00
parent 09f05d6758
commit 533080b0ad
No known key found for this signature in database
GPG key ID: 9584E0870D990D97
4 changed files with 39 additions and 32 deletions

View file

@ -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(

View file

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

View file

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