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); } }