Fixing monitors and migrating them to use strings instead of blobs
This commit is contained in:
parent
bbf38c8c2a
commit
28f730cf81
7 changed files with 48 additions and 25 deletions
19
database/migrations/1711456637438-BlobToVarchar.ts
Normal file
19
database/migrations/1711456637438-BlobToVarchar.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import { MigrationInterface, QueryRunner } from 'typeorm'
|
||||
|
||||
export class BlobToVarchar1711456637438 implements MigrationInterface {
|
||||
name = 'BlobToVarchar1711456637438'
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`ALTER TABLE "monitors" DROP COLUMN "data";`)
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "monitors" ADD COLUMN "data" varchar NOT NULL;`,
|
||||
)
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`ALTER TABLE "monitors" DROP COLUMN "data";`)
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "monitors" ADD COLUMN "data" blob NOT NULL;`,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -19,8 +19,9 @@ export class Monitor {
|
|||
|
||||
@Column('datetime', {
|
||||
nullable: false,
|
||||
default: dayjs(),
|
||||
transformer: {
|
||||
to: (value: Dayjs) => value.format(),
|
||||
to: (value?: Dayjs) => (value ?? dayjs()).format(),
|
||||
from: (value: Date) => dayjs(value),
|
||||
},
|
||||
})
|
||||
|
|
@ -37,14 +38,8 @@ export class Monitor {
|
|||
})
|
||||
createdAt: Dayjs
|
||||
|
||||
@Column('blob', {
|
||||
nullable: false,
|
||||
transformer: {
|
||||
to: (value) => value,
|
||||
from: (value) => value,
|
||||
},
|
||||
})
|
||||
data: unknown
|
||||
@Column('varchar', { nullable: false })
|
||||
data: string
|
||||
|
||||
constructor(partial: Partial<Monitor>) {
|
||||
Object.assign(this, partial)
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import { NtfyModule } from '../ntfy/module'
|
|||
import { MONITORING_QUEUE_NAME } from './config'
|
||||
import { Monitor } from './entity'
|
||||
import { MonitorsService } from './service'
|
||||
import { MonitoringWorker } from './worker'
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
|
|
@ -15,7 +16,7 @@ import { MonitorsService } from './service'
|
|||
TypeOrmModule.forFeature([Monitor]),
|
||||
BullModule.registerQueueAsync({ name: MONITORING_QUEUE_NAME }),
|
||||
],
|
||||
providers: [MonitorsService],
|
||||
providers: [MonitorsService, MonitoringWorker],
|
||||
exports: [MonitorsService],
|
||||
})
|
||||
export class MonitoringModule {}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ export class MonitorsService {
|
|||
private readonly monitorsRepository: Repository<Monitor>,
|
||||
) {}
|
||||
|
||||
async performMonitor(type: MonitorType, data: unknown) {
|
||||
async performMonitor(type: MonitorType, data: string) {
|
||||
await this.monitorsRepository.save(
|
||||
this.monitorsRepository.create({ type, data }),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -2,13 +2,14 @@ import { Process, Processor } from '@nestjs/bull'
|
|||
import { Inject } from '@nestjs/common'
|
||||
import { Job } from 'bull'
|
||||
|
||||
import { LoggerService } from '../logger/service.logger'
|
||||
import { MONITORING_QUEUE_NAME } from './config'
|
||||
import { MonitorType } from './entity'
|
||||
import { MonitorsService } from './service'
|
||||
|
||||
export interface MonitoringQueueData {
|
||||
type: MonitorType
|
||||
data: unknown
|
||||
data: any
|
||||
}
|
||||
|
||||
@Processor(MONITORING_QUEUE_NAME)
|
||||
|
|
@ -16,10 +17,26 @@ export class MonitoringWorker {
|
|||
constructor(
|
||||
@Inject(MonitorsService)
|
||||
private readonly monitorsService: MonitorsService,
|
||||
|
||||
@Inject(LoggerService)
|
||||
private readonly loggerService: LoggerService,
|
||||
) {}
|
||||
|
||||
@Process()
|
||||
async handleMonitoringCourtsJob(job: Job<MonitoringQueueData>) {
|
||||
await this.monitorsService.performMonitor(job.data.type, job.data.data)
|
||||
let json
|
||||
try {
|
||||
json = JSON.stringify(job.data.data)
|
||||
} catch (error) {
|
||||
this.loggerService.error('Could not stringify data')
|
||||
return
|
||||
}
|
||||
await this.monitorsService
|
||||
.performMonitor(job.data.type, json)
|
||||
.catch((error) =>
|
||||
this.loggerService.error(
|
||||
`Failed to monitor courts: ${(error as Error).message}`,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,9 +25,6 @@ export class ReservationsCronService {
|
|||
@InjectQueue(RESERVATIONS_QUEUE_NAME)
|
||||
private readonly reservationsQueue: ReservationsQueue,
|
||||
|
||||
@InjectQueue(MONITORING_QUEUE_NAME)
|
||||
private readonly monitoringQueue: MonitoringQueue,
|
||||
|
||||
@Inject(NtfyProvider)
|
||||
private readonly ntfyProvider: NtfyProvider,
|
||||
|
||||
|
|
@ -55,11 +52,7 @@ export class ReservationsCronService {
|
|||
)
|
||||
} else {
|
||||
this.loggerService.log('Monitoring reservations')
|
||||
const monitorData = await this.brService.monitorCourtReservations(dayjs())
|
||||
await this.monitoringQueue.add({
|
||||
type: MonitorType.CourtReservations,
|
||||
data: monitorData,
|
||||
})
|
||||
await this.brService.monitorCourtReservations(dayjs())
|
||||
}
|
||||
this.loggerService.log('handleDailyReservations ending')
|
||||
await this.ntfyProvider.sendCronStopNotification(
|
||||
|
|
|
|||
|
|
@ -1,16 +1,14 @@
|
|||
import { InjectQueue } from '@nestjs/bull'
|
||||
import { Inject, Injectable } from '@nestjs/common'
|
||||
import { ConfigService } from '@nestjs/config'
|
||||
import { Queue } from 'bull'
|
||||
import { instanceToPlain } from 'class-transformer'
|
||||
import { Dayjs } from 'dayjs'
|
||||
import { ElementHandle, Page } from 'puppeteer'
|
||||
|
||||
import dayjs from '../../common/dayjs'
|
||||
import { LoggerService } from '../../logger/service.logger'
|
||||
import { MONITORING_QUEUE_NAME } from '../../monitoring/config'
|
||||
import { MONITORING_QUEUE_NAME, MonitoringQueue } from '../../monitoring/config'
|
||||
import { MonitorType } from '../../monitoring/entity'
|
||||
import { MonitoringQueueData } from '../../monitoring/worker'
|
||||
import { Reservation } from '../../reservations/entity'
|
||||
import { EmptyPage } from '../pages/empty'
|
||||
|
||||
|
|
@ -95,7 +93,7 @@ export class BaanReserverenService {
|
|||
|
||||
constructor(
|
||||
@InjectQueue(MONITORING_QUEUE_NAME)
|
||||
private readonly monitoringQueue: Queue<MonitoringQueueData>,
|
||||
private readonly monitoringQueue: MonitoringQueue,
|
||||
|
||||
@Inject(LoggerService)
|
||||
private readonly loggerService: LoggerService,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue