@pisell/core 1.0.27 → 1.0.28
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/tasks/index.js +444 -101
- package/es/tasks/scheduledTasksExample.js +351 -0
- package/lib/tasks/index.js +326 -77
- package/lib/tasks/scheduledTasksExample.js +267 -0
- package/package.json +1 -1
- package/es/app/app.d.ts +0 -89
- package/es/request/cache.d.ts +0 -46
- package/es/request/index.d.ts +0 -24
- package/es/request/type.d.ts +0 -42
- package/es/request/utils.d.ts +0 -46
- package/es/tasks/index.d.ts +0 -83
- package/es/tasks/type.d.ts +0 -62
- package/lib/app/app.d.ts +0 -89
- package/lib/request/cache.d.ts +0 -46
- package/lib/request/index.d.ts +0 -24
- package/lib/request/type.d.ts +0 -42
- package/lib/request/utils.d.ts +0 -46
- package/lib/tasks/index.d.ts +0 -83
- package/lib/tasks/type.d.ts +0 -62
package/es/tasks/index.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
var _excluded = ["queueId", "module"];
|
|
1
2
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
3
|
+
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
4
|
+
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
2
5
|
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
3
6
|
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
4
7
|
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
@@ -26,7 +29,10 @@ export var TasksManager = /*#__PURE__*/function () {
|
|
|
26
29
|
var _this = this;
|
|
27
30
|
_classCallCheck(this, TasksManager);
|
|
28
31
|
_defineProperty(this, "taskFunctions", void 0);
|
|
29
|
-
_defineProperty(this, "tasks",
|
|
32
|
+
_defineProperty(this, "tasks", {
|
|
33
|
+
// 内置模块, 定时任务专用
|
|
34
|
+
scheduledTasks: {}
|
|
35
|
+
});
|
|
30
36
|
_defineProperty(this, "app", void 0);
|
|
31
37
|
_defineProperty(this, "db", void 0);
|
|
32
38
|
_defineProperty(this, "useTasks", useTasks);
|
|
@@ -234,6 +240,195 @@ export var TasksManager = /*#__PURE__*/function () {
|
|
|
234
240
|
}
|
|
235
241
|
_this.timerIds = _toConsumableArray(_timerIds || []);
|
|
236
242
|
});
|
|
243
|
+
/**
|
|
244
|
+
* @title: 计算下一次执行时间
|
|
245
|
+
* @description: 根据定时任务配置计算下一次执行时间
|
|
246
|
+
* @param {Task} task
|
|
247
|
+
* @return {string | null} 下一次执行时间
|
|
248
|
+
*/
|
|
249
|
+
_defineProperty(this, "calculateNextExecuteTime", function (task) {
|
|
250
|
+
if (!task.scheduled) {
|
|
251
|
+
return null;
|
|
252
|
+
}
|
|
253
|
+
var scheduled = task.scheduled,
|
|
254
|
+
scheduledResult = task.scheduledResult;
|
|
255
|
+
var now = dayjs();
|
|
256
|
+
|
|
257
|
+
// 如果有结束时间,检查是否已超过
|
|
258
|
+
if (scheduled.endAt && now.isAfter(dayjs(scheduled.endAt))) {
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// 如果 executeAt 是数组,找到第一个未来的时间点
|
|
263
|
+
if (Array.isArray(scheduled.executeAt)) {
|
|
264
|
+
var futureTime = scheduled.executeAt.map(function (time) {
|
|
265
|
+
return dayjs(time);
|
|
266
|
+
}).filter(function (time) {
|
|
267
|
+
return time.isAfter(now);
|
|
268
|
+
}).sort(function (a, b) {
|
|
269
|
+
return a.valueOf() - b.valueOf();
|
|
270
|
+
})[0];
|
|
271
|
+
if (futureTime) {
|
|
272
|
+
return futureTime.format("YYYY-MM-DD HH:mm:ss");
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// 如果所有时间点都已过,且设置了重复
|
|
276
|
+
if (!scheduled.repeat) {
|
|
277
|
+
return null;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// 如果是重复任务,计算下一次执行时间
|
|
282
|
+
if (scheduled.repeat && scheduledResult !== null && scheduledResult !== void 0 && scheduledResult.nextExecuteTime) {
|
|
283
|
+
var lastExecuteTime = dayjs(scheduledResult.nextExecuteTime);
|
|
284
|
+
var interval = scheduled.repeatInterval || 1;
|
|
285
|
+
var nextTime = lastExecuteTime;
|
|
286
|
+
switch (scheduled.repeatType) {
|
|
287
|
+
case 'daily':
|
|
288
|
+
nextTime = lastExecuteTime.add(interval, 'day');
|
|
289
|
+
break;
|
|
290
|
+
case 'weekly':
|
|
291
|
+
nextTime = lastExecuteTime.add(interval, 'week');
|
|
292
|
+
break;
|
|
293
|
+
case 'monthly':
|
|
294
|
+
nextTime = lastExecuteTime.add(interval, 'month');
|
|
295
|
+
break;
|
|
296
|
+
case 'yearly':
|
|
297
|
+
nextTime = lastExecuteTime.add(interval, 'year');
|
|
298
|
+
break;
|
|
299
|
+
default:
|
|
300
|
+
nextTime = lastExecuteTime.add(interval, 'day');
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// 检查是否超过结束时间
|
|
304
|
+
if (scheduled.endAt && nextTime.isAfter(dayjs(scheduled.endAt))) {
|
|
305
|
+
return null;
|
|
306
|
+
}
|
|
307
|
+
return nextTime.format("YYYY-MM-DD HH:mm:ss");
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// 首次执行,返回 executeAt 指定的时间
|
|
311
|
+
var executeTime = Array.isArray(scheduled.executeAt) ? scheduled.executeAt[0] : scheduled.executeAt;
|
|
312
|
+
return dayjs(executeTime).format("YYYY-MM-DD HH:mm:ss");
|
|
313
|
+
});
|
|
314
|
+
/**
|
|
315
|
+
* @title: 启动定时任务
|
|
316
|
+
* @description: 在特定时间点执行任务(仅在 scheduledTasks 模块中生效)
|
|
317
|
+
* @param {Task} task
|
|
318
|
+
* @return {*}
|
|
319
|
+
*/
|
|
320
|
+
_defineProperty(this, "startScheduledTask", function (task) {
|
|
321
|
+
if (!task.scheduled) {
|
|
322
|
+
console.log("Tasks--->", "不是定时任务");
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// 安全检查:确保任务属于 scheduledTasks 模块
|
|
327
|
+
if (task.module !== 'scheduledTasks') {
|
|
328
|
+
console.warn("Tasks--->", "\u5B9A\u65F6\u4EFB\u52A1\u53EA\u5728 scheduledTasks \u6A21\u5757\u4E2D\u751F\u6548\uFF0C\u4EFB\u52A1 ".concat(task.id, " \u5C06\u4F5C\u4E3A\u666E\u901A\u4EFB\u52A1\u6267\u884C"));
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
var nextExecuteTime = _this.calculateNextExecuteTime(task);
|
|
332
|
+
if (!nextExecuteTime) {
|
|
333
|
+
console.log("Tasks--->", "定时任务已完成或无下次执行时间", task);
|
|
334
|
+
// 删除已完成的定时任务
|
|
335
|
+
_this.deleteTask({
|
|
336
|
+
module: task.module,
|
|
337
|
+
queueId: task.queueId,
|
|
338
|
+
taskId: task.id
|
|
339
|
+
});
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
var now = dayjs();
|
|
343
|
+
var executeTime = dayjs(nextExecuteTime);
|
|
344
|
+
var delay = executeTime.diff(now);
|
|
345
|
+
if (delay < 0) {
|
|
346
|
+
console.log("Tasks--->", "执行时间已过,跳过此次执行");
|
|
347
|
+
// 如果是重复任务,计算下一次执行时间
|
|
348
|
+
if (task.scheduled.repeat) {
|
|
349
|
+
var _task$scheduledResult;
|
|
350
|
+
var _task = _objectSpread({}, task);
|
|
351
|
+
_task.scheduledResult = {
|
|
352
|
+
count: ((_task$scheduledResult = _task.scheduledResult) === null || _task$scheduledResult === void 0 ? void 0 : _task$scheduledResult.count) || 0,
|
|
353
|
+
nextExecuteTime: nextExecuteTime
|
|
354
|
+
};
|
|
355
|
+
var newTask = _this.updateTask({
|
|
356
|
+
module: task.module,
|
|
357
|
+
queueId: task.queueId,
|
|
358
|
+
taskId: task.id,
|
|
359
|
+
other: _task
|
|
360
|
+
});
|
|
361
|
+
if (newTask) {
|
|
362
|
+
_this.startScheduledTask(newTask);
|
|
363
|
+
}
|
|
364
|
+
} else {
|
|
365
|
+
_this.deleteTask({
|
|
366
|
+
module: task.module,
|
|
367
|
+
queueId: task.queueId,
|
|
368
|
+
taskId: task.id
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
console.log("Tasks--->", "\u5B9A\u65F6\u4EFB\u52A1\u5C06\u5728 ".concat(nextExecuteTime, " \u6267\u884C (").concat(delay, "ms \u540E)"), task);
|
|
374
|
+
|
|
375
|
+
// 创建定时器
|
|
376
|
+
var timerId = setTimeout( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
|
|
377
|
+
var _task2$scheduledResul, _task$scheduled, _task2, _newTask;
|
|
378
|
+
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
379
|
+
while (1) switch (_context4.prev = _context4.next) {
|
|
380
|
+
case 0:
|
|
381
|
+
_context4.prev = 0;
|
|
382
|
+
_context4.next = 3;
|
|
383
|
+
return _this.runTask(task);
|
|
384
|
+
case 3:
|
|
385
|
+
console.log("Tasks--->", "定时任务执行完成", task);
|
|
386
|
+
_task2 = _objectSpread({}, task);
|
|
387
|
+
_task2.scheduledResult = {
|
|
388
|
+
count: (((_task2$scheduledResul = _task2.scheduledResult) === null || _task2$scheduledResul === void 0 ? void 0 : _task2$scheduledResul.count) || 0) + 1,
|
|
389
|
+
timerId: timerId,
|
|
390
|
+
nextExecuteTime: nextExecuteTime
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
// 如果是重复任务,继续调度下一次执行
|
|
394
|
+
if ((_task$scheduled = task.scheduled) !== null && _task$scheduled !== void 0 && _task$scheduled.repeat) {
|
|
395
|
+
_newTask = _this.updateTask({
|
|
396
|
+
module: task.module,
|
|
397
|
+
queueId: task.queueId,
|
|
398
|
+
taskId: task.id,
|
|
399
|
+
other: _task2
|
|
400
|
+
});
|
|
401
|
+
if (_newTask) {
|
|
402
|
+
_this.startScheduledTask(_newTask);
|
|
403
|
+
}
|
|
404
|
+
} else {
|
|
405
|
+
// 一次性任务执行完成后删除
|
|
406
|
+
_this.deleteTask({
|
|
407
|
+
module: task.module,
|
|
408
|
+
queueId: task.queueId,
|
|
409
|
+
taskId: task.id
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
_context4.next = 13;
|
|
413
|
+
break;
|
|
414
|
+
case 9:
|
|
415
|
+
_context4.prev = 9;
|
|
416
|
+
_context4.t0 = _context4["catch"](0);
|
|
417
|
+
_this.clearTaskTimer({
|
|
418
|
+
timerId: timerId
|
|
419
|
+
});
|
|
420
|
+
console.error("定时任务执行异常", _context4.t0);
|
|
421
|
+
case 13:
|
|
422
|
+
case "end":
|
|
423
|
+
return _context4.stop();
|
|
424
|
+
}
|
|
425
|
+
}, _callee4, null, [[0, 9]]);
|
|
426
|
+
})), delay);
|
|
427
|
+
_this.timerIds.push({
|
|
428
|
+
taskId: task.id,
|
|
429
|
+
timerId: timerId
|
|
430
|
+
});
|
|
431
|
+
});
|
|
237
432
|
/**
|
|
238
433
|
* @title: 启动轮询
|
|
239
434
|
* @description: 根据轮询间隔定期执行任务
|
|
@@ -247,13 +442,13 @@ export var TasksManager = /*#__PURE__*/function () {
|
|
|
247
442
|
}
|
|
248
443
|
|
|
249
444
|
// 创建轮询定时器
|
|
250
|
-
var timerId = setTimeout( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
445
|
+
var timerId = setTimeout( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5() {
|
|
251
446
|
var _task$pollingResult, _task, newTask;
|
|
252
|
-
return _regeneratorRuntime().wrap(function
|
|
253
|
-
while (1) switch (
|
|
447
|
+
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
448
|
+
while (1) switch (_context5.prev = _context5.next) {
|
|
254
449
|
case 0:
|
|
255
|
-
|
|
256
|
-
|
|
450
|
+
_context5.prev = 0;
|
|
451
|
+
_context5.next = 3;
|
|
257
452
|
return _this.runTask(task);
|
|
258
453
|
case 3:
|
|
259
454
|
console.log("轮询任务", task);
|
|
@@ -269,26 +464,26 @@ export var TasksManager = /*#__PURE__*/function () {
|
|
|
269
464
|
other: _task
|
|
270
465
|
});
|
|
271
466
|
if (newTask) {
|
|
272
|
-
|
|
467
|
+
_context5.next = 9;
|
|
273
468
|
break;
|
|
274
469
|
}
|
|
275
|
-
return
|
|
470
|
+
return _context5.abrupt("return");
|
|
276
471
|
case 9:
|
|
277
472
|
_this.startPolling(newTask);
|
|
278
|
-
|
|
473
|
+
_context5.next = 16;
|
|
279
474
|
break;
|
|
280
475
|
case 12:
|
|
281
|
-
|
|
282
|
-
|
|
476
|
+
_context5.prev = 12;
|
|
477
|
+
_context5.t0 = _context5["catch"](0);
|
|
283
478
|
_this.clearTaskTimer({
|
|
284
479
|
timerId: timerId
|
|
285
480
|
});
|
|
286
|
-
console.error("轮询任务异常",
|
|
481
|
+
console.error("轮询任务异常", _context5.t0);
|
|
287
482
|
case 16:
|
|
288
483
|
case "end":
|
|
289
|
-
return
|
|
484
|
+
return _context5.stop();
|
|
290
485
|
}
|
|
291
|
-
},
|
|
486
|
+
}, _callee5, null, [[0, 12]]);
|
|
292
487
|
})), task.polling.interval);
|
|
293
488
|
_this.timerIds.push({
|
|
294
489
|
taskId: task.id,
|
|
@@ -344,7 +539,9 @@ export var TasksManager = /*#__PURE__*/function () {
|
|
|
344
539
|
}
|
|
345
540
|
this.app = app;
|
|
346
541
|
this.taskFunctions = new Map();
|
|
347
|
-
this.tasks = {
|
|
542
|
+
this.tasks = {
|
|
543
|
+
scheduledTasks: {}
|
|
544
|
+
};
|
|
348
545
|
this.db = app.dbManager;
|
|
349
546
|
this.timerIds = [];
|
|
350
547
|
}
|
|
@@ -363,9 +560,9 @@ export var TasksManager = /*#__PURE__*/function () {
|
|
|
363
560
|
key: "addTaskFunctions",
|
|
364
561
|
value: function addTaskFunctions(tasks) {
|
|
365
562
|
var _this2 = this;
|
|
366
|
-
tasks.forEach(function (
|
|
367
|
-
var name =
|
|
368
|
-
fun =
|
|
563
|
+
tasks.forEach(function (_ref6) {
|
|
564
|
+
var name = _ref6.name,
|
|
565
|
+
fun = _ref6.fun;
|
|
369
566
|
_this2.taskFunctions.set(name, fun);
|
|
370
567
|
});
|
|
371
568
|
}
|
|
@@ -389,15 +586,15 @@ export var TasksManager = /*#__PURE__*/function () {
|
|
|
389
586
|
}, {
|
|
390
587
|
key: "init",
|
|
391
588
|
value: function () {
|
|
392
|
-
var _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
589
|
+
var _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
|
|
393
590
|
var tasks;
|
|
394
|
-
return _regeneratorRuntime().wrap(function
|
|
395
|
-
while (1) switch (
|
|
591
|
+
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
592
|
+
while (1) switch (_context6.prev = _context6.next) {
|
|
396
593
|
case 0:
|
|
397
|
-
|
|
594
|
+
_context6.next = 2;
|
|
398
595
|
return this.loadTaskQueueFromLocal();
|
|
399
596
|
case 2:
|
|
400
|
-
tasks =
|
|
597
|
+
tasks = _context6.sent;
|
|
401
598
|
if (tasks) {
|
|
402
599
|
console.log("initTasks", tasks);
|
|
403
600
|
// this.tasks = tasks;
|
|
@@ -406,9 +603,9 @@ export var TasksManager = /*#__PURE__*/function () {
|
|
|
406
603
|
}
|
|
407
604
|
case 4:
|
|
408
605
|
case "end":
|
|
409
|
-
return
|
|
606
|
+
return _context6.stop();
|
|
410
607
|
}
|
|
411
|
-
},
|
|
608
|
+
}, _callee6, this);
|
|
412
609
|
}));
|
|
413
610
|
function init() {
|
|
414
611
|
return _init.apply(this, arguments);
|
|
@@ -426,22 +623,42 @@ export var TasksManager = /*#__PURE__*/function () {
|
|
|
426
623
|
* @Date: 2024-09-26 13:52
|
|
427
624
|
*/
|
|
428
625
|
function () {
|
|
429
|
-
var _run = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
430
|
-
var
|
|
431
|
-
|
|
432
|
-
|
|
626
|
+
var _run = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(payload) {
|
|
627
|
+
var _this$tasks$module2;
|
|
628
|
+
var queueId, module, callback, currentQueue, _currentQueue$progres, _currentQueue$progres2, taskQueueStatus, taskQueue, errorTaskIds, _this$getTaskQueue, tasksToExecute, _iterator3, _step3, task, _yield$this$runTask, status, _task$pollingResult2, _other;
|
|
629
|
+
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
|
|
630
|
+
while (1) switch (_context7.prev = _context7.next) {
|
|
433
631
|
case 0:
|
|
434
|
-
queueId = payload.queueId, module = payload.module, callback = payload.callback;
|
|
632
|
+
queueId = payload.queueId, module = payload.module, callback = payload.callback; // 检查任务队列是否正在执行
|
|
633
|
+
currentQueue = (_this$tasks$module2 = this.tasks[module]) === null || _this$tasks$module2 === void 0 ? void 0 : _this$tasks$module2[queueId];
|
|
634
|
+
if (!(currentQueue !== null && currentQueue !== void 0 && currentQueue.isRunning)) {
|
|
635
|
+
_context7.next = 5;
|
|
636
|
+
break;
|
|
637
|
+
}
|
|
638
|
+
console.warn("Tasks--->", "\u4EFB\u52A1\u961F\u5217 [".concat(module, "/").concat(queueId, "] \u6B63\u5728\u6267\u884C\u4E2D\uFF0C\u5DF2\u62E6\u622A\u91CD\u590D\u8C03\u7528\u3002"), "\u5F53\u524D\u8FDB\u5EA6: ".concat(((_currentQueue$progres = currentQueue.progress) === null || _currentQueue$progres === void 0 ? void 0 : _currentQueue$progres.completed) || 0, "/").concat(((_currentQueue$progres2 = currentQueue.progress) === null || _currentQueue$progres2 === void 0 ? void 0 : _currentQueue$progres2.total) || 0), "\u5982\u9700\u91CD\u65B0\u6267\u884C\uFF0C\u8BF7\u7B49\u5F85\u5F53\u524D\u4EFB\u52A1\u961F\u5217\u6267\u884C\u5B8C\u6210\u3002");
|
|
639
|
+
return _context7.abrupt("return");
|
|
640
|
+
case 5:
|
|
641
|
+
// 标记队列为执行中
|
|
642
|
+
this.updateQueueRunningState({
|
|
643
|
+
module: module,
|
|
644
|
+
queueId: queueId,
|
|
645
|
+
isRunning: true,
|
|
646
|
+
lastRunAt: dayjs().format("YYYY-MM-DD HH:mm:ss")
|
|
647
|
+
});
|
|
648
|
+
console.log("Tasks--->", "\u4EFB\u52A1\u961F\u5217 [".concat(module, "/").concat(queueId, "] \u5F00\u59CB\u6267\u884C"));
|
|
649
|
+
|
|
650
|
+
//@ts-ignore 当前任务队列
|
|
435
651
|
taskQueueStatus = "";
|
|
436
|
-
|
|
652
|
+
_context7.prev = 8;
|
|
653
|
+
case 9:
|
|
437
654
|
if (!(taskQueueStatus !== "completed")) {
|
|
438
|
-
|
|
655
|
+
_context7.next = 72;
|
|
439
656
|
break;
|
|
440
657
|
}
|
|
441
658
|
taskQueue = this.getTaskQueue(payload);
|
|
442
659
|
errorTaskIds = [];
|
|
443
660
|
if (!(taskQueue && taskQueue.length)) {
|
|
444
|
-
|
|
661
|
+
_context7.next = 62;
|
|
445
662
|
break;
|
|
446
663
|
}
|
|
447
664
|
// 只执行未执行(pending)或失败(failure)的任务
|
|
@@ -450,17 +667,17 @@ export var TasksManager = /*#__PURE__*/function () {
|
|
|
450
667
|
});
|
|
451
668
|
console.log("Tasks--->", "需要执行的任务", tasksToExecute);
|
|
452
669
|
_iterator3 = _createForOfIteratorHelper(tasksToExecute);
|
|
453
|
-
|
|
670
|
+
_context7.prev = 16;
|
|
454
671
|
_iterator3.s();
|
|
455
|
-
case
|
|
672
|
+
case 18:
|
|
456
673
|
if ((_step3 = _iterator3.n()).done) {
|
|
457
|
-
|
|
674
|
+
_context7.next = 49;
|
|
458
675
|
break;
|
|
459
676
|
}
|
|
460
677
|
task = _step3.value;
|
|
461
|
-
|
|
678
|
+
_context7.prev = 20;
|
|
462
679
|
if (!(task.retries !== undefined && task.maxRetries !== undefined && task.retries < task.maxRetries)) {
|
|
463
|
-
|
|
680
|
+
_context7.next = 40;
|
|
464
681
|
break;
|
|
465
682
|
}
|
|
466
683
|
// 标记任务为执行中
|
|
@@ -474,23 +691,31 @@ export var TasksManager = /*#__PURE__*/function () {
|
|
|
474
691
|
}
|
|
475
692
|
});
|
|
476
693
|
|
|
477
|
-
//
|
|
694
|
+
// 定时任务(仅在 scheduledTasks 模块中生效)
|
|
695
|
+
if (!(task.scheduled && module === 'scheduledTasks')) {
|
|
696
|
+
_context7.next = 28;
|
|
697
|
+
break;
|
|
698
|
+
}
|
|
699
|
+
this.startScheduledTask(task);
|
|
700
|
+
_context7.next = 37;
|
|
701
|
+
break;
|
|
702
|
+
case 28:
|
|
478
703
|
if (!task.polling) {
|
|
479
|
-
|
|
704
|
+
_context7.next = 32;
|
|
480
705
|
break;
|
|
481
706
|
}
|
|
482
707
|
this.startPolling(task);
|
|
483
|
-
|
|
708
|
+
_context7.next = 37;
|
|
484
709
|
break;
|
|
485
|
-
case
|
|
486
|
-
|
|
710
|
+
case 32:
|
|
711
|
+
_context7.next = 34;
|
|
487
712
|
return this.runTask(task);
|
|
488
|
-
case
|
|
489
|
-
_yield$this$runTask =
|
|
713
|
+
case 34:
|
|
714
|
+
_yield$this$runTask = _context7.sent;
|
|
490
715
|
status = _yield$this$runTask.status;
|
|
491
716
|
// 标记任务状态
|
|
492
717
|
task.status = status;
|
|
493
|
-
case
|
|
718
|
+
case 37:
|
|
494
719
|
// 成功: 删除任务
|
|
495
720
|
if (task.status === "success") {
|
|
496
721
|
if ((_task$pollingResult2 = task.pollingResult) !== null && _task$pollingResult2 !== void 0 && _task$pollingResult2.timerId) {}
|
|
@@ -514,9 +739,9 @@ export var TasksManager = /*#__PURE__*/function () {
|
|
|
514
739
|
other: _other
|
|
515
740
|
});
|
|
516
741
|
}
|
|
517
|
-
|
|
742
|
+
_context7.next = 42;
|
|
518
743
|
break;
|
|
519
|
-
case
|
|
744
|
+
case 40:
|
|
520
745
|
console.log("Tasks--->", "任务没有重试次数,需要删除", task);
|
|
521
746
|
// 如果任务没有重试次数,则删除任务
|
|
522
747
|
this.deleteTask({
|
|
@@ -524,28 +749,28 @@ export var TasksManager = /*#__PURE__*/function () {
|
|
|
524
749
|
queueId: queueId,
|
|
525
750
|
taskId: task.id
|
|
526
751
|
});
|
|
527
|
-
case
|
|
528
|
-
|
|
752
|
+
case 42:
|
|
753
|
+
_context7.next = 47;
|
|
529
754
|
break;
|
|
530
|
-
case
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
console.error("任务异常",
|
|
534
|
-
case
|
|
535
|
-
|
|
755
|
+
case 44:
|
|
756
|
+
_context7.prev = 44;
|
|
757
|
+
_context7.t0 = _context7["catch"](20);
|
|
758
|
+
console.error("任务异常", _context7.t0);
|
|
759
|
+
case 47:
|
|
760
|
+
_context7.next = 18;
|
|
536
761
|
break;
|
|
537
|
-
case
|
|
538
|
-
|
|
762
|
+
case 49:
|
|
763
|
+
_context7.next = 54;
|
|
539
764
|
break;
|
|
540
|
-
case
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
_iterator3.e(
|
|
544
|
-
case
|
|
545
|
-
|
|
765
|
+
case 51:
|
|
766
|
+
_context7.prev = 51;
|
|
767
|
+
_context7.t1 = _context7["catch"](16);
|
|
768
|
+
_iterator3.e(_context7.t1);
|
|
769
|
+
case 54:
|
|
770
|
+
_context7.prev = 54;
|
|
546
771
|
_iterator3.f();
|
|
547
|
-
return
|
|
548
|
-
case
|
|
772
|
+
return _context7.finish(54);
|
|
773
|
+
case 57:
|
|
549
774
|
taskQueueStatus = (_this$getTaskQueue = this.getTaskQueue(payload)) !== null && _this$getTaskQueue !== void 0 && _this$getTaskQueue.some(function (task) {
|
|
550
775
|
return task.status === "failure";
|
|
551
776
|
}) ? "uncompleted" : "completed";
|
|
@@ -555,13 +780,13 @@ export var TasksManager = /*#__PURE__*/function () {
|
|
|
555
780
|
status: taskQueueStatus
|
|
556
781
|
});
|
|
557
782
|
console.log("Tasks--->", "任务队列执行完成", taskQueue);
|
|
558
|
-
|
|
783
|
+
_context7.next = 63;
|
|
559
784
|
break;
|
|
560
|
-
case
|
|
785
|
+
case 62:
|
|
561
786
|
taskQueueStatus = "completed";
|
|
562
|
-
case
|
|
787
|
+
case 63:
|
|
563
788
|
if (!(taskQueueStatus === "uncompleted")) {
|
|
564
|
-
|
|
789
|
+
_context7.next = 69;
|
|
565
790
|
break;
|
|
566
791
|
}
|
|
567
792
|
this.app.logger.addLog({
|
|
@@ -572,24 +797,34 @@ export var TasksManager = /*#__PURE__*/function () {
|
|
|
572
797
|
errorTaskIds: errorTaskIds
|
|
573
798
|
}
|
|
574
799
|
});
|
|
575
|
-
|
|
800
|
+
_context7.next = 67;
|
|
576
801
|
return this.timeout();
|
|
577
|
-
case
|
|
578
|
-
|
|
802
|
+
case 67:
|
|
803
|
+
_context7.next = 70;
|
|
579
804
|
break;
|
|
580
|
-
case
|
|
805
|
+
case 69:
|
|
581
806
|
if (taskQueueStatus === "completed") {
|
|
582
807
|
console.log("Tasks--->", "任务队列全部执行完成");
|
|
583
808
|
callback === null || callback === void 0 || callback();
|
|
584
809
|
}
|
|
585
|
-
case
|
|
586
|
-
|
|
810
|
+
case 70:
|
|
811
|
+
_context7.next = 9;
|
|
587
812
|
break;
|
|
588
|
-
case
|
|
813
|
+
case 72:
|
|
814
|
+
_context7.prev = 72;
|
|
815
|
+
// 无论成功还是失败,都要解除队列的执行状态
|
|
816
|
+
this.updateQueueRunningState({
|
|
817
|
+
module: module,
|
|
818
|
+
queueId: queueId,
|
|
819
|
+
isRunning: false
|
|
820
|
+
});
|
|
821
|
+
console.log("Tasks--->", "\u4EFB\u52A1\u961F\u5217 [".concat(module, "/").concat(queueId, "] \u6267\u884C\u7ED3\u675F"));
|
|
822
|
+
return _context7.finish(72);
|
|
823
|
+
case 76:
|
|
589
824
|
case "end":
|
|
590
|
-
return
|
|
825
|
+
return _context7.stop();
|
|
591
826
|
}
|
|
592
|
-
},
|
|
827
|
+
}, _callee7, this, [[8,, 72, 76], [16, 51, 54, 57], [20, 44]]);
|
|
593
828
|
}));
|
|
594
829
|
function run(_x3) {
|
|
595
830
|
return _run.apply(this, arguments);
|
|
@@ -605,13 +840,23 @@ export var TasksManager = /*#__PURE__*/function () {
|
|
|
605
840
|
module = payload.module,
|
|
606
841
|
taskId = payload.taskId;
|
|
607
842
|
this.tasks[module][queueId].tasks = (_this$tasks = this.tasks) === null || _this$tasks === void 0 || (_this$tasks = _this$tasks[module]) === null || _this$tasks === void 0 || (_this$tasks = _this$tasks[queueId]) === null || _this$tasks === void 0 || (_this$tasks = _this$tasks.tasks) === null || _this$tasks === void 0 ? void 0 : _this$tasks.filter(function (task) {
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
843
|
+
if (task.id === taskId) {
|
|
844
|
+
var _task$pollingResult3, _task$scheduledResult2;
|
|
845
|
+
// 清除轮询任务定时器
|
|
846
|
+
if ((_task$pollingResult3 = task.pollingResult) !== null && _task$pollingResult3 !== void 0 && _task$pollingResult3.timerId) {
|
|
847
|
+
_this3.clearTaskTimer({
|
|
848
|
+
timerId: task.pollingResult.timerId
|
|
849
|
+
});
|
|
850
|
+
}
|
|
851
|
+
// 清除定时任务定时器
|
|
852
|
+
if ((_task$scheduledResult2 = task.scheduledResult) !== null && _task$scheduledResult2 !== void 0 && _task$scheduledResult2.timerId) {
|
|
853
|
+
_this3.clearTaskTimer({
|
|
854
|
+
timerId: task.scheduledResult.timerId
|
|
855
|
+
});
|
|
856
|
+
}
|
|
857
|
+
return false;
|
|
613
858
|
}
|
|
614
|
-
return
|
|
859
|
+
return true;
|
|
615
860
|
});
|
|
616
861
|
this.saveTaskQueueToLocal(this.tasks);
|
|
617
862
|
console.log("Tasks--->", "删除指定任务", queueId, module, taskId);
|
|
@@ -639,19 +884,23 @@ export var TasksManager = /*#__PURE__*/function () {
|
|
|
639
884
|
}, {
|
|
640
885
|
key: "addTask",
|
|
641
886
|
value: function addTask(payload) {
|
|
642
|
-
var _this$tasks$
|
|
887
|
+
var _this$tasks$module3,
|
|
643
888
|
_tasks$map,
|
|
644
889
|
_this4 = this;
|
|
645
890
|
var queueId = payload.queueId,
|
|
646
891
|
module = payload.module,
|
|
647
892
|
tasks = payload.tasks;
|
|
648
893
|
|
|
649
|
-
|
|
650
|
-
var taskQueue = (_this$tasks$
|
|
894
|
+
// 当前任务队列
|
|
895
|
+
var taskQueue = (_this$tasks$module3 = this.tasks[module]) === null || _this$tasks$module3 === void 0 || (_this$tasks$module3 = _this$tasks$module3[queueId]) === null || _this$tasks$module3 === void 0 ? void 0 : _this$tasks$module3.tasks;
|
|
651
896
|
var _tasks = _toConsumableArray(taskQueue || []);
|
|
652
897
|
|
|
653
898
|
// 创建新任务
|
|
654
899
|
var newTasks = tasks === null || tasks === void 0 || (_tasks$map = tasks.map) === null || _tasks$map === void 0 ? void 0 : _tasks$map.call(tasks, function (d) {
|
|
900
|
+
// 检查定时任务配置
|
|
901
|
+
if (d.scheduled && module !== 'scheduledTasks') {
|
|
902
|
+
console.warn("Tasks--->", "\u68C0\u6D4B\u5230\u5B9A\u65F6\u4EFB\u52A1\u914D\u7F6E\uFF0C\u4F46\u6A21\u5757\u4E3A \"".concat(module, "\"\u3002\u5B9A\u65F6\u4EFB\u52A1\u529F\u80FD\u4EC5\u5728 \"scheduledTasks\" \u6A21\u5757\u4E2D\u751F\u6548\u3002"), "\u8BE5\u4EFB\u52A1\u5C06\u4F5C\u4E3A\u666E\u901A\u4EFB\u52A1\u7ACB\u5373\u6267\u884C\u3002\u5982\u9700\u4F7F\u7528\u5B9A\u65F6\u529F\u80FD\uFF0C\u8BF7\u5C06\u4EFB\u52A1\u6DFB\u52A0\u5230 \"scheduledTasks\" \u6A21\u5757\u3002");
|
|
903
|
+
}
|
|
655
904
|
return _this4.createTaskData(_objectSpread(_objectSpread({}, d), {}, {
|
|
656
905
|
queueId: queueId,
|
|
657
906
|
module: module
|
|
@@ -672,13 +921,13 @@ export var TasksManager = /*#__PURE__*/function () {
|
|
|
672
921
|
}, {
|
|
673
922
|
key: "updateTask",
|
|
674
923
|
value: function updateTask(payload) {
|
|
675
|
-
var _this$tasks$
|
|
924
|
+
var _this$tasks$module4, _this$tasks$module5;
|
|
676
925
|
var queueId = payload.queueId,
|
|
677
926
|
taskId = payload.taskId,
|
|
678
927
|
other = payload.other,
|
|
679
928
|
module = payload.module;
|
|
680
929
|
var newTask = null;
|
|
681
|
-
var taskQueue = ((_this$tasks$
|
|
930
|
+
var taskQueue = ((_this$tasks$module4 = this.tasks[module]) === null || _this$tasks$module4 === void 0 || (_this$tasks$module4 = _this$tasks$module4[queueId]) === null || _this$tasks$module4 === void 0 ? void 0 : _this$tasks$module4.tasks) || [];
|
|
682
931
|
var updatedTasks = taskQueue.map(function (task) {
|
|
683
932
|
if (task.id === taskId) {
|
|
684
933
|
newTask = _objectSpread(_objectSpread({}, task), other); // 更新任务状态
|
|
@@ -686,7 +935,7 @@ export var TasksManager = /*#__PURE__*/function () {
|
|
|
686
935
|
}
|
|
687
936
|
return task;
|
|
688
937
|
});
|
|
689
|
-
var newState = _objectSpread(_objectSpread({}, this.tasks), {}, _defineProperty({}, module, _objectSpread(_objectSpread({}, this.tasks[module]), {}, _defineProperty({}, queueId, _objectSpread(_objectSpread({}, (_this$tasks$
|
|
938
|
+
var newState = _objectSpread(_objectSpread({}, this.tasks), {}, _defineProperty({}, module, _objectSpread(_objectSpread({}, this.tasks[module]), {}, _defineProperty({}, queueId, _objectSpread(_objectSpread({}, (_this$tasks$module5 = this.tasks[module]) === null || _this$tasks$module5 === void 0 ? void 0 : _this$tasks$module5[queueId]), {}, {
|
|
690
939
|
status: updatedTasks.some(function (task) {
|
|
691
940
|
return task.status === "failure";
|
|
692
941
|
}) ? "uncompleted" : "completed",
|
|
@@ -704,9 +953,43 @@ export var TasksManager = /*#__PURE__*/function () {
|
|
|
704
953
|
var queueId = payload.queueId,
|
|
705
954
|
status = payload.status,
|
|
706
955
|
module = payload.module;
|
|
707
|
-
|
|
956
|
+
this.setTasksData({
|
|
957
|
+
queueId: queueId,
|
|
958
|
+
module: module,
|
|
708
959
|
status: status
|
|
709
|
-
})
|
|
960
|
+
});
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
/**
|
|
964
|
+
* @title: 更新队列运行状态
|
|
965
|
+
* @description: 标记队列是否正在执行
|
|
966
|
+
*/
|
|
967
|
+
}, {
|
|
968
|
+
key: "updateQueueRunningState",
|
|
969
|
+
value: function updateQueueRunningState(payload) {
|
|
970
|
+
var queueId = payload.queueId,
|
|
971
|
+
module = payload.module,
|
|
972
|
+
isRunning = payload.isRunning,
|
|
973
|
+
lastRunAt = payload.lastRunAt;
|
|
974
|
+
this.setTasksData({
|
|
975
|
+
queueId: queueId,
|
|
976
|
+
module: module,
|
|
977
|
+
isRunning: isRunning,
|
|
978
|
+
lastRunAt: lastRunAt
|
|
979
|
+
});
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
// 设置任务
|
|
983
|
+
}, {
|
|
984
|
+
key: "setTasksData",
|
|
985
|
+
value: function setTasksData(payload) {
|
|
986
|
+
var queueId = payload.queueId,
|
|
987
|
+
module = payload.module,
|
|
988
|
+
data = _objectWithoutProperties(payload, _excluded);
|
|
989
|
+
var moduleData = this.tasks[module] || {};
|
|
990
|
+
var queueIdData = moduleData[queueId] || {};
|
|
991
|
+
var newState = _objectSpread(_objectSpread({}, this.tasks), {}, _defineProperty({}, module, _objectSpread(_objectSpread({}, moduleData), {}, _defineProperty({}, queueId, _objectSpread(_objectSpread({}, queueIdData), data)))));
|
|
992
|
+
|
|
710
993
|
// 保存任务队列到本地存储
|
|
711
994
|
this.saveTaskQueueToLocal(newState);
|
|
712
995
|
this.tasks = newState;
|
|
@@ -719,15 +1002,11 @@ export var TasksManager = /*#__PURE__*/function () {
|
|
|
719
1002
|
var queueId = payload.queueId,
|
|
720
1003
|
module = payload.module,
|
|
721
1004
|
tasks = payload.tasks;
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
1005
|
+
this.setTasksData({
|
|
1006
|
+
queueId: queueId,
|
|
1007
|
+
module: module,
|
|
725
1008
|
tasks: tasks
|
|
726
|
-
})
|
|
727
|
-
|
|
728
|
-
// 保存任务队列到本地存储
|
|
729
|
-
this.saveTaskQueueToLocal(newState);
|
|
730
|
-
this.tasks = newState;
|
|
1009
|
+
});
|
|
731
1010
|
}
|
|
732
1011
|
}, {
|
|
733
1012
|
key: "clearAllTaskTimer",
|
|
@@ -752,8 +1031,12 @@ export var TasksManager = /*#__PURE__*/function () {
|
|
|
752
1031
|
}, {
|
|
753
1032
|
key: "clearTasks",
|
|
754
1033
|
value: function clearTasks(payload) {
|
|
1034
|
+
var _this$tasks2;
|
|
755
1035
|
var queueId = payload.queueId,
|
|
756
1036
|
module = payload.module;
|
|
1037
|
+
if (!((_this$tasks2 = this.tasks) !== null && _this$tasks2 !== void 0 && (_this$tasks2 = _this$tasks2[module]) !== null && _this$tasks2 !== void 0 && _this$tasks2[queueId])) {
|
|
1038
|
+
return;
|
|
1039
|
+
}
|
|
757
1040
|
this.clearAllTaskTimer(this.tasks[module][queueId].tasks);
|
|
758
1041
|
this.tasks[module][queueId].tasks = [];
|
|
759
1042
|
this.saveTaskQueueToLocal(this.tasks);
|
|
@@ -788,6 +1071,66 @@ export var TasksManager = /*#__PURE__*/function () {
|
|
|
788
1071
|
value: function watchTask(callback) {
|
|
789
1072
|
this.watchTaskCallback = callback;
|
|
790
1073
|
}
|
|
1074
|
+
|
|
1075
|
+
/**
|
|
1076
|
+
* @title: 获取队列执行状态
|
|
1077
|
+
* @description: 获取指定队列的执行状态和进度信息
|
|
1078
|
+
* @param {string} module - 模块名
|
|
1079
|
+
* @param {string} queueId - 队列ID
|
|
1080
|
+
* @return {object} 队列状态信息
|
|
1081
|
+
*/
|
|
1082
|
+
}, {
|
|
1083
|
+
key: "getQueueStatus",
|
|
1084
|
+
value: function getQueueStatus(module, queueId) {
|
|
1085
|
+
var _this$tasks$module6, _queue$tasks;
|
|
1086
|
+
var queue = (_this$tasks$module6 = this.tasks[module]) === null || _this$tasks$module6 === void 0 ? void 0 : _this$tasks$module6[queueId];
|
|
1087
|
+
if (!queue) {
|
|
1088
|
+
return null;
|
|
1089
|
+
}
|
|
1090
|
+
return {
|
|
1091
|
+
isRunning: queue.isRunning || false,
|
|
1092
|
+
status: queue.status,
|
|
1093
|
+
progress: queue.progress || {
|
|
1094
|
+
total: 0,
|
|
1095
|
+
completed: 0,
|
|
1096
|
+
failed: 0,
|
|
1097
|
+
inProgress: 0
|
|
1098
|
+
},
|
|
1099
|
+
lastRunAt: queue.lastRunAt || null,
|
|
1100
|
+
tasksCount: ((_queue$tasks = queue.tasks) === null || _queue$tasks === void 0 ? void 0 : _queue$tasks.length) || 0
|
|
1101
|
+
};
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
/**
|
|
1105
|
+
* @title: 获取所有队列状态
|
|
1106
|
+
* @description: 获取所有任务队列的执行状态概览
|
|
1107
|
+
* @return {object} 所有队列的状态信息
|
|
1108
|
+
*/
|
|
1109
|
+
}, {
|
|
1110
|
+
key: "getAllQueuesStatus",
|
|
1111
|
+
value: function getAllQueuesStatus() {
|
|
1112
|
+
var result = {};
|
|
1113
|
+
for (var module in this.tasks) {
|
|
1114
|
+
result[module] = {};
|
|
1115
|
+
for (var queueId in this.tasks[module]) {
|
|
1116
|
+
var _queue$tasks2;
|
|
1117
|
+
var queue = this.tasks[module][queueId];
|
|
1118
|
+
result[module][queueId] = {
|
|
1119
|
+
isRunning: queue.isRunning || false,
|
|
1120
|
+
status: queue.status,
|
|
1121
|
+
progress: queue.progress || {
|
|
1122
|
+
total: 0,
|
|
1123
|
+
completed: 0,
|
|
1124
|
+
failed: 0,
|
|
1125
|
+
inProgress: 0
|
|
1126
|
+
},
|
|
1127
|
+
lastRunAt: queue.lastRunAt || null,
|
|
1128
|
+
tasksCount: ((_queue$tasks2 = queue.tasks) === null || _queue$tasks2 === void 0 ? void 0 : _queue$tasks2.length) || 0
|
|
1129
|
+
};
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
return result;
|
|
1133
|
+
}
|
|
791
1134
|
}], [{
|
|
792
1135
|
key: "getInstance",
|
|
793
1136
|
value: function getInstance(app) {
|