任务调用支持多参数

This commit is contained in:
COOL 2024-06-24 14:09:22 +08:00
parent 73fb586797
commit 4ce452cb76
3 changed files with 24 additions and 11 deletions

View File

@ -4,16 +4,16 @@
"id": 1,
"jobId": null,
"repeatConf": "{\"count\":1,\"type\":1,\"limit\":5,\"name\":\"每秒执行,总共5次\",\"taskType\":1,\"every\":1000,\"service\":\"taskDemoService.test()\",\"status\":1,\"id\":1,\"createTime\":\"2021-03-10 14:25:13\",\"updateTime\":\"2021-03-10 14:25:13\",\"jobId\":1}",
"name": "每秒执行,总共5次",
"name": "每秒执行次",
"cron": null,
"limit": 5,
"limit": null,
"every": 1000,
"remark": null,
"status": 0,
"startDate": null,
"endDate": null,
"data": null,
"service": "taskDemoService.test()",
"service": "taskDemoService.test(1,2)",
"type": 1,
"nextRunTime": "2021-3-10 14:25:18",
"taskType": 1

View File

@ -12,8 +12,8 @@ export class TaskDemoService extends BaseService {
/**
*
*/
async test() {
this.logger.info('我被调用了');
async test(a, b) {
this.logger.info('我被调用了', a, b);
return '任务执行成功';
}
}

View File

@ -332,14 +332,27 @@ export class TaskInfoService extends BaseService {
const service = await this.app
.getApplicationContext()
.getAsync(_.lowerFirst(arr[0]));
for (const child of arr) {
for (let i = 1; i < arr.length; i++) {
const child = arr[i];
if (child.includes('(')) {
const lastArr = child.split('(');
const param = lastArr[1].replace(')', '');
if (!param) {
return service[lastArr[0]]();
const [methodName, paramsStr] = child.split('(');
const params = paramsStr
.replace(')', '')
.split(',')
.map(param => param.trim());
if (params.length === 1 && params[0] === '') {
return service[methodName]();
} else {
return service[lastArr[0]](JSON.parse(param));
const parsedParams = params.map(param => {
try {
return JSON.parse(param);
} catch (e) {
return param; // 如果不是有效的JSON,则返回原始字符串
}
});
return service[methodName](...parsedParams);
}
}
}