From 14e1b49f2875c36fbbc15878554b7aeb0fadde31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E9=B9=8F?= Date: Tue, 31 May 2022 14:37:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=88=86=E5=B8=83=E5=BC=8F?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E8=B0=83=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/task/middleware/task.ts | 31 +++++++++++++++++++++++++++++ src/modules/task/service/info.ts | 14 ++++++------- 2 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 src/modules/task/middleware/task.ts diff --git a/src/modules/task/middleware/task.ts b/src/modules/task/middleware/task.ts new file mode 100644 index 0000000..8926852 --- /dev/null +++ b/src/modules/task/middleware/task.ts @@ -0,0 +1,31 @@ +import { CoolCommException } from '@cool-midway/core'; +import { Inject, Middleware } from '@midwayjs/decorator'; +import { NextFunction, Context } from '@midwayjs/koa'; +import { IMiddleware } from '@midwayjs/core'; +import { TaskInfoQueue } from '../queue/task'; + +/** + * 任务中间件 + */ +@Middleware() +export class TaskMiddleware implements IMiddleware { + @Inject() + taskInfoQueue: TaskInfoQueue; + resolve() { + return async (ctx: Context, next: NextFunction) => { + const urls = ctx.url.split('/'); + if ( + ['add', 'update', 'once', 'stop', 'start'].includes( + urls[urls.length - 1] + ) + ) { + if (!this.taskInfoQueue.metaQueue) { + throw new CoolCommException( + 'task插件未启用或redis配置错误或redis版本过低(>=6.x)' + ); + } + } + await next(); + }; + } +} diff --git a/src/modules/task/service/info.ts b/src/modules/task/service/info.ts index 6c5b08b..91e6833 100644 --- a/src/modules/task/service/info.ts +++ b/src/modules/task/service/info.ts @@ -294,13 +294,13 @@ export class TaskInfoService extends BaseService { const task = await this.taskInfoEntity.findOne({ id: job.id }); const nextTime = await this.getNextRunTime(task.id); if (task) { - if (task.nextRunTime.getTime() == nextTime.getTime()) { - task.status = 0; - task.nextRunTime = nextTime; - this.taskInfoQueue.removeRepeatableByKey(job.key); - } else { - task.nextRunTime = nextTime; - } + // if (task.nextRunTime.getTime() == nextTime.getTime()) { + // task.status = 0; + // task.nextRunTime = nextTime; + // this.taskInfoQueue.removeRepeatableByKey(job.key); + // } else { + task.nextRunTime = nextTime; + // } await this.taskInfoEntity.update(task.id, task); } }