@kne/fastify-task 3.0.0-alpha.2 → 3.0.0-alpha.4

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.
@@ -52,6 +52,10 @@ module.exports = fp(async (fastify, options) => {
52
52
  maxRetries: {
53
53
  type: 'number',
54
54
  description: '最大自动重试次数,默认0'
55
+ },
56
+ timeout: {
57
+ type: 'number',
58
+ description: '任务超时时间(分钟),0表示不超时,默认60分钟'
55
59
  }
56
60
  },
57
61
  required: ['type', 'targetId', 'targetType']
@@ -43,6 +43,11 @@ module.exports = ({ DataTypes, definePrimaryType, options }) => {
43
43
  comment: '最大重试次数,0表示不自动重试',
44
44
  defaultValue: 0
45
45
  },
46
+ timeout: {
47
+ type: DataTypes.INTEGER,
48
+ comment: '任务超时时间(分钟),0表示不超时,默认60分钟',
49
+ defaultValue: 60
50
+ },
46
51
  startTime: {
47
52
  type: DataTypes.DATE,
48
53
  comment: '任务最早执行时间',
@@ -32,35 +32,26 @@ module.exports = fp(async (fastify, options) => {
32
32
  hasTiming = totalTime > 0;
33
33
  }
34
34
 
35
- const channel = `task:${task.type}:${task.runnerType || 'manual'}`;
35
+ const completedHour = new Date(completedAt).getHours();
36
+ const channel = `${task.type}:${task.runnerType || 'manual'}:${completedHour}`;
36
37
  const status = task.status || 'unknown';
37
38
 
38
- // 分两次采集:计数指标和时长指标,避免聚合时不同语义的值混在一起
39
- const countData = { total: 1, [status]: 1 };
39
+ const data = { total: 1, [status]: 1 };
40
+ const unit = { total: 'count', [status]: 'count' };
41
+ if (hasTiming) {
42
+ Object.assign(data, { waitingTime, executionTime, totalTime });
43
+ Object.assign(unit, { waitingTime: 'ms', executionTime: 'ms', totalTime: 'ms' });
44
+ }
40
45
  fastify[`${options.name}Statistics`].services
41
46
  .collect({
42
47
  channel,
43
- data: countData,
44
- unit: 'count',
48
+ data,
49
+ unit,
45
50
  time: completedAt
46
51
  })
47
52
  .catch(e => {
48
53
  fastify.log.error(`采集任务统计数据失败: ${e.message}`);
49
54
  });
50
-
51
- if (hasTiming) {
52
- const timingData = { waitingTime, executionTime, totalTime };
53
- fastify[`${options.name}Statistics`].services
54
- .collect({
55
- channel,
56
- data: timingData,
57
- unit: 'ms',
58
- time: completedAt
59
- })
60
- .catch(e => {
61
- fastify.log.error(`采集任务统计数据失败: ${e.message}`);
62
- });
63
- }
64
55
  };
65
56
 
66
57
  const generateSignature = ({ secret, id, data }) => {
@@ -76,7 +67,7 @@ module.exports = fp(async (fastify, options) => {
76
67
  return signature === expectedSignature;
77
68
  };
78
69
 
79
- const create = async ({ userId, input, type, targetId, targetType, runnerType, delay = 0, scriptName, priority = 0, parentTaskId, maxRetries = 0, options: currentOptions }) => {
70
+ const create = async ({ userId, input, type, targetId, targetType, runnerType, delay = 0, scriptName, priority = 0, parentTaskId, maxRetries = 0, timeout = 60, options: currentOptions }) => {
80
71
  if (typeof delay !== 'number' || delay < 0) {
81
72
  throw new Error('delay 必须为非负数');
82
73
  }
@@ -86,10 +77,13 @@ module.exports = fp(async (fastify, options) => {
86
77
  if (typeof maxRetries !== 'number' || !Number.isInteger(maxRetries) || maxRetries < 0) {
87
78
  throw new Error('maxRetries 必须为非负整数');
88
79
  }
80
+ if (typeof timeout !== 'number' || !Number.isInteger(timeout) || timeout < 0) {
81
+ throw new Error('timeout 必须为非负整数');
82
+ }
89
83
  if (typeof options.task[type] !== 'function') {
90
84
  throw new Error('未找到合法的任务声明');
91
85
  }
92
- return await models.task.create({
86
+ const task = await models.task.create({
93
87
  userId,
94
88
  input,
95
89
  type,
@@ -99,22 +93,23 @@ module.exports = fp(async (fastify, options) => {
99
93
  priority,
100
94
  parentTaskId,
101
95
  maxRetries,
96
+ timeout,
102
97
  startTime: delay > 0 ? new Date(Date.now() + 1000 * delay) : new Date(),
103
98
  scriptName,
104
99
  options: currentOptions
105
100
  });
101
+ return task;
106
102
  };
107
103
 
108
104
  const resetAll = async () => {
105
+ const runningTasks = await models.task.findAll({
106
+ where: { status: 'running' },
107
+ attributes: ['type', 'runnerType'],
108
+ raw: true
109
+ });
109
110
  await models.task.update(
110
- {
111
- status: 'pending'
112
- },
113
- {
114
- where: {
115
- status: 'running'
116
- }
117
- }
111
+ { status: 'pending' },
112
+ { where: { status: 'running' } }
118
113
  );
119
114
  };
120
115
 
@@ -317,7 +312,42 @@ module.exports = fp(async (fastify, options) => {
317
312
  }
318
313
  };
319
314
 
315
+ /** 检查超时任务,将超时的 running/waiting 任务标记为 failed */
316
+ const checkTimeout = async () => {
317
+ const now = new Date();
318
+ const timedOutTasks = await models.task.findAll({
319
+ where: {
320
+ status: { [Op.in]: ['running', 'waiting'] },
321
+ timeout: { [Op.gt]: 0 },
322
+ startedAt: { [Op.ne]: null }
323
+ }
324
+ });
325
+
326
+ const tasksToFail = timedOutTasks.filter(task => {
327
+ const elapsed = now.getTime() - new Date(task.startedAt).getTime();
328
+ return elapsed > task.timeout * 60 * 1000;
329
+ });
330
+
331
+ if (tasksToFail.length === 0) return;
332
+
333
+ fastify.log.info(`检测到 ${tasksToFail.length} 个超时任务`);
334
+
335
+ for (const task of tasksToFail) {
336
+ const elapsed = now.getTime() - new Date(task.startedAt).getTime();
337
+ const prevStatus = task.status;
338
+ await task.update({
339
+ status: 'failed',
340
+ error: `任务超时:已执行 ${Math.round(elapsed / 60000)} 分钟,超过设定的 ${task.timeout} 分钟超时时间`,
341
+ completedAt: now
342
+ });
343
+ collectTaskStatistics(task);
344
+ }
345
+ };
346
+
320
347
  const runner = async () => {
348
+ // 检查超时任务
349
+ await checkTimeout();
350
+
321
351
  const runningTaskCount = await models.task.count({
322
352
  where: {
323
353
  runnerType: 'system',
@@ -404,6 +434,7 @@ module.exports = fp(async (fastify, options) => {
404
434
 
405
435
  const complete = async ({ id, userId, output, ...props }) => {
406
436
  const task = await detail({ id });
437
+ const prevStatus = task.status;
407
438
  if (props.status === 'success') {
408
439
  try {
409
440
  await options.task[task.type]({ task, result: output });
@@ -443,14 +474,28 @@ module.exports = fp(async (fastify, options) => {
443
474
  };
444
475
 
445
476
  const waitingComplete = async ({ id, pollInterval = 1000, maxPollTimes = 20 }) => {
446
- const task = await detail({ id });
477
+ let task = await detail({ id });
447
478
 
448
479
  if (task.status === 'pending') {
449
480
  // 标记为最高优先级,确保立即执行(即使因重试等重新入队也会被优先处理)
450
481
  await task.update({ priority: Number.MAX_SAFE_INTEGER });
451
482
  await processSystemTask(task);
483
+ // processSystemTask 完成后重新加载任务状态
484
+ await task.reload();
485
+ }
486
+
487
+ // 立即检查是否已到达终态
488
+ if (task.status === 'success') {
489
+ return task.output;
490
+ }
491
+ if (task.status === 'failed') {
492
+ throw new Error(task.error || '任务失败');
493
+ }
494
+ if (task.status === 'canceled') {
495
+ throw new Error(task.error || '任务取消');
452
496
  }
453
497
 
498
+ // 任务仍在运行中,开始轮询
454
499
  let pollCount = 0;
455
500
  return await new Promise((resolve, reject) => {
456
501
  const executePolling = async () => {