-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternaltask-input.js
189 lines (154 loc) · 8.13 KB
/
externaltask-input.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const EventEmitter = require('node:events');
function showStatus(node, msgCounter) {
if (msgCounter >= 1) {
node.status({ fill: 'blue', shape: 'dot', text: `handling tasks ${msgCounter}.` });
} else {
node.status({ fill: 'blue', shape: 'ring', text: `subcribed.` });
}
}
const started_external_tasks = {};
module.exports = function (RED) {
function ExternalTaskInput(config) {
RED.nodes.createNode(this, config);
var node = this;
var flowContext = node.context().flow;
node.engine = RED.nodes.getNode(config.engine);
var eventEmitter = flowContext.get('emitter');
if (!eventEmitter) {
flowContext.set('emitter', new EventEmitter());
eventEmitter = flowContext.get('emitter');
}
const register = async () => {
if (node.etw) {
try {
node.etw.stop();
node.log(`old etw closed: ${JSON.stringify(node.etw)}`);
} catch (e) {
node.log(`cant close etw: ${JSON.stringify(node.etw)}`);
}
}
const client = node.engine.engineClient;
if (!client) {
node.error('No engine configured.', {});
return;
}
const etwCallback = async (payload, externalTask) => {
const saveHandleCallback = (data, callback) => {
try {
callback(data);
} catch (error) {
node.error(`Error in callback 'saveHandleCallback': ${error.message}`, {});
}
};
return await new Promise((resolve, reject) => {
const handleFinishTask = (msg) => {
let result = RED.util.encodeObject(msg.payload);
// remote msg and format from result
delete result.format;
delete result.msg;
node.log(
`handle event for *external task flowNodeInstanceId* '${externalTask.flowNodeInstanceId}' and *processInstanceId* ${externalTask.processInstanceId} with result ${JSON.stringify(result)} on msg._msgid ${msg._msgid}.`
);
if (externalTask.flowNodeInstanceId) {
delete started_external_tasks[externalTask.flowNodeInstanceId];
}
showStatus(node, Object.keys(started_external_tasks).length);
//resolve(result);
saveHandleCallback(result, resolve);
};
const handleErrorTask = (msg) => {
node.log(
`handle error event for *external task flowNodeInstanceId* '${externalTask.flowNodeInstanceId}' and *processInstanceId* '${externalTask.processInstanceId}' on *msg._msgid* '${msg._msgid}'.`
);
if (externalTask.flowNodeInstanceId) {
delete started_external_tasks[externalTask.flowNodeInstanceId];
}
showStatus(node, Object.keys(started_external_tasks).length);
// TODO: with reject, the default error handling is proceed
// SEE: https://github.com/5minds/ProcessCube.Engine.Client.ts/blob/develop/src/ExternalTaskWorker.ts#L180
// reject(result);
//resolve(msg);
saveHandleCallback(msg, resolve);
};
eventEmitter.once(`handle-${externalTask.flowNodeInstanceId}`, (msg, isError = false) => {
node.log(
`handle event for *external task flowNodeInstanceId* '${externalTask.flowNodeInstanceId}' and *processInstanceId* '${externalTask.processInstanceId}' with *msg._msgid* '${msg._msgid}' and *isError* '${isError}'`
);
if (isError) {
handleErrorTask(msg);
} else {
handleFinishTask(msg);
}
});
started_external_tasks[externalTask.flowNodeInstanceId] = externalTask;
showStatus(node, Object.keys(started_external_tasks).length);
let msg = {
_msgid: RED.util.generateId(),
task: RED.util.encodeObject(externalTask),
payload: payload,
flowNodeInstanceId: externalTask.flowNodeInstanceId,
processInstanceId: externalTask.processInstanceId,
};
node.log(
`Received *external task flowNodeInstanceId* '${externalTask.flowNodeInstanceId}' and *processInstanceId* '${externalTask.processInstanceId}' with *msg._msgid* '${msg._msgid}'`
);
node.send(msg);
});
};
let options = RED.util.evaluateNodeProperty(config.workerConfig, config.workerConfigType, node);
let topic = RED.util.evaluateNodeProperty(config.topic, config.topicType, node)
client.externalTasks
.subscribeToExternalTaskTopic(topic, etwCallback, options)
.then(async (externalTaskWorker) => {
node.status({ fill: 'blue', shape: 'ring', text: 'subcribed' });
node.etw = externalTaskWorker;
// export type WorkerErrorHandler = (errorType: 'fetchAndLock' | 'extendLock' | 'processExternalTask' | 'finishExternalTask', error: Error, externalTask?: ExternalTask<any>) => void;
externalTaskWorker.onWorkerError((errorType, error, externalTask) => {
switch (errorType) {
case 'extendLock':
case 'finishExternalTask':
case 'processExternalTask':
node.error(
`Worker error ${errorType} for *external task flowNodeInstanceId* '${externalTask.flowNodeInstanceId}' and *processInstanceId* '${externalTask.processInstanceId}': ${error.message}`,
{}
);
if (externalTask) {
delete started_external_tasks[externalTask.flowNodeInstanceId];
}
showStatus(node, Object.keys(started_external_tasks).length);
break;
case 'fetchAndLock':
node.status({});
node.error(`Worker error ${errorType}: ${error.message}`, {});
break;
default:
// reduce noise error logs
break;
}
});
try {
externalTaskWorker.start();
showStatus(node, Object.keys(started_external_tasks).length);
} catch (error) {
node.error(`Worker start 'externalTaskWorker.start' failed: ${error.message}`, {});
}
node.on('close', () => {
try {
externalTaskWorker.stop();
} catch {
node.error('Client close failed', {});
}
});
})
.catch((error) => {
node.error(`Error in subscribeToExternalTaskTopic: ${error.message}`, {});
});
};
if (node.engine) {
register().catch((error) => {
node.error(error, {});
});
}
}
RED.nodes.registerType('externaltask-input', ExternalTaskInput);
};