@kne/fastify-task 3.0.0-alpha.7 → 3.0.0-alpha.8

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.
@@ -0,0 +1,230 @@
1
+ const fp = require('fastify-plugin');
2
+ const getTaskServiceContext = require('../helpers/task-service-context');
3
+
4
+ module.exports = fp(async (fastify, options) => {
5
+ const context = getTaskServiceContext(fastify, options);
6
+ const { models, Op } = context;
7
+
8
+ const getRunningRecoveryBefore = () => {
9
+ const recoverAfter = Number.isInteger(options.recoverRunningTaskAfter) && options.recoverRunningTaskAfter >= 0 ? options.recoverRunningTaskAfter : 30 * 60 * 1000;
10
+ return new Date(Date.now() - recoverAfter);
11
+ };
12
+
13
+ const getRunningResetWhere = ({ staleOnly = false, before } = {}) => {
14
+ const where = { status: 'running' };
15
+ if (staleOnly) {
16
+ where[Op.or] = [{ startedAt: { [Op.lte]: before || getRunningRecoveryBefore() } }, { startedAt: null }];
17
+ }
18
+ return where;
19
+ };
20
+
21
+ const resetAll = async (props = {}) => {
22
+ const [affectedCount] = await models.task.update({ status: 'pending' }, { where: getRunningResetWhere(props) });
23
+ return affectedCount;
24
+ };
25
+
26
+ const claimTask = async (task, { allowChild = false } = {}) => {
27
+ const now = new Date();
28
+ const [affectedCount] = await models.task.update(
29
+ {
30
+ status: 'running',
31
+ startedAt: now,
32
+ progress: 0,
33
+ error: null,
34
+ output: null,
35
+ pollCount: 0,
36
+ pollResults: [],
37
+ completedAt: null
38
+ },
39
+ {
40
+ where: Object.assign(
41
+ {
42
+ id: task.id,
43
+ runnerType: 'system',
44
+ status: 'pending',
45
+ startTime: {
46
+ [Op.lte]: now
47
+ }
48
+ },
49
+ allowChild ? {} : { parentTaskId: null }
50
+ )
51
+ }
52
+ );
53
+ if (affectedCount !== 1) return null;
54
+ return await models.task.findByPk(task.id);
55
+ };
56
+
57
+ const processSystemTask = async (task, { claimed = false } = {}) => {
58
+ if (!claimed) {
59
+ task = await claimTask(task, { allowChild: true });
60
+ if (!task) return false;
61
+ }
62
+ const addLog = props => {
63
+ return fastify[options.name].services.log(Object.assign({}, props, { taskId: task.id }));
64
+ };
65
+
66
+ const createTimeout = ms => new Promise((_, reject) => setTimeout(() => reject(new Error(`任务执行超时(${ms}ms)`)), ms));
67
+
68
+ const executorPromise = context.executor({ type: task.type, scriptName: task.scriptName, task, log: addLog });
69
+ const racePromise = options.taskTimeout > 0 ? Promise.race([executorPromise, createTimeout(options.taskTimeout)]) : executorPromise;
70
+
71
+ return racePromise
72
+ .then(async result => {
73
+ if (result === false) {
74
+ return;
75
+ }
76
+ await context.finalizeSuccess({ task, output: result });
77
+ })
78
+ .catch(async e => {
79
+ const currentRetryCount = (task.retryCount || 0) + 1;
80
+ const maxRetries = task.maxRetries || 0;
81
+ if (currentRetryCount <= maxRetries) {
82
+ const baseDelay = options.retryBaseDelay || 5000;
83
+ const backoffDelay = baseDelay * Math.pow(2, currentRetryCount - 1);
84
+ const retryScheduled = await context.updateTaskByStatus({
85
+ task,
86
+ allowedStatuses: ['running'],
87
+ updateData: {
88
+ status: 'pending',
89
+ retryCount: currentRetryCount,
90
+ error: (e.stack || '').replaceAll(process.cwd(), '/server'),
91
+ startTime: new Date(Date.now() + backoffDelay),
92
+ completedAt: null
93
+ }
94
+ });
95
+ if (retryScheduled) {
96
+ fastify.log.info(`任务 ${task.id} 第 ${currentRetryCount} 次重试,${backoffDelay}ms 后执行`);
97
+ }
98
+ } else {
99
+ await context.failTask({ task, error: e, updateData: { retryCount: currentRetryCount } });
100
+ }
101
+ });
102
+ };
103
+
104
+ const triggerChildTasks = async parentTask => {
105
+ const childTasks = await models.task.findAll({
106
+ where: {
107
+ parentTaskId: parentTask.id,
108
+ status: 'pending'
109
+ }
110
+ });
111
+ if (childTasks.length === 0) return;
112
+
113
+ fastify.log.info(`父任务 ${parentTask.id} 完成,激活 ${childTasks.length} 个子任务`);
114
+ for (const child of childTasks) {
115
+ if (child.runnerType === 'system') {
116
+ await processSystemTask(child);
117
+ }
118
+ }
119
+ };
120
+
121
+ const checkTimeout = async () => {
122
+ const now = new Date();
123
+ const timedOutTasks = await models.task.findAll({
124
+ where: {
125
+ status: { [Op.in]: ['running', 'waiting'] },
126
+ timeout: { [Op.gt]: 0 },
127
+ startedAt: { [Op.ne]: null }
128
+ }
129
+ });
130
+
131
+ const tasksToFail = timedOutTasks.filter(task => {
132
+ const elapsed = now.getTime() - new Date(task.startedAt).getTime();
133
+ return elapsed > task.timeout;
134
+ });
135
+
136
+ if (tasksToFail.length === 0) return;
137
+
138
+ fastify.log.info(`检测到 ${tasksToFail.length} 个超时任务`);
139
+
140
+ for (const task of tasksToFail) {
141
+ const elapsed = now.getTime() - new Date(task.startedAt).getTime();
142
+ await context.failTask({ task, error: `任务超时:已执行 ${elapsed}ms,超过设定的 ${task.timeout}ms 超时时间`, updateData: { completedAt: now } });
143
+ }
144
+ };
145
+
146
+ const claimPendingTasks = async limit => {
147
+ if (limit <= 0) return [];
148
+ const now = new Date();
149
+ const pendingTasks = await models.task.findAll({
150
+ where: {
151
+ runnerType: 'system',
152
+ status: 'pending',
153
+ parentTaskId: null,
154
+ startTime: {
155
+ [Op.lte]: now
156
+ }
157
+ },
158
+ order: [
159
+ ['priority', 'DESC'],
160
+ ['startTime', 'ASC']
161
+ ],
162
+ limit: limit
163
+ });
164
+
165
+ const claimedTasks = [];
166
+ for (const task of pendingTasks) {
167
+ const [affectedCount] = await models.task.update(
168
+ {
169
+ status: 'running',
170
+ startedAt: now,
171
+ progress: 0,
172
+ error: null,
173
+ output: null,
174
+ pollCount: 0,
175
+ pollResults: [],
176
+ completedAt: null
177
+ },
178
+ {
179
+ where: {
180
+ id: task.id,
181
+ runnerType: 'system',
182
+ status: 'pending',
183
+ parentTaskId: null,
184
+ startTime: {
185
+ [Op.lte]: now
186
+ }
187
+ }
188
+ }
189
+ );
190
+ if (affectedCount === 1) {
191
+ const claimedTask = await models.task.findByPk(task.id);
192
+ if (claimedTask) {
193
+ claimedTasks.push(claimedTask);
194
+ }
195
+ }
196
+ }
197
+ return claimedTasks;
198
+ };
199
+
200
+ const runner = async () => {
201
+ await checkTimeout();
202
+
203
+ const runningTaskCount = await models.task.count({
204
+ where: {
205
+ runnerType: 'system',
206
+ status: 'running'
207
+ }
208
+ });
209
+ const limit = options.limit - runningTaskCount;
210
+ if (limit <= 0) {
211
+ fastify.log.info(`当前运行中的系统任务数(${runningTaskCount})已达上限(${options.limit}),暂不执行新任务`);
212
+ return;
213
+ }
214
+ const pendingTasks = await claimPendingTasks(limit);
215
+
216
+ if (pendingTasks.length > 0) {
217
+ fastify.log.info(`本轮执行 ${pendingTasks.length} 个待处理的系统任务`);
218
+ await Promise.all(pendingTasks.map(async task => processSystemTask(task, { claimed: true })));
219
+ }
220
+ };
221
+
222
+ Object.assign(fastify[options.name].services, {
223
+ runner,
224
+ resetAll,
225
+ claimPendingTasks,
226
+ claimTask,
227
+ triggerChildTasks,
228
+ processSystemTask
229
+ });
230
+ });
@@ -0,0 +1,230 @@
1
+ const fp = require('fastify-plugin');
2
+ const getTaskServiceContext = require('../helpers/task-service-context');
3
+
4
+ module.exports = fp(async (fastify, options) => {
5
+ const context = getTaskServiceContext(fastify, options);
6
+ const { models, Op } = context;
7
+ const SORTABLE_FIELDS = new Set(['id', 'targetId', 'targetType', 'targetName', 'type', 'status', 'runnerType', 'priority', 'createdAt', 'updatedAt', 'startTime', 'startedAt', 'completedAt']);
8
+
9
+ const cancel = async ({ id, targetId, targetType, type }) => {
10
+ if (!id && !(targetId && targetType && type)) {
11
+ throw new Error('必须提供 id 或 targetId+targetType+type');
12
+ }
13
+ if (targetId && targetType && type) {
14
+ const bulkWhere = {
15
+ targetId,
16
+ targetType,
17
+ type,
18
+ status: {
19
+ [Op.in]: ['pending', 'running']
20
+ }
21
+ };
22
+ const tasksToCancel = await models.task.findAll({ where: bulkWhere, raw: true });
23
+ const completedAt = new Date();
24
+ const [affectedCount] = await models.task.update(
25
+ {
26
+ status: 'canceled',
27
+ completedAt
28
+ },
29
+ {
30
+ where: bulkWhere
31
+ }
32
+ );
33
+ for (const row of tasksToCancel) {
34
+ context.collectTaskStatistics(Object.assign({}, row, { status: 'canceled', completedAt }));
35
+ }
36
+ return affectedCount;
37
+ }
38
+ if (id) {
39
+ const task = await context.detail({ id });
40
+ const transitioned = await context.updateTaskByStatus({
41
+ task,
42
+ allowedStatuses: ['pending', 'running'],
43
+ updateData: {
44
+ status: 'canceled',
45
+ completedAt: new Date()
46
+ }
47
+ });
48
+ if (!transitioned) {
49
+ return;
50
+ }
51
+ context.collectTaskStatistics(task);
52
+ }
53
+ };
54
+
55
+ const complete = async ({ id, userId, output, ...props }) => {
56
+ const task = await context.detail({ id });
57
+ if (props.status === 'success') {
58
+ try {
59
+ await context.finalizeSuccess({ task, output, userId });
60
+ } catch (e) {
61
+ await context.failTask({ task, error: e, updateData: Object.assign({}, props, { output, completedUserId: userId }) });
62
+ throw e;
63
+ }
64
+ } else {
65
+ await context.failTask({ task, error: props.error || '任务失败', updateData: Object.assign({}, props, { completedUserId: userId }) });
66
+ }
67
+ };
68
+
69
+ const waitingComplete = async ({ id, pollInterval = 1000, maxPollTimes = 20 }) => {
70
+ let task = await context.detail({ id });
71
+
72
+ if (task.status === 'pending') {
73
+ await context.updateTaskByStatus({
74
+ task,
75
+ allowedStatuses: ['pending'],
76
+ updateData: { priority: Number.MAX_SAFE_INTEGER }
77
+ });
78
+ const claimedTask = await fastify[options.name].services.claimTask(task);
79
+ if (claimedTask) {
80
+ await fastify[options.name].services.processSystemTask(claimedTask, { claimed: true });
81
+ task = claimedTask;
82
+ }
83
+ await task.reload();
84
+ }
85
+
86
+ if (task.status === 'success') {
87
+ return task.output;
88
+ }
89
+ if (task.status === 'failed') {
90
+ throw new Error(task.error || '任务失败');
91
+ }
92
+ if (task.status === 'canceled') {
93
+ throw new Error(task.error || '任务取消');
94
+ }
95
+
96
+ let pollCount = 0;
97
+ return await new Promise((resolve, reject) => {
98
+ const executePolling = async () => {
99
+ try {
100
+ pollCount++;
101
+ if (pollCount > maxPollTimes) {
102
+ reject(new Error('任务超时'));
103
+ return;
104
+ }
105
+
106
+ await task.reload();
107
+ const { status, output, error } = task;
108
+
109
+ if (status === 'success') {
110
+ resolve(output);
111
+ return;
112
+ }
113
+
114
+ if (status === 'failed' || status === 'canceled') {
115
+ reject(new Error(error || `任务${status === 'failed' ? '失败' : '取消'}`));
116
+ return;
117
+ }
118
+
119
+ setTimeout(executePolling, pollInterval);
120
+ } catch (e) {
121
+ reject(e);
122
+ }
123
+ };
124
+ setTimeout(executePolling, pollInterval);
125
+ });
126
+ };
127
+
128
+ const getTimeQuery = fieldValue => {
129
+ const { startTime, endTime } = fieldValue;
130
+ if (!!startTime && !!endTime) {
131
+ return {
132
+ [Op.between]: [new Date(startTime), new Date(endTime)]
133
+ };
134
+ } else if (!!startTime) {
135
+ return {
136
+ [Op.gte]: new Date(startTime)
137
+ };
138
+ } else if (!!endTime) {
139
+ return {
140
+ [Op.lte]: new Date(endTime)
141
+ };
142
+ }
143
+ };
144
+
145
+ const list = async ({ filter, perPage = 20, currentPage = 1, sort }) => {
146
+ const whereQuery = {};
147
+
148
+ ['id', 'targetId', 'type', 'status', 'runnerType'].forEach(key => {
149
+ if (filter && filter[key]) {
150
+ whereQuery[key] = filter[key];
151
+ }
152
+ });
153
+
154
+ if (filter && filter.targetName) {
155
+ whereQuery['input.name'] = {
156
+ [Op.like]: `%${filter.targetName}%`
157
+ };
158
+ }
159
+
160
+ if (filter && filter.createdAt) {
161
+ whereQuery.createdAt = getTimeQuery(filter.createdAt);
162
+ }
163
+
164
+ if (filter && filter.completedAt) {
165
+ whereQuery.completedAt = getTimeQuery(filter.completedAt);
166
+ }
167
+
168
+ let order = [['createdAt', 'DESC']];
169
+
170
+ if (sort && Object.keys(sort).length > 0) {
171
+ order = Object.entries(sort).map(([key, direction]) => {
172
+ if (!SORTABLE_FIELDS.has(key)) {
173
+ throw new Error(`不支持的排序字段:${key}`);
174
+ }
175
+ const normalizedDirection = String(direction || '').toUpperCase();
176
+ if (!['ASC', 'DESC'].includes(normalizedDirection)) {
177
+ throw new Error(`不支持的排序方向:${direction}`);
178
+ }
179
+ return [key, normalizedDirection];
180
+ });
181
+ }
182
+
183
+ const { rows, count } = await models.task.findAndCountAll({
184
+ where: Object.assign({}, whereQuery),
185
+ offset: perPage * (currentPage - 1),
186
+ limit: perPage,
187
+ order: order
188
+ });
189
+
190
+ return {
191
+ pageData: rows,
192
+ totalCount: count
193
+ };
194
+ };
195
+
196
+ const retryFunc = async ({ id }) => {
197
+ const task = await context.detail({ id });
198
+ if (['failed', 'canceled'].indexOf(task.status) === -1) {
199
+ throw new Error('只有失败或取消的任务允许重试');
200
+ }
201
+ await task.update({
202
+ status: 'pending',
203
+ completedAt: null,
204
+ completedUserId: null,
205
+ retryCount: 0,
206
+ error: null
207
+ });
208
+ };
209
+
210
+ const retry = async ({ id, taskIds }) => {
211
+ if (id) {
212
+ await retryFunc({ id });
213
+ }
214
+ if (taskIds && taskIds.length > 0) {
215
+ for (const taskId of taskIds) {
216
+ await retryFunc({ id: taskId });
217
+ }
218
+ }
219
+ };
220
+
221
+ Object.assign(fastify[options.name].services, {
222
+ create: context.create,
223
+ detail: context.detail,
224
+ list,
225
+ complete,
226
+ cancel,
227
+ retry,
228
+ waitingComplete
229
+ });
230
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-task",
3
- "version": "3.0.0-alpha.7",
3
+ "version": "3.0.0-alpha.8",
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",
59
+ "@kne/fastify-statistics": "^0.1.1",
60
60
  "dayjs": "^1.11.20",
61
61
  "fastify-cron": "^1.4.0",
62
62
  "fs-extra": "^11.3.0"