mirror of
https://github.com/cool-team-official/cool-admin-midway.git
synced 2024-11-01 14:10:29 +08:00
完善分布式任务调度
This commit is contained in:
parent
8941a2953d
commit
14e1b49f28
31
src/modules/task/middleware/task.ts
Normal file
31
src/modules/task/middleware/task.ts
Normal file
@ -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<Context, NextFunction> {
|
||||
@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();
|
||||
};
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user