5초마다 재고 상태를 확인해주는 스케쥴링을 구해봅시다.
우선 필요한 패키지를 설치 합시다.
> npm install --save @nestjs/schedule
> npm install --save-dev @types/cron
참고
매 5초마다 request를 요청하는 scheduler를 만든다. (넘 심한가...?)
@Injectable()
export class TaskSchedulerService {
private static readonly logger = new Logger(TaskSchedulerService.name);
constructor(
private readonly watcherService: WatcherService,
private readonly schedulerRegistry: SchedulerRegistry,
) {}
@Cron(CronExpression.EVERY_5_SECONDS, {
name: 'V28UE_WATCHING',
})
async handleWatchingStock() {
const requestUrl =
'https://www.jooyonshop.co.kr/goods/goods_view.php?goodsNo=1000000165';
// TaskSchedulerService.logger.debug('아이고난!');
const resultRequest = await this.watcherService.getHttpRequest(requestUrl);
const isSoldOut =
this.watcherService.parseHtmlAndCheckIsSoldOut(resultRequest);
if (!isSoldOut) {
const job = this.schedulerRegistry.getCronJob('V28UE_WATCHING');
job.stop();
TaskSchedulerService.logger.debug('떳따!');
this.watcherService.notify({ text: 'Buy It! Hurry Up!' });
} else {
TaskSchedulerService.logger.debug('아직 안떴따');
}
}
}
참고