优化本地任务

This commit is contained in:
COOL 2025-02-08 16:20:40 +08:00
parent cbb0639f74
commit bdb221ea15
5 changed files with 37 additions and 73 deletions

View File

@ -54,7 +54,7 @@
},
"scripts": {
"start": "NODE_ENV=production node ./bootstrap.js",
"dev": "rimraf src/index.ts && cool check entity && cross-env NODE_ENV=local mwtsc --cleanOutDir --watch --run @midwayjs/mock/app.js",
"dev": "rimraf src/index.ts && cross-env NODE_ENV=local mwtsc --cleanOutDir --watch --run @midwayjs/mock/app.js",
"test": "cross-env NODE_ENV=unittest jest",
"cov": "jest --coverage",
"lint": "mwts check",

22
pnpm-lock.yaml generated
View File

@ -9,8 +9,8 @@ importers:
.:
dependencies:
'@cool-midway/core':
specifier: file:///Users/ap/Documents/src/admin/midway-packages/core
version: file:../midway-packages/core(debug@4.4.0)
specifier: 8.0.0-beta.2
version: 8.0.0-beta.2(debug@4.4.0)
'@cool-midway/rpc':
specifier: 8.0.0-beta.2
version: 8.0.0-beta.2(debug@4.4.0)(encoding@0.1.13)
@ -318,8 +318,8 @@ packages:
resolution: {integrity: sha512-Bl43bVCUm0V+w1wkfOicRfiHHZtaH1mQCp06baNapG86uC7OiXwMrml5+cvZD+mYSHdP07qUxtqkL8oUhrgAfg==}
engines: {node: '>=8.0.0'}
'@cool-midway/core@file:../midway-packages/core':
resolution: {directory: ../midway-packages/core, type: directory}
'@cool-midway/core@8.0.0-beta.2':
resolution: {integrity: sha512-knddLQ6HWSC3do3f7mjnUjKLekS8cdk9DXVxXJtx0e17tlQjIpOtz0YiPz9HAVi/F9ThShFB7R388DNe6GP/QA==}
hasBin: true
'@cool-midway/rpc@8.0.0-beta.2':
@ -450,79 +450,67 @@ packages:
resolution: {integrity: sha512-9B+taZ8DlyyqzZQnoeIvDVR/2F4EbMepXMc/NdVbkzsJbzkUjhXv/70GQJ7tdLA4YJgNP25zukcxpX2/SueNrA==}
cpu: [arm64]
os: [linux]
libc: [glibc]
'@img/sharp-libvips-linux-arm@1.0.5':
resolution: {integrity: sha512-gvcC4ACAOPRNATg/ov8/MnbxFDJqf/pDePbBnuBDcjsI8PssmjoKMAz4LtLaVi+OnSb5FK/yIOamqDwGmXW32g==}
cpu: [arm]
os: [linux]
libc: [glibc]
'@img/sharp-libvips-linux-s390x@1.0.4':
resolution: {integrity: sha512-u7Wz6ntiSSgGSGcjZ55im6uvTrOxSIS8/dgoVMoiGE9I6JAfU50yH5BoDlYA1tcuGS7g/QNtetJnxA6QEsCVTA==}
cpu: [s390x]
os: [linux]
libc: [glibc]
'@img/sharp-libvips-linux-x64@1.0.4':
resolution: {integrity: sha512-MmWmQ3iPFZr0Iev+BAgVMb3ZyC4KeFc3jFxnNbEPas60e1cIfevbtuyf9nDGIzOaW9PdnDciJm+wFFaTlj5xYw==}
cpu: [x64]
os: [linux]
libc: [glibc]
'@img/sharp-libvips-linuxmusl-arm64@1.0.4':
resolution: {integrity: sha512-9Ti+BbTYDcsbp4wfYib8Ctm1ilkugkA/uscUn6UXK1ldpC1JjiXbLfFZtRlBhjPZ5o1NCLiDbg8fhUPKStHoTA==}
cpu: [arm64]
os: [linux]
libc: [musl]
'@img/sharp-libvips-linuxmusl-x64@1.0.4':
resolution: {integrity: sha512-viYN1KX9m+/hGkJtvYYp+CCLgnJXwiQB39damAO7WMdKWlIhmYTfHjwSbQeUK/20vY154mwezd9HflVFM1wVSw==}
cpu: [x64]
os: [linux]
libc: [musl]
'@img/sharp-linux-arm64@0.33.5':
resolution: {integrity: sha512-JMVv+AMRyGOHtO1RFBiJy/MBsgz0x4AWrT6QoEVVTyh1E39TrCUpTRI7mx9VksGX4awWASxqCYLCV4wBZHAYxA==}
engines: {node: ^18.17.0 || ^20.3.0 || >=21.0.0}
cpu: [arm64]
os: [linux]
libc: [glibc]
'@img/sharp-linux-arm@0.33.5':
resolution: {integrity: sha512-JTS1eldqZbJxjvKaAkxhZmBqPRGmxgu+qFKSInv8moZ2AmT5Yib3EQ1c6gp493HvrvV8QgdOXdyaIBrhvFhBMQ==}
engines: {node: ^18.17.0 || ^20.3.0 || >=21.0.0}
cpu: [arm]
os: [linux]
libc: [glibc]
'@img/sharp-linux-s390x@0.33.5':
resolution: {integrity: sha512-y/5PCd+mP4CA/sPDKl2961b+C9d+vPAveS33s6Z3zfASk2j5upL6fXVPZi7ztePZ5CuH+1kW8JtvxgbuXHRa4Q==}
engines: {node: ^18.17.0 || ^20.3.0 || >=21.0.0}
cpu: [s390x]
os: [linux]
libc: [glibc]
'@img/sharp-linux-x64@0.33.5':
resolution: {integrity: sha512-opC+Ok5pRNAzuvq1AG0ar+1owsu842/Ab+4qvU879ippJBHvyY5n2mxF1izXqkPYlGuP/M556uh53jRLJmzTWA==}
engines: {node: ^18.17.0 || ^20.3.0 || >=21.0.0}
cpu: [x64]
os: [linux]
libc: [glibc]
'@img/sharp-linuxmusl-arm64@0.33.5':
resolution: {integrity: sha512-XrHMZwGQGvJg2V/oRSUfSAfjfPxO+4DkiRh6p2AFjLQztWUuY/o8Mq0eMQVIY7HJ1CDQUJlxGGZRw1a5bqmd1g==}
engines: {node: ^18.17.0 || ^20.3.0 || >=21.0.0}
cpu: [arm64]
os: [linux]
libc: [musl]
'@img/sharp-linuxmusl-x64@0.33.5':
resolution: {integrity: sha512-WT+d/cgqKkkKySYmqoZ8y3pxx7lx9vVejxW/W4DOFMYVSkErR+w7mf2u8m/y4+xHe7yY9DAXQMWQhpnMuFfScw==}
engines: {node: ^18.17.0 || ^20.3.0 || >=21.0.0}
cpu: [x64]
os: [linux]
libc: [musl]
'@img/sharp-wasm32@0.33.5':
resolution: {integrity: sha512-ykUW4LVGaMcU9lu9thv85CbRMAwfeadCJHRsg2GmeRa/cJxsVY9Rbd57JcMxBkKHag5U/x7TSBpScF4U8ElVzg==}
@ -4890,7 +4878,7 @@ snapshots:
dependencies:
lockfile: 1.0.4
'@cool-midway/core@file:../midway-packages/core(debug@4.4.0)':
'@cool-midway/core@8.0.0-beta.2(debug@4.4.0)':
dependencies:
'@cool-midway/cache-manager-fs-hash': 7.0.0
'@midwayjs/cache-manager': 3.20.0

View File

@ -1,49 +1,2 @@
// 自动生成的文件,请勿手动修改
import * as entity0 from './modules/user/entity/wx';
import * as entity1 from './modules/user/entity/info';
import * as entity2 from './modules/user/entity/address';
import * as entity3 from './modules/task/entity/log';
import * as entity4 from './modules/task/entity/info';
import * as entity5 from './modules/space/entity/type';
import * as entity6 from './modules/space/entity/info';
import * as entity7 from './modules/recycle/entity/data';
import * as entity8 from './modules/plugin/entity/info';
import * as entity9 from './modules/dict/entity/type';
import * as entity10 from './modules/dict/entity/info';
import * as entity11 from './modules/demo/entity/goods';
import * as entity12 from './modules/base/entity/base';
import * as entity13 from './modules/base/entity/sys/user_role';
import * as entity14 from './modules/base/entity/sys/user';
import * as entity15 from './modules/base/entity/sys/role_menu';
import * as entity16 from './modules/base/entity/sys/role_department';
import * as entity17 from './modules/base/entity/sys/role';
import * as entity18 from './modules/base/entity/sys/param';
import * as entity19 from './modules/base/entity/sys/menu';
import * as entity20 from './modules/base/entity/sys/log';
import * as entity21 from './modules/base/entity/sys/department';
import * as entity22 from './modules/base/entity/sys/conf';
export const entities = [
...Object.values(entity0),
...Object.values(entity1),
...Object.values(entity2),
...Object.values(entity3),
...Object.values(entity4),
...Object.values(entity5),
...Object.values(entity6),
...Object.values(entity7),
...Object.values(entity8),
...Object.values(entity9),
...Object.values(entity10),
...Object.values(entity11),
...Object.values(entity12),
...Object.values(entity13),
...Object.values(entity14),
...Object.values(entity15),
...Object.values(entity16),
...Object.values(entity17),
...Object.values(entity18),
...Object.values(entity19),
...Object.values(entity20),
...Object.values(entity21),
...Object.values(entity22),
];
export const entities = [];

View File

@ -1,17 +1,26 @@
import { Inject } from '@midwayjs/core';
import { CoolEvent, Event } from '@cool-midway/core';
import { TaskInfoService } from '../service/info';
import { TaskLocalService } from '../service/local';
/**
*
*/
@CoolEvent()
export class TaskAppEvent {
export class TaskCommEvent {
@Inject()
taskInfoService: TaskInfoService;
@Inject()
taskLocalService: TaskLocalService;
@Event('onServerReady')
async onServerReady() {
this.taskInfoService.initTask();
}
@Event()
async onLocalTaskStop(jobId) {
this.taskLocalService.stopByJobId(jobId);
}
}

View File

@ -7,7 +7,7 @@ import {
Scope,
ScopeEnum,
} from '@midwayjs/core';
import { BaseService } from '@cool-midway/core';
import { BaseService, CoolEventManager } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Equal, LessThan, Repository } from 'typeorm';
import { TaskInfoEntity } from '../entity/info';
@ -44,6 +44,9 @@ export class TaskLocalService extends BaseService {
@Config('task.log.keepDays')
keepDays: number;
@Inject()
coolEventManager: CoolEventManager;
// 存储所有运行的任务
private cronJobs: Map<string, CronJob.CronJob> = new Map();
@ -53,17 +56,26 @@ export class TaskLocalService extends BaseService {
async stop(id) {
const task = await this.taskInfoEntity.findOneBy({ id: Equal(id) });
if (task) {
const job = this.cronJobs.get(task.jobId);
if (job) {
job.stop();
this.cronJobs.delete(task.jobId);
}
this.stopByJobId(task.jobId);
this.coolEventManager.emit('onLocalTaskStop', task.jobId);
task.status = 0;
await this.taskInfoEntity.update(task.id, task);
await this.updateNextRunTime(task.jobId);
}
}
/**
*
* @param jobId
*/
async stopByJobId(jobId) {
const job = this.cronJobs.get(jobId);
if (job) {
job.stop();
this.cronJobs.delete(jobId);
}
}
/**
*
*/
@ -151,7 +163,9 @@ export class TaskLocalService extends BaseService {
const job = this.cronJobs.get(params.jobId);
job.stop();
this.cronJobs.delete(params.jobId);
this.coolEventManager.emit('onLocalTaskStop', params.jobId);
}
await this.utils.sleep(1000);
this.createCronJob(params);
}
});