@kne/fastify-task 3.0.0-alpha.1 → 3.0.0-alpha.11

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,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
+ });