@pisell/core 1.0.30 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/app/app.d.ts +3 -7
- package/es/app/app.js +0 -3
- package/es/indexDB/index.d.ts +6 -3
- package/es/indexDB/index.js +265 -41
- package/es/request/cache.d.ts +1 -1
- package/es/request/cache.js +29 -81
- package/es/request/index.d.ts +3 -3
- package/es/request/index.js +1 -14
- package/es/request/type.d.ts +0 -11
- package/es/request/type.js +1 -10
- package/es/request/utils.js +1 -1
- package/es/tasks/index.d.ts +0 -45
- package/es/tasks/index.js +101 -444
- package/es/tasks/type.d.ts +0 -38
- package/lib/app/app.d.ts +3 -7
- package/lib/app/app.js +0 -3
- package/lib/indexDB/index.d.ts +6 -3
- package/lib/indexDB/index.js +218 -21
- package/lib/request/cache.d.ts +1 -1
- package/lib/request/cache.js +5 -42
- package/lib/request/index.d.ts +3 -3
- package/lib/request/index.js +1 -10
- package/lib/request/type.d.ts +0 -11
- package/lib/request/type.js +0 -19
- package/lib/request/utils.js +1 -1
- package/lib/tasks/index.d.ts +0 -45
- package/lib/tasks/index.js +77 -326
- package/lib/tasks/type.d.ts +0 -38
- package/package.json +1 -1
- package/es/tasks/scheduledTasksExample.d.ts +0 -61
- package/es/tasks/scheduledTasksExample.js +0 -351
- package/lib/tasks/scheduledTasksExample.d.ts +0 -61
- package/lib/tasks/scheduledTasksExample.js +0 -267
package/lib/request/type.js
CHANGED
|
@@ -2,10 +2,6 @@ var __defProp = Object.defineProperty;
|
|
|
2
2
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
3
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
4
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
5
|
var __copyProps = (to, from, except, desc) => {
|
|
10
6
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
7
|
for (let key of __getOwnPropNames(from))
|
|
@@ -18,19 +14,4 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
18
14
|
|
|
19
15
|
// src/request/type.ts
|
|
20
16
|
var type_exports = {};
|
|
21
|
-
__export(type_exports, {
|
|
22
|
-
RequestModeENUM: () => RequestModeENUM
|
|
23
|
-
});
|
|
24
17
|
module.exports = __toCommonJS(type_exports);
|
|
25
|
-
var RequestModeENUM = /* @__PURE__ */ ((RequestModeENUM2) => {
|
|
26
|
-
RequestModeENUM2["LOCAL"] = "local";
|
|
27
|
-
RequestModeENUM2["REMOTE"] = "remote";
|
|
28
|
-
RequestModeENUM2["LOCAL_REMOTE"] = "local_remote";
|
|
29
|
-
RequestModeENUM2["REMOTE_LOCAL"] = "remote_local";
|
|
30
|
-
RequestModeENUM2["OS_SERVER"] = "os_server";
|
|
31
|
-
return RequestModeENUM2;
|
|
32
|
-
})(RequestModeENUM || {});
|
|
33
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
34
|
-
0 && (module.exports = {
|
|
35
|
-
RequestModeENUM
|
|
36
|
-
});
|
package/lib/request/utils.js
CHANGED
|
@@ -86,7 +86,7 @@ var requestCallback = (resData) => {
|
|
|
86
86
|
const codeFn = requestCallbacks == null ? void 0 : requestCallbacks[res == null ? void 0 : res.code];
|
|
87
87
|
if (codeFn) {
|
|
88
88
|
if ((res == null ? void 0 : res.code) === 200) {
|
|
89
|
-
if (
|
|
89
|
+
if (config == null ? void 0 : config.cache) {
|
|
90
90
|
(0, import_cache.setCacheData)(props.url, props.data, res, config == null ? void 0 : config.cache);
|
|
91
91
|
(_a = config == null ? void 0 : config.cacheUpdateChange) == null ? void 0 : _a.call(config, res);
|
|
92
92
|
}
|
package/lib/tasks/index.d.ts
CHANGED
|
@@ -37,20 +37,6 @@ export declare class TasksManager {
|
|
|
37
37
|
* @param {NodeJS.Timeout} timerId
|
|
38
38
|
*/
|
|
39
39
|
private clearTaskTimer;
|
|
40
|
-
/**
|
|
41
|
-
* @title: 计算下一次执行时间
|
|
42
|
-
* @description: 根据定时任务配置计算下一次执行时间
|
|
43
|
-
* @param {Task} task
|
|
44
|
-
* @return {string | null} 下一次执行时间
|
|
45
|
-
*/
|
|
46
|
-
private calculateNextExecuteTime;
|
|
47
|
-
/**
|
|
48
|
-
* @title: 启动定时任务
|
|
49
|
-
* @description: 在特定时间点执行任务(仅在 scheduledTasks 模块中生效)
|
|
50
|
-
* @param {Task} task
|
|
51
|
-
* @return {*}
|
|
52
|
-
*/
|
|
53
|
-
private startScheduledTask;
|
|
54
40
|
/**
|
|
55
41
|
* @title: 启动轮询
|
|
56
42
|
* @description: 根据轮询间隔定期执行任务
|
|
@@ -89,40 +75,9 @@ export declare class TasksManager {
|
|
|
89
75
|
addTask(payload: AddTaskParams): void;
|
|
90
76
|
private updateTask;
|
|
91
77
|
private updateQueueStatus;
|
|
92
|
-
/**
|
|
93
|
-
* @title: 更新队列运行状态
|
|
94
|
-
* @description: 标记队列是否正在执行
|
|
95
|
-
*/
|
|
96
|
-
private updateQueueRunningState;
|
|
97
|
-
private setTasksData;
|
|
98
78
|
private setTasks;
|
|
99
79
|
clearAllTaskTimer(tasks: Task[]): void;
|
|
100
80
|
clearTasks(payload: RunTaskParams): void;
|
|
101
81
|
clearAllTasks(): void;
|
|
102
82
|
watchTask(callback: (taskModule: TasksModule) => void): void;
|
|
103
|
-
/**
|
|
104
|
-
* @title: 获取队列执行状态
|
|
105
|
-
* @description: 获取指定队列的执行状态和进度信息
|
|
106
|
-
* @param {string} module - 模块名
|
|
107
|
-
* @param {string} queueId - 队列ID
|
|
108
|
-
* @return {object} 队列状态信息
|
|
109
|
-
*/
|
|
110
|
-
getQueueStatus(module: string, queueId: string): {
|
|
111
|
-
isRunning: boolean;
|
|
112
|
-
status: "uncompleted" | "completed";
|
|
113
|
-
progress: {
|
|
114
|
-
total: number;
|
|
115
|
-
completed: number;
|
|
116
|
-
failed: number;
|
|
117
|
-
inProgress: number;
|
|
118
|
-
};
|
|
119
|
-
lastRunAt: string | null;
|
|
120
|
-
tasksCount: number;
|
|
121
|
-
} | null;
|
|
122
|
-
/**
|
|
123
|
-
* @title: 获取所有队列状态
|
|
124
|
-
* @description: 获取所有任务队列的执行状态概览
|
|
125
|
-
* @return {object} 所有队列的状态信息
|
|
126
|
-
*/
|
|
127
|
-
getAllQueuesStatus(): any;
|
|
128
83
|
}
|
package/lib/tasks/index.js
CHANGED
|
@@ -38,10 +38,7 @@ var import_useTasks = __toESM(require("./useTasks"));
|
|
|
38
38
|
var TasksManager = class _TasksManager {
|
|
39
39
|
static instance;
|
|
40
40
|
taskFunctions;
|
|
41
|
-
tasks
|
|
42
|
-
// 内置模块, 定时任务专用
|
|
43
|
-
scheduledTasks: {}
|
|
44
|
-
};
|
|
41
|
+
tasks;
|
|
45
42
|
app;
|
|
46
43
|
db;
|
|
47
44
|
useTasks = import_useTasks.default;
|
|
@@ -54,9 +51,7 @@ var TasksManager = class _TasksManager {
|
|
|
54
51
|
}
|
|
55
52
|
this.app = app;
|
|
56
53
|
this.taskFunctions = /* @__PURE__ */ new Map();
|
|
57
|
-
this.tasks = {
|
|
58
|
-
scheduledTasks: {}
|
|
59
|
-
};
|
|
54
|
+
this.tasks = {};
|
|
60
55
|
this.db = app.dbManager;
|
|
61
56
|
this.timerIds = [];
|
|
62
57
|
}
|
|
@@ -188,149 +183,6 @@ var TasksManager = class _TasksManager {
|
|
|
188
183
|
}
|
|
189
184
|
this.timerIds = [..._timerIds || []];
|
|
190
185
|
};
|
|
191
|
-
/**
|
|
192
|
-
* @title: 计算下一次执行时间
|
|
193
|
-
* @description: 根据定时任务配置计算下一次执行时间
|
|
194
|
-
* @param {Task} task
|
|
195
|
-
* @return {string | null} 下一次执行时间
|
|
196
|
-
*/
|
|
197
|
-
calculateNextExecuteTime = (task) => {
|
|
198
|
-
if (!task.scheduled) {
|
|
199
|
-
return null;
|
|
200
|
-
}
|
|
201
|
-
const { scheduled, scheduledResult } = task;
|
|
202
|
-
const now = (0, import_dayjs.default)();
|
|
203
|
-
if (scheduled.endAt && now.isAfter((0, import_dayjs.default)(scheduled.endAt))) {
|
|
204
|
-
return null;
|
|
205
|
-
}
|
|
206
|
-
if (Array.isArray(scheduled.executeAt)) {
|
|
207
|
-
const futureTime = scheduled.executeAt.map((time) => (0, import_dayjs.default)(time)).filter((time) => time.isAfter(now)).sort((a, b) => a.valueOf() - b.valueOf())[0];
|
|
208
|
-
if (futureTime) {
|
|
209
|
-
return futureTime.format("YYYY-MM-DD HH:mm:ss");
|
|
210
|
-
}
|
|
211
|
-
if (!scheduled.repeat) {
|
|
212
|
-
return null;
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
if (scheduled.repeat && (scheduledResult == null ? void 0 : scheduledResult.nextExecuteTime)) {
|
|
216
|
-
const lastExecuteTime = (0, import_dayjs.default)(scheduledResult.nextExecuteTime);
|
|
217
|
-
const interval = scheduled.repeatInterval || 1;
|
|
218
|
-
let nextTime = lastExecuteTime;
|
|
219
|
-
switch (scheduled.repeatType) {
|
|
220
|
-
case "daily":
|
|
221
|
-
nextTime = lastExecuteTime.add(interval, "day");
|
|
222
|
-
break;
|
|
223
|
-
case "weekly":
|
|
224
|
-
nextTime = lastExecuteTime.add(interval, "week");
|
|
225
|
-
break;
|
|
226
|
-
case "monthly":
|
|
227
|
-
nextTime = lastExecuteTime.add(interval, "month");
|
|
228
|
-
break;
|
|
229
|
-
case "yearly":
|
|
230
|
-
nextTime = lastExecuteTime.add(interval, "year");
|
|
231
|
-
break;
|
|
232
|
-
default:
|
|
233
|
-
nextTime = lastExecuteTime.add(interval, "day");
|
|
234
|
-
}
|
|
235
|
-
if (scheduled.endAt && nextTime.isAfter((0, import_dayjs.default)(scheduled.endAt))) {
|
|
236
|
-
return null;
|
|
237
|
-
}
|
|
238
|
-
return nextTime.format("YYYY-MM-DD HH:mm:ss");
|
|
239
|
-
}
|
|
240
|
-
const executeTime = Array.isArray(scheduled.executeAt) ? scheduled.executeAt[0] : scheduled.executeAt;
|
|
241
|
-
return (0, import_dayjs.default)(executeTime).format("YYYY-MM-DD HH:mm:ss");
|
|
242
|
-
};
|
|
243
|
-
/**
|
|
244
|
-
* @title: 启动定时任务
|
|
245
|
-
* @description: 在特定时间点执行任务(仅在 scheduledTasks 模块中生效)
|
|
246
|
-
* @param {Task} task
|
|
247
|
-
* @return {*}
|
|
248
|
-
*/
|
|
249
|
-
startScheduledTask = (task) => {
|
|
250
|
-
var _a;
|
|
251
|
-
if (!task.scheduled) {
|
|
252
|
-
console.log("Tasks--->", "不是定时任务");
|
|
253
|
-
return;
|
|
254
|
-
}
|
|
255
|
-
if (task.module !== "scheduledTasks") {
|
|
256
|
-
console.warn("Tasks--->", `定时任务只在 scheduledTasks 模块中生效,任务 ${task.id} 将作为普通任务执行`);
|
|
257
|
-
return;
|
|
258
|
-
}
|
|
259
|
-
const nextExecuteTime = this.calculateNextExecuteTime(task);
|
|
260
|
-
if (!nextExecuteTime) {
|
|
261
|
-
console.log("Tasks--->", "定时任务已完成或无下次执行时间", task);
|
|
262
|
-
this.deleteTask({
|
|
263
|
-
module: task.module,
|
|
264
|
-
queueId: task.queueId,
|
|
265
|
-
taskId: task.id
|
|
266
|
-
});
|
|
267
|
-
return;
|
|
268
|
-
}
|
|
269
|
-
const now = (0, import_dayjs.default)();
|
|
270
|
-
const executeTime = (0, import_dayjs.default)(nextExecuteTime);
|
|
271
|
-
const delay = executeTime.diff(now);
|
|
272
|
-
if (delay < 0) {
|
|
273
|
-
console.log("Tasks--->", "执行时间已过,跳过此次执行");
|
|
274
|
-
if (task.scheduled.repeat) {
|
|
275
|
-
const _task = { ...task };
|
|
276
|
-
_task.scheduledResult = {
|
|
277
|
-
count: ((_a = _task.scheduledResult) == null ? void 0 : _a.count) || 0,
|
|
278
|
-
nextExecuteTime
|
|
279
|
-
};
|
|
280
|
-
const newTask = this.updateTask({
|
|
281
|
-
module: task.module,
|
|
282
|
-
queueId: task.queueId,
|
|
283
|
-
taskId: task.id,
|
|
284
|
-
other: _task
|
|
285
|
-
});
|
|
286
|
-
if (newTask) {
|
|
287
|
-
this.startScheduledTask(newTask);
|
|
288
|
-
}
|
|
289
|
-
} else {
|
|
290
|
-
this.deleteTask({
|
|
291
|
-
module: task.module,
|
|
292
|
-
queueId: task.queueId,
|
|
293
|
-
taskId: task.id
|
|
294
|
-
});
|
|
295
|
-
}
|
|
296
|
-
return;
|
|
297
|
-
}
|
|
298
|
-
console.log("Tasks--->", `定时任务将在 ${nextExecuteTime} 执行 (${delay}ms 后)`, task);
|
|
299
|
-
const timerId = setTimeout(async () => {
|
|
300
|
-
var _a2, _b;
|
|
301
|
-
try {
|
|
302
|
-
await this.runTask(task);
|
|
303
|
-
console.log("Tasks--->", "定时任务执行完成", task);
|
|
304
|
-
let _task = { ...task };
|
|
305
|
-
_task.scheduledResult = {
|
|
306
|
-
count: (((_a2 = _task.scheduledResult) == null ? void 0 : _a2.count) || 0) + 1,
|
|
307
|
-
timerId,
|
|
308
|
-
nextExecuteTime
|
|
309
|
-
};
|
|
310
|
-
if ((_b = task.scheduled) == null ? void 0 : _b.repeat) {
|
|
311
|
-
const newTask = this.updateTask({
|
|
312
|
-
module: task.module,
|
|
313
|
-
queueId: task.queueId,
|
|
314
|
-
taskId: task.id,
|
|
315
|
-
other: _task
|
|
316
|
-
});
|
|
317
|
-
if (newTask) {
|
|
318
|
-
this.startScheduledTask(newTask);
|
|
319
|
-
}
|
|
320
|
-
} else {
|
|
321
|
-
this.deleteTask({
|
|
322
|
-
module: task.module,
|
|
323
|
-
queueId: task.queueId,
|
|
324
|
-
taskId: task.id
|
|
325
|
-
});
|
|
326
|
-
}
|
|
327
|
-
} catch (error) {
|
|
328
|
-
this.clearTaskTimer({ timerId });
|
|
329
|
-
console.error("定时任务执行异常", error);
|
|
330
|
-
}
|
|
331
|
-
}, delay);
|
|
332
|
-
this.timerIds.push({ taskId: task.id, timerId });
|
|
333
|
-
};
|
|
334
186
|
/**
|
|
335
187
|
* @title: 启动轮询
|
|
336
188
|
* @description: 根据轮询间隔定期执行任务
|
|
@@ -421,116 +273,82 @@ var TasksManager = class _TasksManager {
|
|
|
421
273
|
* @Date: 2024-09-26 13:52
|
|
422
274
|
*/
|
|
423
275
|
async run(payload) {
|
|
424
|
-
var _a, _b
|
|
276
|
+
var _a, _b;
|
|
425
277
|
const { queueId, module: module2, callback } = payload;
|
|
426
|
-
const currentQueue = (_a = this.tasks[module2]) == null ? void 0 : _a[queueId];
|
|
427
|
-
if (currentQueue == null ? void 0 : currentQueue.isRunning) {
|
|
428
|
-
console.warn(
|
|
429
|
-
"Tasks--->",
|
|
430
|
-
`任务队列 [${module2}/${queueId}] 正在执行中,已拦截重复调用。`,
|
|
431
|
-
`当前进度: ${((_b = currentQueue.progress) == null ? void 0 : _b.completed) || 0}/${((_c = currentQueue.progress) == null ? void 0 : _c.total) || 0}`,
|
|
432
|
-
`如需重新执行,请等待当前任务队列执行完成。`
|
|
433
|
-
);
|
|
434
|
-
return;
|
|
435
|
-
}
|
|
436
|
-
this.updateQueueRunningState({
|
|
437
|
-
module: module2,
|
|
438
|
-
queueId,
|
|
439
|
-
isRunning: true,
|
|
440
|
-
lastRunAt: (0, import_dayjs.default)().format("YYYY-MM-DD HH:mm:ss")
|
|
441
|
-
});
|
|
442
|
-
console.log("Tasks--->", `任务队列 [${module2}/${queueId}] 开始执行`);
|
|
443
278
|
let taskQueueStatus = "";
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
this.startScheduledTask(task);
|
|
460
|
-
} else if (task.polling) {
|
|
461
|
-
this.startPolling(task);
|
|
462
|
-
} else {
|
|
463
|
-
const { status } = await this.runTask(task);
|
|
464
|
-
task.status = status;
|
|
465
|
-
}
|
|
466
|
-
if (task.status === "success") {
|
|
467
|
-
if ((_d = task.pollingResult) == null ? void 0 : _d.timerId) {
|
|
468
|
-
}
|
|
469
|
-
this.deleteTask({ module: module2, queueId, taskId: task.id });
|
|
470
|
-
} else {
|
|
471
|
-
let _other = {
|
|
472
|
-
status: task.status
|
|
473
|
-
};
|
|
474
|
-
if (task.status === "failure") {
|
|
475
|
-
_other.retries = task.retries + 1;
|
|
476
|
-
}
|
|
477
|
-
errorTaskIds.push(task.id);
|
|
478
|
-
this.updateTask({ module: module2, queueId, taskId: task.id, other: _other });
|
|
479
|
-
}
|
|
279
|
+
while (taskQueueStatus !== "completed") {
|
|
280
|
+
const taskQueue = this.getTaskQueue(payload);
|
|
281
|
+
const errorTaskIds = [];
|
|
282
|
+
if (taskQueue && taskQueue.length) {
|
|
283
|
+
const tasksToExecute = taskQueue.filter(
|
|
284
|
+
(task) => (task == null ? void 0 : task.status) === "pending" || (task == null ? void 0 : task.status) === "failure"
|
|
285
|
+
);
|
|
286
|
+
console.log("Tasks--->", "需要执行的任务", tasksToExecute);
|
|
287
|
+
for (let task of tasksToExecute) {
|
|
288
|
+
try {
|
|
289
|
+
if (task.retries !== void 0 && task.maxRetries !== void 0 && task.retries < task.maxRetries) {
|
|
290
|
+
task.status = "in-progress";
|
|
291
|
+
this.updateTask({ module: module2, queueId, taskId: task.id, other: { status: task.status } });
|
|
292
|
+
if (task.polling) {
|
|
293
|
+
this.startPolling(task);
|
|
480
294
|
} else {
|
|
481
|
-
|
|
295
|
+
const { status } = await this.runTask(task);
|
|
296
|
+
task.status = status;
|
|
297
|
+
}
|
|
298
|
+
if (task.status === "success") {
|
|
299
|
+
if ((_a = task.pollingResult) == null ? void 0 : _a.timerId) {
|
|
300
|
+
}
|
|
482
301
|
this.deleteTask({ module: module2, queueId, taskId: task.id });
|
|
302
|
+
} else {
|
|
303
|
+
let _other = {
|
|
304
|
+
status: task.status
|
|
305
|
+
};
|
|
306
|
+
if (task.status === "failure") {
|
|
307
|
+
_other.retries = task.retries + 1;
|
|
308
|
+
}
|
|
309
|
+
errorTaskIds.push(task.id);
|
|
310
|
+
this.updateTask({ module: module2, queueId, taskId: task.id, other: _other });
|
|
483
311
|
}
|
|
484
|
-
}
|
|
485
|
-
console.
|
|
312
|
+
} else {
|
|
313
|
+
console.log("Tasks--->", "任务没有重试次数,需要删除", task);
|
|
314
|
+
this.deleteTask({ module: module2, queueId, taskId: task.id });
|
|
486
315
|
}
|
|
316
|
+
} catch (error) {
|
|
317
|
+
console.error("任务异常", error);
|
|
487
318
|
}
|
|
488
|
-
taskQueueStatus = ((_e = this.getTaskQueue(payload)) == null ? void 0 : _e.some((task) => task.status === "failure")) ? "uncompleted" : "completed";
|
|
489
|
-
this.updateQueueStatus({
|
|
490
|
-
queueId,
|
|
491
|
-
module: module2,
|
|
492
|
-
status: taskQueueStatus
|
|
493
|
-
});
|
|
494
|
-
console.log("Tasks--->", "任务队列执行完成", taskQueue);
|
|
495
|
-
} else {
|
|
496
|
-
taskQueueStatus = "completed";
|
|
497
|
-
}
|
|
498
|
-
if (taskQueueStatus === "uncompleted") {
|
|
499
|
-
this.app.logger.addLog({
|
|
500
|
-
type: "info",
|
|
501
|
-
title: `任务队列执行存在失败-${queueId}`,
|
|
502
|
-
metadata: { taskQueue, errorTaskIds }
|
|
503
|
-
});
|
|
504
|
-
await this.timeout();
|
|
505
|
-
} else if (taskQueueStatus === "completed") {
|
|
506
|
-
console.log("Tasks--->", "任务队列全部执行完成");
|
|
507
|
-
callback == null ? void 0 : callback();
|
|
508
319
|
}
|
|
320
|
+
taskQueueStatus = ((_b = this.getTaskQueue(payload)) == null ? void 0 : _b.some((task) => task.status === "failure")) ? "uncompleted" : "completed";
|
|
321
|
+
this.updateQueueStatus({
|
|
322
|
+
queueId,
|
|
323
|
+
module: module2,
|
|
324
|
+
status: taskQueueStatus
|
|
325
|
+
});
|
|
326
|
+
console.log("Tasks--->", "任务队列执行完成", taskQueue);
|
|
327
|
+
} else {
|
|
328
|
+
taskQueueStatus = "completed";
|
|
329
|
+
}
|
|
330
|
+
if (taskQueueStatus === "uncompleted") {
|
|
331
|
+
this.app.logger.addLog({
|
|
332
|
+
type: "info",
|
|
333
|
+
title: `任务队列执行存在失败-${queueId}`,
|
|
334
|
+
metadata: { taskQueue, errorTaskIds }
|
|
335
|
+
});
|
|
336
|
+
await this.timeout();
|
|
337
|
+
} else if (taskQueueStatus === "completed") {
|
|
338
|
+
console.log("Tasks--->", "任务队列全部执行完成");
|
|
339
|
+
callback == null ? void 0 : callback();
|
|
509
340
|
}
|
|
510
|
-
} finally {
|
|
511
|
-
this.updateQueueRunningState({
|
|
512
|
-
module: module2,
|
|
513
|
-
queueId,
|
|
514
|
-
isRunning: false
|
|
515
|
-
});
|
|
516
|
-
console.log("Tasks--->", `任务队列 [${module2}/${queueId}] 执行结束`);
|
|
517
341
|
}
|
|
518
342
|
}
|
|
519
343
|
deleteTask(payload) {
|
|
520
344
|
var _a, _b, _c, _d;
|
|
521
345
|
const { queueId, module: module2, taskId } = payload;
|
|
522
346
|
this.tasks[module2][queueId].tasks = (_d = (_c = (_b = (_a = this.tasks) == null ? void 0 : _a[module2]) == null ? void 0 : _b[queueId]) == null ? void 0 : _c.tasks) == null ? void 0 : _d.filter((task) => {
|
|
523
|
-
var _a2
|
|
524
|
-
if (task.
|
|
525
|
-
|
|
526
|
-
this.clearTaskTimer({ timerId: task.pollingResult.timerId });
|
|
527
|
-
}
|
|
528
|
-
if ((_b2 = task.scheduledResult) == null ? void 0 : _b2.timerId) {
|
|
529
|
-
this.clearTaskTimer({ timerId: task.scheduledResult.timerId });
|
|
530
|
-
}
|
|
531
|
-
return false;
|
|
347
|
+
var _a2;
|
|
348
|
+
if ((_a2 = task.pollingResult) == null ? void 0 : _a2.timerId) {
|
|
349
|
+
this.clearTaskTimer({ timerId: task.pollingResult.timerId });
|
|
532
350
|
}
|
|
533
|
-
return
|
|
351
|
+
return task.id !== taskId;
|
|
534
352
|
});
|
|
535
353
|
this.saveTaskQueueToLocal(this.tasks);
|
|
536
354
|
console.log("Tasks--->", "删除指定任务", queueId, module2, taskId);
|
|
@@ -553,13 +371,6 @@ var TasksManager = class _TasksManager {
|
|
|
553
371
|
const taskQueue = (_b = (_a = this.tasks[module2]) == null ? void 0 : _a[queueId]) == null ? void 0 : _b.tasks;
|
|
554
372
|
let _tasks = [...taskQueue || []];
|
|
555
373
|
let newTasks = (_c = tasks == null ? void 0 : tasks.map) == null ? void 0 : _c.call(tasks, (d) => {
|
|
556
|
-
if (d.scheduled && module2 !== "scheduledTasks") {
|
|
557
|
-
console.warn(
|
|
558
|
-
"Tasks--->",
|
|
559
|
-
`检测到定时任务配置,但模块为 "${module2}"。定时任务功能仅在 "scheduledTasks" 模块中生效。`,
|
|
560
|
-
`该任务将作为普通任务立即执行。如需使用定时功能,请将任务添加到 "scheduledTasks" 模块。`
|
|
561
|
-
);
|
|
562
|
-
}
|
|
563
374
|
return this.createTaskData({ ...d, queueId, module: module2 });
|
|
564
375
|
});
|
|
565
376
|
_tasks = _tasks.concat(newTasks);
|
|
@@ -596,19 +407,22 @@ var TasksManager = class _TasksManager {
|
|
|
596
407
|
}
|
|
597
408
|
updateQueueStatus(payload) {
|
|
598
409
|
const { queueId, status, module: module2 } = payload;
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
410
|
+
const newState = {
|
|
411
|
+
...this.tasks,
|
|
412
|
+
[module2]: {
|
|
413
|
+
...this.tasks[module2],
|
|
414
|
+
[queueId]: {
|
|
415
|
+
...this.tasks[module2][queueId],
|
|
416
|
+
status
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
};
|
|
420
|
+
this.saveTaskQueueToLocal(newState);
|
|
421
|
+
this.tasks = newState;
|
|
608
422
|
}
|
|
609
423
|
// 设置任务
|
|
610
|
-
|
|
611
|
-
const { queueId, module: module2,
|
|
424
|
+
setTasks(payload) {
|
|
425
|
+
const { queueId, module: module2, tasks } = payload;
|
|
612
426
|
const moduleData = this.tasks[module2] || {};
|
|
613
427
|
const queueIdData = moduleData[queueId] || {};
|
|
614
428
|
const newState = {
|
|
@@ -617,18 +431,13 @@ var TasksManager = class _TasksManager {
|
|
|
617
431
|
...moduleData,
|
|
618
432
|
[queueId]: {
|
|
619
433
|
...queueIdData,
|
|
620
|
-
|
|
434
|
+
tasks
|
|
621
435
|
}
|
|
622
436
|
}
|
|
623
437
|
};
|
|
624
438
|
this.saveTaskQueueToLocal(newState);
|
|
625
439
|
this.tasks = newState;
|
|
626
440
|
}
|
|
627
|
-
// 设置任务
|
|
628
|
-
setTasks(payload) {
|
|
629
|
-
const { queueId, module: module2, tasks } = payload;
|
|
630
|
-
this.setTasksData({ queueId, module: module2, tasks });
|
|
631
|
-
}
|
|
632
441
|
clearAllTaskTimer(tasks) {
|
|
633
442
|
for (let task of tasks || []) {
|
|
634
443
|
this.clearTaskTimer({ taskId: task.id });
|
|
@@ -636,11 +445,7 @@ var TasksManager = class _TasksManager {
|
|
|
636
445
|
}
|
|
637
446
|
// 清空任务
|
|
638
447
|
clearTasks(payload) {
|
|
639
|
-
var _a, _b;
|
|
640
448
|
const { queueId, module: module2 } = payload;
|
|
641
|
-
if (!((_b = (_a = this.tasks) == null ? void 0 : _a[module2]) == null ? void 0 : _b[queueId])) {
|
|
642
|
-
return;
|
|
643
|
-
}
|
|
644
449
|
this.clearAllTaskTimer(this.tasks[module2][queueId].tasks);
|
|
645
450
|
this.tasks[module2][queueId].tasks = [];
|
|
646
451
|
this.saveTaskQueueToLocal(this.tasks);
|
|
@@ -661,60 +466,6 @@ var TasksManager = class _TasksManager {
|
|
|
661
466
|
watchTask(callback) {
|
|
662
467
|
this.watchTaskCallback = callback;
|
|
663
468
|
}
|
|
664
|
-
/**
|
|
665
|
-
* @title: 获取队列执行状态
|
|
666
|
-
* @description: 获取指定队列的执行状态和进度信息
|
|
667
|
-
* @param {string} module - 模块名
|
|
668
|
-
* @param {string} queueId - 队列ID
|
|
669
|
-
* @return {object} 队列状态信息
|
|
670
|
-
*/
|
|
671
|
-
getQueueStatus(module2, queueId) {
|
|
672
|
-
var _a, _b;
|
|
673
|
-
const queue = (_a = this.tasks[module2]) == null ? void 0 : _a[queueId];
|
|
674
|
-
if (!queue) {
|
|
675
|
-
return null;
|
|
676
|
-
}
|
|
677
|
-
return {
|
|
678
|
-
isRunning: queue.isRunning || false,
|
|
679
|
-
status: queue.status,
|
|
680
|
-
progress: queue.progress || {
|
|
681
|
-
total: 0,
|
|
682
|
-
completed: 0,
|
|
683
|
-
failed: 0,
|
|
684
|
-
inProgress: 0
|
|
685
|
-
},
|
|
686
|
-
lastRunAt: queue.lastRunAt || null,
|
|
687
|
-
tasksCount: ((_b = queue.tasks) == null ? void 0 : _b.length) || 0
|
|
688
|
-
};
|
|
689
|
-
}
|
|
690
|
-
/**
|
|
691
|
-
* @title: 获取所有队列状态
|
|
692
|
-
* @description: 获取所有任务队列的执行状态概览
|
|
693
|
-
* @return {object} 所有队列的状态信息
|
|
694
|
-
*/
|
|
695
|
-
getAllQueuesStatus() {
|
|
696
|
-
var _a;
|
|
697
|
-
const result = {};
|
|
698
|
-
for (const module2 in this.tasks) {
|
|
699
|
-
result[module2] = {};
|
|
700
|
-
for (const queueId in this.tasks[module2]) {
|
|
701
|
-
const queue = this.tasks[module2][queueId];
|
|
702
|
-
result[module2][queueId] = {
|
|
703
|
-
isRunning: queue.isRunning || false,
|
|
704
|
-
status: queue.status,
|
|
705
|
-
progress: queue.progress || {
|
|
706
|
-
total: 0,
|
|
707
|
-
completed: 0,
|
|
708
|
-
failed: 0,
|
|
709
|
-
inProgress: 0
|
|
710
|
-
},
|
|
711
|
-
lastRunAt: queue.lastRunAt || null,
|
|
712
|
-
tasksCount: ((_a = queue.tasks) == null ? void 0 : _a.length) || 0
|
|
713
|
-
};
|
|
714
|
-
}
|
|
715
|
-
}
|
|
716
|
-
return result;
|
|
717
|
-
}
|
|
718
469
|
};
|
|
719
470
|
// Annotate the CommonJS export names for ESM import in node:
|
|
720
471
|
0 && (module.exports = {
|
package/lib/tasks/type.d.ts
CHANGED
|
@@ -18,18 +18,6 @@ export interface Task {
|
|
|
18
18
|
count: number;
|
|
19
19
|
timerId?: any;
|
|
20
20
|
};
|
|
21
|
-
scheduled?: {
|
|
22
|
-
executeAt: string | string[];
|
|
23
|
-
repeat?: boolean;
|
|
24
|
-
repeatType?: 'daily' | 'weekly' | 'monthly' | 'yearly';
|
|
25
|
-
repeatInterval?: number;
|
|
26
|
-
endAt?: string;
|
|
27
|
-
};
|
|
28
|
-
scheduledResult?: {
|
|
29
|
-
count: number;
|
|
30
|
-
timerId?: any;
|
|
31
|
-
nextExecuteTime?: string;
|
|
32
|
-
};
|
|
33
21
|
manual?: boolean;
|
|
34
22
|
destroy?: boolean;
|
|
35
23
|
[key: string]: any;
|
|
@@ -43,14 +31,6 @@ declare type TaskStatus = "uncompleted" | "completed";
|
|
|
43
31
|
export interface TaskQueue {
|
|
44
32
|
status: TaskStatus;
|
|
45
33
|
tasks: Task[];
|
|
46
|
-
isRunning?: boolean;
|
|
47
|
-
progress?: {
|
|
48
|
-
total: number;
|
|
49
|
-
completed: number;
|
|
50
|
-
failed: number;
|
|
51
|
-
inProgress: number;
|
|
52
|
-
};
|
|
53
|
-
lastRunAt?: string;
|
|
54
34
|
}
|
|
55
35
|
export interface RunTaskParams {
|
|
56
36
|
module: TaskModuleName;
|
|
@@ -67,33 +47,15 @@ export interface AddTaskParams {
|
|
|
67
47
|
queueId: TaskQueueName;
|
|
68
48
|
tasks: Task[];
|
|
69
49
|
}
|
|
70
|
-
export interface AddTaskDataParams {
|
|
71
|
-
module: TaskModuleName;
|
|
72
|
-
queueId: TaskQueueName;
|
|
73
|
-
[key: string]: any;
|
|
74
|
-
}
|
|
75
50
|
export interface TaskRunResult {
|
|
76
51
|
status: TaskRunStatus;
|
|
77
52
|
[key: string]: any;
|
|
78
53
|
}
|
|
79
|
-
/**
|
|
80
|
-
* 任务模块
|
|
81
|
-
* 注意:'scheduledTasks' 是保留的模块名,专门用于定时任务
|
|
82
|
-
* 在其他模块中,scheduled 配置会被忽略,任务将作为普通任务执行
|
|
83
|
-
*/
|
|
84
54
|
export interface TasksModule {
|
|
85
55
|
[key: TaskModuleName]: {
|
|
86
56
|
[key: TaskQueueName]: {
|
|
87
57
|
status: TaskStatus;
|
|
88
58
|
tasks: Task[];
|
|
89
|
-
isRunning?: boolean;
|
|
90
|
-
progress?: {
|
|
91
|
-
total: number;
|
|
92
|
-
completed: number;
|
|
93
|
-
failed: number;
|
|
94
|
-
inProgress: number;
|
|
95
|
-
};
|
|
96
|
-
lastRunAt?: string;
|
|
97
59
|
};
|
|
98
60
|
};
|
|
99
61
|
}
|