@kne/fastify-task 3.0.0-alpha.0 → 3.0.0-alpha.2
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/libs/services/main.js +26 -12
- package/package.json +2 -2
package/libs/services/main.js
CHANGED
|
@@ -8,7 +8,11 @@ module.exports = fp(async (fastify, options) => {
|
|
|
8
8
|
const { Op } = fastify.sequelize.Sequelize;
|
|
9
9
|
|
|
10
10
|
/** 通过 @kne/fastify-statistics 采集任务完成数据 */
|
|
11
|
-
const collectTaskStatistics = task => {
|
|
11
|
+
const collectTaskStatistics = async task => {
|
|
12
|
+
// 确保 task 数据是最新的(Sequelize update 后实例可能未完全同步)
|
|
13
|
+
if (task.reload && typeof task.reload === 'function') {
|
|
14
|
+
await task.reload();
|
|
15
|
+
}
|
|
12
16
|
const completedAt = task.completedAt || new Date();
|
|
13
17
|
const createdAt = task.createdAt;
|
|
14
18
|
const startedAt = task.startedAt;
|
|
@@ -28,25 +32,35 @@ module.exports = fp(async (fastify, options) => {
|
|
|
28
32
|
hasTiming = totalTime > 0;
|
|
29
33
|
}
|
|
30
34
|
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
[task.status]: 1
|
|
34
|
-
};
|
|
35
|
-
if (hasTiming) {
|
|
36
|
-
data.waitingTime = waitingTime;
|
|
37
|
-
data.executionTime = executionTime;
|
|
38
|
-
data.totalTime = totalTime;
|
|
39
|
-
}
|
|
35
|
+
const channel = `task:${task.type}:${task.runnerType || 'manual'}`;
|
|
36
|
+
const status = task.status || 'unknown';
|
|
40
37
|
|
|
38
|
+
// 分两次采集:计数指标和时长指标,避免聚合时不同语义的值混在一起
|
|
39
|
+
const countData = { total: 1, [status]: 1 };
|
|
41
40
|
fastify[`${options.name}Statistics`].services
|
|
42
41
|
.collect({
|
|
43
|
-
channel
|
|
44
|
-
data,
|
|
42
|
+
channel,
|
|
43
|
+
data: countData,
|
|
44
|
+
unit: 'count',
|
|
45
45
|
time: completedAt
|
|
46
46
|
})
|
|
47
47
|
.catch(e => {
|
|
48
48
|
fastify.log.error(`采集任务统计数据失败: ${e.message}`);
|
|
49
49
|
});
|
|
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
|
+
}
|
|
50
64
|
};
|
|
51
65
|
|
|
52
66
|
const generateSignature = ({ secret, id, data }) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kne/fastify-task",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.2",
|
|
4
4
|
"description": "一个 Fastify 插件,用于任务编排、追踪任务执行状态和结果。支持系统自动执行和手动执行两种模式,提供完整的任务生命周期管理",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
"sqlite3": "^5.1.7"
|
|
57
57
|
},
|
|
58
58
|
"dependencies": {
|
|
59
|
-
"@kne/fastify-statistics": "^0.1.0-alpha.
|
|
59
|
+
"@kne/fastify-statistics": "^0.1.0-alpha.5",
|
|
60
60
|
"fastify-cron": "^1.4.0",
|
|
61
61
|
"fs-extra": "^11.3.0"
|
|
62
62
|
}
|