Vitalik
9 months ago
13 changed files with 249 additions and 29 deletions
@ -0,0 +1 @@
@@ -0,0 +1 @@
|
||||
export * from './tasks-dedlines.cron' |
@ -0,0 +1,88 @@
@@ -0,0 +1,88 @@
|
||||
import { Inject, Injectable } from '@nestjs/common' |
||||
import { TASKS_REPOSITORY } from '../consts' |
||||
import { ITasksRepository } from '../interfaces' |
||||
import { Cron } from 'src/shared' |
||||
import * as moment from 'moment' |
||||
import { Tasks } from 'src/core' |
||||
import { EventEmitter2 } from '@nestjs/event-emitter' |
||||
import { Events } from 'src/core/enums' |
||||
|
||||
@Injectable() |
||||
export class TasksDedlinesCron extends Cron { |
||||
constructor( |
||||
@Inject(TASKS_REPOSITORY) |
||||
private readonly tasksRepository: ITasksRepository, |
||||
|
||||
private eventEmitter: EventEmitter2, |
||||
) { |
||||
super() |
||||
} |
||||
|
||||
protected register(): void { |
||||
this.start('0 10 * * *', this.everyDayBody.bind(this)) |
||||
this.start('0 10 * * 0', this.everyWeekBody.bind(this)) |
||||
} |
||||
|
||||
protected async everyDayBody() { |
||||
try { |
||||
await this.findPreDeadlineTasks() |
||||
} catch (e) {} |
||||
|
||||
try { |
||||
await this.findExpiredTasks() |
||||
} catch (e) {} |
||||
} |
||||
|
||||
protected async everyWeekBody() { |
||||
const now = moment(new Date()).format('YYYY-MM-DD') |
||||
|
||||
const tasks = await this.tasksRepository |
||||
.createQueryBuilder('it') |
||||
.where('it.endDate < :now', { now }) |
||||
.andWhere('it.doneDate IS NULL') |
||||
.andWhere('it.status = :status', { status: Tasks.Status.Active }) |
||||
.getMany() |
||||
|
||||
console.log('everyWeekBody', tasks) |
||||
for await (const task of tasks) { |
||||
this.eventEmitter.emit(Events.OnTaskDeadlineExpired, { |
||||
task, |
||||
}) |
||||
} |
||||
} |
||||
|
||||
protected async findPreDeadlineTasks() { |
||||
const tomorrowDate = moment(new Date()).add(1, 'days').format('YYYY-MM-DD') |
||||
const tasks = await this.tasksRepository |
||||
.createQueryBuilder('it') |
||||
.where('it.endDate = :endDate', { endDate: tomorrowDate }) |
||||
.andWhere('it.doneDate IS NULL') |
||||
.andWhere('it.status = :status', { status: Tasks.Status.Active }) |
||||
.getMany() |
||||
|
||||
console.log('findPreDeadlineTasks', tasks) |
||||
for await (const task of tasks) { |
||||
this.eventEmitter.emit(Events.OnTaskDeadlineSoon, { |
||||
task, |
||||
}) |
||||
} |
||||
} |
||||
|
||||
protected async findExpiredTasks() { |
||||
const date = moment(new Date()).subtract(1, 'days').format('YYYY-MM-DD') |
||||
const tasks = await this.tasksRepository |
||||
.createQueryBuilder('it') |
||||
.where('it.endDate = :endDate', { endDate: date }) |
||||
.andWhere('it.doneDate IS NULL') |
||||
.andWhere('it.status = :status', { status: Tasks.Status.Active }) |
||||
.getMany() |
||||
|
||||
console.log('findExpiredTasks', tasks) |
||||
|
||||
for await (const task of tasks) { |
||||
this.eventEmitter.emit(Events.OnTaskDeadlineExpired, { |
||||
task, |
||||
}) |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,31 @@
@@ -0,0 +1,31 @@
|
||||
import { OnModuleDestroy } from '@nestjs/common' |
||||
import { CronJob } from 'cron' |
||||
|
||||
export abstract class Cron implements OnModuleDestroy { |
||||
static isEnabled = null |
||||
|
||||
static setup(isEnabled: boolean) { |
||||
this.isEnabled = isEnabled |
||||
} |
||||
|
||||
protected abstract register(): void |
||||
|
||||
protected job: CronJob |
||||
|
||||
onModuleInit() { |
||||
if (Cron.isEnabled === null) throw new Error('Should setup Cron by Cron.setup') |
||||
if (Cron.isEnabled) this.register() |
||||
} |
||||
|
||||
onModuleDestroy() { |
||||
try { |
||||
if (this.job) this.job.stop() |
||||
} catch (e) {} |
||||
} |
||||
|
||||
protected start(time: string, fn: () => void) { |
||||
this.job = new CronJob(time, fn, null, true, 'UTC+2') |
||||
|
||||
this.job.start() |
||||
} |
||||
} |
Loading…
Reference in new issue