feat: 支持秒杀活动
This commit is contained in:
@@ -1,12 +1,16 @@
|
||||
import { Injectable, Logger } from '@nestjs/common'
|
||||
import { Cron } from '@nestjs/schedule'
|
||||
import { SlotGeneratorService } from '../time-slot/slot-generator.service'
|
||||
import { FlashSaleService } from '../flash-sale/flash-sale.service'
|
||||
|
||||
@Injectable()
|
||||
export class SchedulerService {
|
||||
private readonly logger = new Logger(SchedulerService.name)
|
||||
|
||||
constructor(private readonly slotGenerator: SlotGeneratorService) {}
|
||||
constructor(
|
||||
private readonly slotGenerator: SlotGeneratorService,
|
||||
private readonly flashSaleService: FlashSaleService,
|
||||
) {}
|
||||
|
||||
/** 02:00 daily — generate slots 14 days ahead from week templates */
|
||||
@Cron('0 2 * * *')
|
||||
@@ -51,4 +55,17 @@ export class SchedulerService {
|
||||
this.logger.error('[handleCompleteBookings] Failed to complete bookings', err)
|
||||
}
|
||||
}
|
||||
|
||||
/** Every 5 min — expire unpaid flash sale reservations older than 15min */
|
||||
@Cron('*/5 * * * *')
|
||||
async handleExpireFlashSaleReservations(): Promise<void> {
|
||||
try {
|
||||
const count = await this.flashSaleService.expireUnpaidReservations(15)
|
||||
if (count > 0) {
|
||||
this.logger.log(`[handleExpireFlashSaleReservations] Expired ${count} unpaid reservations`)
|
||||
}
|
||||
} catch (err) {
|
||||
this.logger.error('[handleExpireFlashSaleReservations] Failed', err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user