|
@@ -1,6 +1,7 @@
|
|
|
import { Injectable, OnApplicationBootstrap } from '@nestjs/common';
|
|
import { Injectable, OnApplicationBootstrap } from '@nestjs/common';
|
|
|
import CronTime from 'cron-time-generator';
|
|
import CronTime from 'cron-time-generator';
|
|
|
import { Cron } from 'croner';
|
|
import { Cron } from 'croner';
|
|
|
|
|
+import cronstrue from 'cronstrue';
|
|
|
|
|
|
|
|
import { ConfigService } from '../config/config.service';
|
|
import { ConfigService } from '../config/config.service';
|
|
|
import { Logger } from '../config/logger/vendure-logger';
|
|
import { Logger } from '../config/logger/vendure-logger';
|
|
@@ -8,9 +9,19 @@ import { Logger } from '../config/logger/vendure-logger';
|
|
|
import { NoopSchedulerStrategy } from './noop-scheduler-strategy';
|
|
import { NoopSchedulerStrategy } from './noop-scheduler-strategy';
|
|
|
import { ScheduledTask } from './scheduled-task';
|
|
import { ScheduledTask } from './scheduled-task';
|
|
|
|
|
|
|
|
|
|
+export interface TaskInfo {
|
|
|
|
|
+ id: string;
|
|
|
|
|
+ schedule: string;
|
|
|
|
|
+ scheduleDescription: string;
|
|
|
|
|
+ lastExecutedAt: Date | null;
|
|
|
|
|
+ nextExecutionAt: Date | null;
|
|
|
|
|
+ isRunning: boolean;
|
|
|
|
|
+ lastResult: any;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
@Injectable()
|
|
@Injectable()
|
|
|
export class SchedulerService implements OnApplicationBootstrap {
|
|
export class SchedulerService implements OnApplicationBootstrap {
|
|
|
- private jobs: Cron[] = [];
|
|
|
|
|
|
|
+ private jobs: Map<string, { task: ScheduledTask; job: Cron }> = new Map();
|
|
|
constructor(private configService: ConfigService) {}
|
|
constructor(private configService: ConfigService) {}
|
|
|
|
|
|
|
|
onApplicationBootstrap() {
|
|
onApplicationBootstrap() {
|
|
@@ -26,10 +37,33 @@ export class SchedulerService implements OnApplicationBootstrap {
|
|
|
|
|
|
|
|
for (const task of scheduledTasks) {
|
|
for (const task of scheduledTasks) {
|
|
|
const job = this.createCronJob(task);
|
|
const job = this.createCronJob(task);
|
|
|
- this.jobs.push(job);
|
|
|
|
|
|
|
+ this.jobs.set(task.id, { task, job });
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ getTaskList(): Promise<TaskInfo[]> {
|
|
|
|
|
+ return this.configService.schedulerOptions.schedulerStrategy.getTasks().then(tasks =>
|
|
|
|
|
+ tasks
|
|
|
|
|
+ .map(task => {
|
|
|
|
|
+ const job = this.jobs.get(task.id)?.job;
|
|
|
|
|
+ if (!job) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ const pattern = job.getPattern();
|
|
|
|
|
+ return {
|
|
|
|
|
+ id: task.id,
|
|
|
|
|
+ schedule: pattern ?? 'unknown',
|
|
|
|
|
+ scheduleDescription: pattern ? cronstrue.toString(pattern) : 'unknown',
|
|
|
|
|
+ lastExecutedAt: task.lastExecutedAt,
|
|
|
|
|
+ nextExecutionAt: job.nextRun(),
|
|
|
|
|
+ isRunning: task.isRunning,
|
|
|
|
|
+ lastResult: task.lastResult,
|
|
|
|
|
+ };
|
|
|
|
|
+ })
|
|
|
|
|
+ .filter(x => x !== undefined),
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private createCronJob(task: ScheduledTask) {
|
|
private createCronJob(task: ScheduledTask) {
|
|
|
const schedulerStrategy = this.configService.schedulerOptions.schedulerStrategy;
|
|
const schedulerStrategy = this.configService.schedulerOptions.schedulerStrategy;
|
|
|
const protectCallback = (_job: Cron) => {
|
|
const protectCallback = (_job: Cron) => {
|