@kne/fastify-task 3.0.0-alpha.6 → 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,45 @@
1
+ const fp = require('fastify-plugin');
2
+ const fs = require('fs-extra');
3
+ const getTaskServiceContext = require('../helpers/task-service-context');
4
+
5
+ module.exports = fp(async (fastify, options) => {
6
+ const context = getTaskServiceContext(fastify, options);
7
+
8
+ const append = async ({ name = context.mainNamespace, dir, dirs, scriptName, tasks, override = false }) => {
9
+ const appendDirs = context.normalizeDirs({ dir, dirs });
10
+ const result = { dirs: [], tasks: [], skippedTasks: [] };
11
+ for (const currentDir of appendDirs) {
12
+ if (!options.dirs.includes(currentDir)) {
13
+ if (!(await fs.exists(currentDir))) {
14
+ console.warn(`append 目录不存在:${currentDir},仍会添加但运行时可能无法匹配任务执行器`);
15
+ }
16
+ // 保持旧 API 可见性;执行器会优先使用任务声明自己的 dirs。
17
+ options.dirs.push(currentDir);
18
+ result.dirs.push(currentDir);
19
+ }
20
+ }
21
+ if (tasks && typeof tasks === 'object') {
22
+ Object.entries(tasks).forEach(([taskName, config]) => {
23
+ const { skipped, declaration } = context.registerTaskDeclaration({
24
+ namespace: name,
25
+ taskName,
26
+ dirs: appendDirs.length > 0 ? appendDirs : options.dirs,
27
+ scriptName: scriptName || 'index',
28
+ config,
29
+ override,
30
+ syncLegacyTask: true
31
+ });
32
+ if (skipped) {
33
+ result.skippedTasks.push(declaration.type);
34
+ return;
35
+ }
36
+ result.tasks.push(declaration.type);
37
+ });
38
+ }
39
+ return result;
40
+ };
41
+
42
+ Object.assign(fastify[options.name].services, {
43
+ append
44
+ });
45
+ });
@@ -0,0 +1,109 @@
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
+
7
+ const processNext = async ({ id, signature, result: resultStr }) => {
8
+ const task = await context.detail({ id });
9
+ if (task.status !== 'waiting') {
10
+ throw new Error('当前任务状态不允许执行Next操作');
11
+ }
12
+ if (task.context?.secret && !context.verifySignature({ secret: task.context.secret, id, data: resultStr, signature })) {
13
+ throw new Error('签名验证失败');
14
+ }
15
+ let result;
16
+ try {
17
+ result = JSON.parse(resultStr);
18
+ } catch (e) {
19
+ await context.failTask({ task, error: e });
20
+ throw e;
21
+ }
22
+ if (result.code !== 0) {
23
+ await context.failTask({ task, error: result });
24
+ return;
25
+ }
26
+ try {
27
+ await context.finalizeSuccess({ task, output: result, handlerResult: result.data });
28
+ } catch (e) {
29
+ await context.failTask({ task, error: e, updateData: { output: result } });
30
+ throw e;
31
+ }
32
+ };
33
+
34
+ const log = async ({ id, taskId, data, message = '' }) => {
35
+ const targetId = id || taskId;
36
+ const task = await context.detail({ id: targetId });
37
+
38
+ const currentOptions = task.options || {};
39
+ const logs = (currentOptions.logs || []).slice(0);
40
+ logs.splice(0, 0, {
41
+ data,
42
+ message,
43
+ time: new Date()
44
+ });
45
+ if (logs.length > 100) {
46
+ logs.splice(0, logs.length - 100);
47
+ }
48
+ await task.update({
49
+ options: {
50
+ ...currentOptions,
51
+ logs
52
+ }
53
+ });
54
+
55
+ return task;
56
+ };
57
+
58
+ const logWithSignature = async ({ id, taskId, data, message = '', signature }) => {
59
+ const targetId = id || taskId;
60
+ const task = await context.detail({ id: targetId });
61
+
62
+ if (task.context?.secret && !context.verifySignature({ secret: task.context.secret, id: targetId, data: { data, message }, signature })) {
63
+ throw new Error('签名验证失败');
64
+ }
65
+
66
+ return log({ id: targetId, data, message });
67
+ };
68
+
69
+ const callback = async ({ id, code, data, message }) => {
70
+ await context.detail({ id });
71
+
72
+ const result = { code, data, message };
73
+ await log({ id, message: '回调结果', data: JSON.stringify(result) });
74
+ const input = Object.assign(
75
+ {},
76
+ { id },
77
+ code === 0
78
+ ? {
79
+ status: 'success',
80
+ output: data
81
+ }
82
+ : {
83
+ status: 'failed',
84
+ output: data,
85
+ error: message
86
+ }
87
+ );
88
+ await fastify[options.name].services.complete(input);
89
+ };
90
+
91
+ const callbackWithSignature = async ({ id, taskId, code, data, message, signature }) => {
92
+ const targetId = id || taskId;
93
+ const task = await context.detail({ id: targetId });
94
+
95
+ if (task.context?.secret && !context.verifySignature({ secret: task.context.secret, id: targetId, data: { code, data, message }, signature })) {
96
+ throw new Error('签名验证失败');
97
+ }
98
+
99
+ return callback({ id: targetId, code, data, message });
100
+ };
101
+
102
+ Object.assign(fastify[options.name].services, {
103
+ log,
104
+ logWithSignature,
105
+ callback,
106
+ callbackWithSignature,
107
+ processNext
108
+ });
109
+ });
@@ -0,0 +1,10 @@
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
+
7
+ Object.assign(fastify[options.name].services, {
8
+ executor: context.executor
9
+ });
10
+ });