@kne/fastify-task 2.0.5 → 3.0.0-alpha.0
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/index.js +17 -14
- package/libs/controllers/main.js +76 -1
- package/libs/controllers/statistics.js +12 -152
- package/libs/models/task.js +34 -3
- package/libs/services/main.js +203 -90
- package/libs/services/statistics.js +63 -966
- package/package.json +2 -3
- package/libs/models/task-daily-statistics.js +0 -66
- package/libs/models/task-hourly-statistics.js +0 -48
package/index.js
CHANGED
|
@@ -11,12 +11,16 @@ module.exports = fp(
|
|
|
11
11
|
name: 'task',
|
|
12
12
|
limit: 10,
|
|
13
13
|
dir: path.resolve(process.cwd(), 'libs', 'tasks'),
|
|
14
|
+
/** 任务目录列表,运行时可通过 addDir 动态添加 */
|
|
15
|
+
dirs: null,
|
|
14
16
|
cronTime: '*/10 * * * *',
|
|
15
17
|
scriptName: 'index',
|
|
16
18
|
maxPollTimes: 20,
|
|
17
19
|
pollInterval: 10000,
|
|
18
|
-
/**
|
|
19
|
-
|
|
20
|
+
/** 任务执行超时时间(毫秒),0 表示不超时,默认 30 分钟 */
|
|
21
|
+
taskTimeout: 30 * 60 * 1000,
|
|
22
|
+
/** 重试基础延迟(毫秒),实际延迟 = retryBaseDelay * 2^(retryCount-1) */
|
|
23
|
+
retryBaseDelay: 5000,
|
|
20
24
|
getUserModel: () => {
|
|
21
25
|
return fastify.account.models.user;
|
|
22
26
|
},
|
|
@@ -28,7 +32,17 @@ module.exports = fp(
|
|
|
28
32
|
options
|
|
29
33
|
);
|
|
30
34
|
|
|
31
|
-
|
|
35
|
+
// 初始化 dirs:优先使用用户传入的 dirs,否则以 dir 为默认值,保证向后兼容
|
|
36
|
+
if (!options.dirs) {
|
|
37
|
+
options.dirs = [options.dir];
|
|
38
|
+
} else if (!options.dirs.includes(options.dir)) {
|
|
39
|
+
options.dirs = [options.dir, ...options.dirs];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
fastify.register(require('@kne/fastify-statistics'), {
|
|
43
|
+
dbTableNamePrefix: options.dbTableNamePrefix,
|
|
44
|
+
name: `${options.name}Statistics`
|
|
45
|
+
});
|
|
32
46
|
|
|
33
47
|
fastify.register(require('@kne/fastify-namespace'), {
|
|
34
48
|
options,
|
|
@@ -61,17 +75,6 @@ module.exports = fp(
|
|
|
61
75
|
//启动时,将running状态的任务设置为pending
|
|
62
76
|
fastify.addHook('onReady', async () => {
|
|
63
77
|
await fastify[options.name].services.resetAll();
|
|
64
|
-
fastify[options.name].services.syncHourlyStatisticsFromTasks().catch(console.error);
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
if (options.hourlyStatisticsCronTime) {
|
|
68
|
-
fastify.cron.createJob({
|
|
69
|
-
cronTime: options.hourlyStatisticsCronTime,
|
|
70
|
-
onTick: async () => {
|
|
71
|
-
const { syncHourlyStatisticsFromTasks } = fastify[options.name].services;
|
|
72
|
-
await syncHourlyStatisticsFromTasks();
|
|
73
|
-
},
|
|
74
|
-
start: true
|
|
75
78
|
});
|
|
76
79
|
}
|
|
77
80
|
})
|
package/libs/controllers/main.js
CHANGED
|
@@ -2,6 +2,68 @@ const fp = require('fastify-plugin');
|
|
|
2
2
|
|
|
3
3
|
module.exports = fp(async (fastify, options) => {
|
|
4
4
|
const { services } = fastify[options.name];
|
|
5
|
+
// #14: 新增创建任务的 REST 接口
|
|
6
|
+
fastify.post(
|
|
7
|
+
`${options.prefix}/create`,
|
|
8
|
+
{
|
|
9
|
+
onRequest: options.getAuthenticate('write'),
|
|
10
|
+
schema: {
|
|
11
|
+
summary: '创建任务',
|
|
12
|
+
body: {
|
|
13
|
+
type: 'object',
|
|
14
|
+
properties: {
|
|
15
|
+
type: {
|
|
16
|
+
type: 'string',
|
|
17
|
+
description: '任务类型'
|
|
18
|
+
},
|
|
19
|
+
targetId: {
|
|
20
|
+
type: 'string',
|
|
21
|
+
description: '目标对象ID'
|
|
22
|
+
},
|
|
23
|
+
targetType: {
|
|
24
|
+
type: 'string',
|
|
25
|
+
description: '目标对象类型'
|
|
26
|
+
},
|
|
27
|
+
input: {
|
|
28
|
+
type: 'object',
|
|
29
|
+
description: '输入数据'
|
|
30
|
+
},
|
|
31
|
+
runnerType: {
|
|
32
|
+
type: 'string',
|
|
33
|
+
enum: ['manual', 'system'],
|
|
34
|
+
description: '执行者类型'
|
|
35
|
+
},
|
|
36
|
+
delay: {
|
|
37
|
+
type: 'number',
|
|
38
|
+
description: '延迟执行秒数'
|
|
39
|
+
},
|
|
40
|
+
scriptName: {
|
|
41
|
+
type: 'string',
|
|
42
|
+
description: '任务脚本名称'
|
|
43
|
+
},
|
|
44
|
+
priority: {
|
|
45
|
+
type: 'number',
|
|
46
|
+
description: '优先级,数值越大越优先,默认0'
|
|
47
|
+
},
|
|
48
|
+
parentTaskId: {
|
|
49
|
+
type: 'string',
|
|
50
|
+
description: '父任务ID,用于任务依赖'
|
|
51
|
+
},
|
|
52
|
+
maxRetries: {
|
|
53
|
+
type: 'number',
|
|
54
|
+
description: '最大自动重试次数,默认0'
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
required: ['type', 'targetId', 'targetType']
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
async request => {
|
|
62
|
+
const task = await services.create(Object.assign({}, request.body, { userId: request.userInfo?.id }));
|
|
63
|
+
return { id: task.id };
|
|
64
|
+
}
|
|
65
|
+
);
|
|
66
|
+
|
|
5
67
|
fastify.get(
|
|
6
68
|
`${options.prefix}/list`,
|
|
7
69
|
{
|
|
@@ -125,7 +187,20 @@ module.exports = fp(async (fastify, options) => {
|
|
|
125
187
|
type: 'object',
|
|
126
188
|
properties: {
|
|
127
189
|
id: {
|
|
128
|
-
type: 'string'
|
|
190
|
+
type: 'string',
|
|
191
|
+
description: '任务ID'
|
|
192
|
+
},
|
|
193
|
+
targetId: {
|
|
194
|
+
type: 'string',
|
|
195
|
+
description: '目标对象ID(批量取消时使用)'
|
|
196
|
+
},
|
|
197
|
+
targetType: {
|
|
198
|
+
type: 'string',
|
|
199
|
+
description: '目标对象类型(批量取消时使用)'
|
|
200
|
+
},
|
|
201
|
+
type: {
|
|
202
|
+
type: 'string',
|
|
203
|
+
description: '任务类型(批量取消时使用)'
|
|
129
204
|
}
|
|
130
205
|
}
|
|
131
206
|
}
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
const fp = require('fastify-plugin');
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
async (fastify, options) => {
|
|
6
|
-
const { services } = fastify[options.name];
|
|
3
|
+
module.exports = fp(async (fastify, options) => {
|
|
4
|
+
const { services } = fastify[options.name];
|
|
7
5
|
|
|
8
6
|
fastify.get(
|
|
9
7
|
`${options.prefix}/statistics`,
|
|
@@ -14,142 +12,17 @@ module.exports = fp(
|
|
|
14
12
|
query: {
|
|
15
13
|
type: 'object',
|
|
16
14
|
properties: {
|
|
17
|
-
range: { type: 'string', default: '7d', description: '时间范围: 7d=近7天, 1m=近1个月, 1y=近1年' },
|
|
15
|
+
range: { type: 'string', default: '7d', description: '时间范围: 7d=近7天, 1m=近1个月, 3m=近3个月, 1y=近1年' },
|
|
18
16
|
timezone: { type: 'string', description: '时区,如 Asia/Shanghai,默认服务器时区' },
|
|
19
17
|
type: { type: 'string', description: '按任务类型筛选' },
|
|
20
18
|
runnerType: { type: 'string', description: '按执行方式筛选 manual / system' }
|
|
21
19
|
}
|
|
22
|
-
},
|
|
23
|
-
response: {
|
|
24
|
-
200: {
|
|
25
|
-
type: 'object',
|
|
26
|
-
properties: {
|
|
27
|
-
range: { type: 'string', description: '当前时间范围' },
|
|
28
|
-
rangeLabel: { type: 'string', description: '时间范围描述' },
|
|
29
|
-
totalTasks: { type: 'integer', description: '时间范围内任务总数' },
|
|
30
|
-
byStatus: {
|
|
31
|
-
type: 'object',
|
|
32
|
-
description: '按任务状态统计',
|
|
33
|
-
additionalProperties: { type: 'integer' }
|
|
34
|
-
},
|
|
35
|
-
byType: {
|
|
36
|
-
type: 'object',
|
|
37
|
-
description: '按任务类型统计',
|
|
38
|
-
additionalProperties: { type: 'integer' }
|
|
39
|
-
},
|
|
40
|
-
byRunnerType: {
|
|
41
|
-
type: 'object',
|
|
42
|
-
description: '按执行者类型统计(manual/system)',
|
|
43
|
-
additionalProperties: { type: 'integer' }
|
|
44
|
-
},
|
|
45
|
-
byTargetType: {
|
|
46
|
-
type: 'object',
|
|
47
|
-
description: '按目标对象类型统计',
|
|
48
|
-
additionalProperties: { type: 'integer' }
|
|
49
|
-
},
|
|
50
|
-
recentTrend: {
|
|
51
|
-
type: 'array',
|
|
52
|
-
description: '按日任务创建趋势',
|
|
53
|
-
items: {
|
|
54
|
-
type: 'object',
|
|
55
|
-
properties: {
|
|
56
|
-
date: { type: 'string', description: '日期' },
|
|
57
|
-
count: { type: 'integer', description: '任务数量' }
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
},
|
|
61
|
-
recentTrendByStatus: {
|
|
62
|
-
type: 'array',
|
|
63
|
-
description: '按日+状态任务创建趋势',
|
|
64
|
-
items: {
|
|
65
|
-
type: 'object',
|
|
66
|
-
properties: {
|
|
67
|
-
date: { type: 'string', description: '日期' },
|
|
68
|
-
status: { type: 'string', description: '任务状态' },
|
|
69
|
-
count: { type: 'integer', description: '任务数量' }
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
},
|
|
73
|
-
recentTrendByType: {
|
|
74
|
-
type: 'array',
|
|
75
|
-
description: '按日+类型任务创建趋势',
|
|
76
|
-
items: {
|
|
77
|
-
type: 'object',
|
|
78
|
-
properties: {
|
|
79
|
-
date: { type: 'string', description: '日期' },
|
|
80
|
-
type: { type: 'string', description: '任务类型' },
|
|
81
|
-
count: { type: 'integer', description: '任务数量' }
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
},
|
|
85
|
-
durationTrend: {
|
|
86
|
-
type: 'array',
|
|
87
|
-
description: '按日平均耗时趋势(来自预聚合统计表)',
|
|
88
|
-
items: {
|
|
89
|
-
type: 'object',
|
|
90
|
-
properties: {
|
|
91
|
-
date: { type: 'string', description: '完成日期' },
|
|
92
|
-
completedCount: { type: 'integer', description: '完成任务数' },
|
|
93
|
-
successCount: { type: 'integer', description: '成功数' },
|
|
94
|
-
failedCount: { type: 'integer', description: '失败数' },
|
|
95
|
-
canceledCount: { type: 'integer', description: '取消数' },
|
|
96
|
-
avgWaitingTime: { type: 'integer', description: '平均等待时间(毫秒)' },
|
|
97
|
-
avgExecutionTime: { type: 'integer', description: '平均执行时间(毫秒)' },
|
|
98
|
-
avgTotalTime: { type: 'integer', description: '平均总耗时(毫秒)' },
|
|
99
|
-
byType: {
|
|
100
|
-
type: 'object',
|
|
101
|
-
description: '按类型分组的耗时统计',
|
|
102
|
-
additionalProperties: {
|
|
103
|
-
type: 'object',
|
|
104
|
-
properties: {
|
|
105
|
-
count: { type: 'integer' },
|
|
106
|
-
avgWaitingTime: { type: 'integer' },
|
|
107
|
-
avgExecutionTime: { type: 'integer' },
|
|
108
|
-
avgTotalTime: { type: 'integer' }
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
},
|
|
112
|
-
byRunnerType: {
|
|
113
|
-
type: 'object',
|
|
114
|
-
description: '按执行者类型分组的耗时统计',
|
|
115
|
-
additionalProperties: {
|
|
116
|
-
type: 'object',
|
|
117
|
-
properties: {
|
|
118
|
-
count: { type: 'integer' },
|
|
119
|
-
avgWaitingTime: { type: 'integer' },
|
|
120
|
-
avgExecutionTime: { type: 'integer' },
|
|
121
|
-
avgTotalTime: { type: 'integer' }
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
},
|
|
128
|
-
hourlyCompletionTrend: {
|
|
129
|
-
type: 'array',
|
|
130
|
-
description: '按完成时刻归入 UTC 小时桶后,在请求 timezone 下展开的每小时+类型+执行方式完成数',
|
|
131
|
-
items: {
|
|
132
|
-
type: 'object',
|
|
133
|
-
properties: {
|
|
134
|
-
date: { type: 'string', description: '客户端时区下的日期 YYYY-MM-DD' },
|
|
135
|
-
hour: { type: 'integer', description: '客户端时区下的小时 0-23' },
|
|
136
|
-
type: { type: 'string' },
|
|
137
|
-
runnerType: { type: 'string' },
|
|
138
|
-
totalCompleted: { type: 'integer' },
|
|
139
|
-
successCount: { type: 'integer' },
|
|
140
|
-
failedCount: { type: 'integer' },
|
|
141
|
-
canceledCount: { type: 'integer' }
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
20
|
}
|
|
148
21
|
}
|
|
149
22
|
},
|
|
150
23
|
async request => {
|
|
151
24
|
const { range = '7d', timezone, type, runnerType } = request.query;
|
|
152
|
-
return await services.
|
|
25
|
+
return await services.queryStatistics({ range, timezone, type, runnerType });
|
|
153
26
|
}
|
|
154
27
|
);
|
|
155
28
|
|
|
@@ -157,37 +30,24 @@ module.exports = fp(
|
|
|
157
30
|
fastify.get(
|
|
158
31
|
`${options.prefix}/statistics/sse`,
|
|
159
32
|
{
|
|
160
|
-
sse: true,
|
|
161
33
|
onRequest: options.getAuthenticate('statistics'),
|
|
162
34
|
schema: {
|
|
163
|
-
summary: 'SSE
|
|
35
|
+
summary: 'SSE实时推送任务统计数据',
|
|
164
36
|
query: {
|
|
165
37
|
type: 'object',
|
|
166
38
|
properties: {
|
|
167
|
-
|
|
39
|
+
range: { type: 'string', default: '7d', description: '时间范围: 7d=近7天, 1m=近1个月, 3m=近3个月, 1y=近1年' },
|
|
168
40
|
timezone: { type: 'string', description: '时区,如 Asia/Shanghai,默认服务器时区' },
|
|
169
41
|
type: { type: 'string', description: '按任务类型筛选' },
|
|
170
|
-
runnerType: { type: 'string', description: '按执行方式筛选 manual / system' }
|
|
42
|
+
runnerType: { type: 'string', description: '按执行方式筛选 manual / system' },
|
|
43
|
+
interval: { type: 'integer', minimum: 1, default: 5, description: '推送间隔时间(秒),最小1秒,默认5秒' }
|
|
171
44
|
}
|
|
172
45
|
}
|
|
173
46
|
}
|
|
174
47
|
},
|
|
175
|
-
async
|
|
176
|
-
const
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
async function* eventStream() {
|
|
180
|
-
while (reply.sse.isConnected) {
|
|
181
|
-
try {
|
|
182
|
-
yield { data: JSON.stringify(await services.statistics.getRealtime({ timezone, type, runnerType })) };
|
|
183
|
-
} catch (err) {
|
|
184
|
-
yield { event: 'error', data: JSON.stringify({ message: err.message }) };
|
|
185
|
-
}
|
|
186
|
-
await new Promise(resolve => setTimeout(resolve, intervalSeconds * 1000));
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
await reply.sse.send(eventStream());
|
|
48
|
+
async (request, reply) => {
|
|
49
|
+
const { range = '7d', timezone, type, runnerType, interval } = request.query;
|
|
50
|
+
await services.sseStatistics({ range, timezone, type, runnerType, interval }, reply);
|
|
191
51
|
}
|
|
192
52
|
);
|
|
193
|
-
|
|
53
|
+
});
|
package/libs/models/task.js
CHANGED
|
@@ -24,6 +24,25 @@ module.exports = ({ DataTypes, definePrimaryType, options }) => {
|
|
|
24
24
|
defaultValue: 'manual',
|
|
25
25
|
allowNull: false
|
|
26
26
|
},
|
|
27
|
+
priority: {
|
|
28
|
+
type: DataTypes.INTEGER,
|
|
29
|
+
comment: '任务优先级,数值越大越优先',
|
|
30
|
+
defaultValue: 0
|
|
31
|
+
},
|
|
32
|
+
parentTaskId: definePrimaryType('parentTaskId', {
|
|
33
|
+
comment: '父任务ID,用于任务依赖/链式执行',
|
|
34
|
+
allowNull: true
|
|
35
|
+
}),
|
|
36
|
+
retryCount: {
|
|
37
|
+
type: DataTypes.INTEGER,
|
|
38
|
+
comment: '已重试次数',
|
|
39
|
+
defaultValue: 0
|
|
40
|
+
},
|
|
41
|
+
maxRetries: {
|
|
42
|
+
type: DataTypes.INTEGER,
|
|
43
|
+
comment: '最大重试次数,0表示不自动重试',
|
|
44
|
+
defaultValue: 0
|
|
45
|
+
},
|
|
27
46
|
startTime: {
|
|
28
47
|
type: DataTypes.DATE,
|
|
29
48
|
comment: '任务最早执行时间',
|
|
@@ -37,13 +56,18 @@ module.exports = ({ DataTypes, definePrimaryType, options }) => {
|
|
|
37
56
|
type: DataTypes.DATE,
|
|
38
57
|
comment: '任务完成时间'
|
|
39
58
|
},
|
|
59
|
+
completedUserId: definePrimaryType('completedUserId', {
|
|
60
|
+
comment: '完成任务的用户ID'
|
|
61
|
+
}),
|
|
40
62
|
input: {
|
|
41
63
|
type: DataTypes.JSON,
|
|
42
|
-
comment: '输入数据'
|
|
64
|
+
comment: '输入数据',
|
|
65
|
+
defaultValue: null
|
|
43
66
|
},
|
|
44
67
|
output: {
|
|
45
68
|
type: DataTypes.JSON,
|
|
46
|
-
comment: '输出数据'
|
|
69
|
+
comment: '输出数据',
|
|
70
|
+
defaultValue: null
|
|
47
71
|
},
|
|
48
72
|
error: {
|
|
49
73
|
type: DataTypes.JSON,
|
|
@@ -94,6 +118,11 @@ module.exports = ({ DataTypes, definePrimaryType, options }) => {
|
|
|
94
118
|
as: 'completedUser',
|
|
95
119
|
comment: '任务完成人'
|
|
96
120
|
});
|
|
121
|
+
task.belongsTo(task, {
|
|
122
|
+
foreignKey: 'parentTaskId',
|
|
123
|
+
as: 'parentTask',
|
|
124
|
+
comment: '父任务'
|
|
125
|
+
});
|
|
97
126
|
},
|
|
98
127
|
options: {
|
|
99
128
|
comment: '任务',
|
|
@@ -104,7 +133,9 @@ module.exports = ({ DataTypes, definePrimaryType, options }) => {
|
|
|
104
133
|
{ fields: ['runner_type'] },
|
|
105
134
|
{ fields: ['type', 'status'] },
|
|
106
135
|
{ fields: ['runner_type', 'status'] },
|
|
107
|
-
{ fields: ['target_id', 'target_type', 'type'] }
|
|
136
|
+
{ fields: ['target_id', 'target_type', 'type'] },
|
|
137
|
+
{ fields: ['parent_task_id'] },
|
|
138
|
+
{ fields: ['priority'] }
|
|
108
139
|
]
|
|
109
140
|
}
|
|
110
141
|
};
|