2023-05-26 15:43:14 -05:00
|
|
|
import { InjectQueue } from '@nestjs/bull'
|
|
|
|
|
import { Inject, Injectable } from '@nestjs/common'
|
|
|
|
|
import { Cron, CronExpression } from '@nestjs/schedule'
|
2023-06-29 10:32:09 +02:00
|
|
|
|
2024-03-14 12:27:47 +01:00
|
|
|
import dayjs from '../common/dayjs'
|
2023-08-29 10:44:12 +02:00
|
|
|
import { LoggerService } from '../logger/service.logger'
|
2024-03-14 12:27:47 +01:00
|
|
|
import { MONITORING_QUEUE_NAME, MonitoringQueue } from '../monitoring/config'
|
|
|
|
|
import { MonitorType } from '../monitoring/entity'
|
2023-09-06 11:23:21 +02:00
|
|
|
import { NtfyProvider } from '../ntfy/provider'
|
2024-03-14 12:27:47 +01:00
|
|
|
import { BaanReserverenService } from '../runner/baanreserveren/service'
|
|
|
|
|
import { RESERVATIONS_QUEUE_NAME, ReservationsQueue } from './config'
|
2023-05-26 15:43:14 -05:00
|
|
|
import { ReservationsService } from './service'
|
|
|
|
|
|
2024-02-23 07:35:35 -06:00
|
|
|
export const DAILY_RESERVATIONS_ATTEMPTS = 2
|
|
|
|
|
|
2023-05-26 15:43:14 -05:00
|
|
|
@Injectable()
|
|
|
|
|
export class ReservationsCronService {
|
|
|
|
|
constructor(
|
|
|
|
|
@Inject(ReservationsService)
|
|
|
|
|
private readonly reservationService: ReservationsService,
|
2023-06-27 16:06:19 +02:00
|
|
|
|
2024-03-14 12:27:47 +01:00
|
|
|
@Inject(BaanReserverenService)
|
|
|
|
|
private readonly brService: BaanReserverenService,
|
|
|
|
|
|
2023-05-26 15:43:14 -05:00
|
|
|
@InjectQueue(RESERVATIONS_QUEUE_NAME)
|
2024-03-14 12:27:47 +01:00
|
|
|
private readonly reservationsQueue: ReservationsQueue,
|
|
|
|
|
|
2023-09-06 11:23:21 +02:00
|
|
|
@Inject(NtfyProvider)
|
|
|
|
|
private readonly ntfyProvider: NtfyProvider,
|
|
|
|
|
|
2023-05-26 15:43:14 -05:00
|
|
|
@Inject(LoggerService)
|
2023-07-29 14:58:48 +02:00
|
|
|
private readonly loggerService: LoggerService,
|
2023-05-26 15:43:14 -05:00
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
@Cron(CronExpression.EVERY_DAY_AT_7AM, {
|
|
|
|
|
name: 'handleDailyReservations',
|
|
|
|
|
timeZone: 'Europe/Amsterdam',
|
|
|
|
|
})
|
|
|
|
|
async handleDailyReservations() {
|
2023-08-29 10:44:12 +02:00
|
|
|
this.loggerService.log('handleDailyReservations beginning')
|
2023-09-06 11:23:21 +02:00
|
|
|
await this.ntfyProvider.sendCronStartNotification('handleDailyReservations')
|
2023-09-13 12:01:06 +02:00
|
|
|
const reservationsToPerform = await this.reservationService.getSchedulable()
|
2024-03-14 12:27:47 +01:00
|
|
|
if (reservationsToPerform.length > 0) {
|
|
|
|
|
this.loggerService.log(
|
|
|
|
|
`Found ${reservationsToPerform.length} reservations to perform`,
|
|
|
|
|
)
|
|
|
|
|
await this.reservationsQueue.addBulk(
|
|
|
|
|
reservationsToPerform.map((r) => ({
|
|
|
|
|
data: r,
|
|
|
|
|
opts: { attempts: DAILY_RESERVATIONS_ATTEMPTS },
|
|
|
|
|
})),
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
this.loggerService.log('Monitoring reservations')
|
2024-03-26 14:05:33 +01:00
|
|
|
await this.brService.monitorCourtReservations(dayjs())
|
2024-03-14 12:27:47 +01:00
|
|
|
}
|
2023-08-29 10:44:12 +02:00
|
|
|
this.loggerService.log('handleDailyReservations ending')
|
2023-09-06 11:23:21 +02:00
|
|
|
await this.ntfyProvider.sendCronStopNotification(
|
|
|
|
|
'handleDailyReservations',
|
|
|
|
|
`Count: ${reservationsToPerform.length}`,
|
|
|
|
|
)
|
2023-06-27 16:06:19 +02:00
|
|
|
}
|
2023-07-29 15:23:13 +02:00
|
|
|
|
|
|
|
|
@Cron(CronExpression.EVERY_DAY_AT_11PM, {
|
|
|
|
|
name: 'cleanUpExpiredReservations',
|
|
|
|
|
timeZone: 'Europe/Amsterdam',
|
|
|
|
|
})
|
|
|
|
|
async cleanUpExpiredReservations() {
|
2023-08-29 10:44:12 +02:00
|
|
|
this.loggerService.log('cleanUpExpiredReservations beginning')
|
2023-09-06 11:23:21 +02:00
|
|
|
await this.ntfyProvider.sendCronStartNotification(
|
|
|
|
|
'cleanUpExpiredReservations',
|
|
|
|
|
)
|
2023-07-29 15:23:13 +02:00
|
|
|
const reservations = await this.reservationService.getByDate()
|
|
|
|
|
this.loggerService.log(
|
|
|
|
|
`Found ${reservations.length} reservations to delete`,
|
|
|
|
|
)
|
|
|
|
|
for (const reservation of reservations) {
|
|
|
|
|
await this.reservationService.deleteById(reservation.id)
|
|
|
|
|
}
|
2023-08-29 10:44:12 +02:00
|
|
|
this.loggerService.log('cleanUpExpiredReservations ending')
|
2023-09-06 11:23:21 +02:00
|
|
|
await this.ntfyProvider.sendCronStopNotification(
|
|
|
|
|
'cleanUpExpiredReservations',
|
|
|
|
|
`Count: ${reservations.length}`,
|
|
|
|
|
)
|
2023-07-29 15:23:13 +02:00
|
|
|
}
|
2023-05-26 15:43:14 -05:00
|
|
|
}
|