-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathchild.js
194 lines (162 loc) · 4.1 KB
/
child.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
190
191
192
193
194
'use strict';
const Xible = require('./index.js');
let flow;
let flowInstance;
let xible;
// handle disconnects from master
process.on('disconnect', () => {
if (flowInstance) {
try {
flowInstance.stopChild();
} catch (err) {
process.exit(0);
}
} else {
process.exit(0);
}
});
/**
* Requires the path to a Node.
* The try/catch prevents proper compilation by the v8 engine,
* therefore it is in a seperate function.
* @param {String} nodePath Path to the node directory where index.js resides.
* @returns {Function|null}
*/
function requireNode(nodePath) {
return require(`${nodePath}/index.js`);
}
function initNodes(flowNodes, nodes) {
let structuredNodes = new Set();
let nodeName;
for (let i = 0; i < flowNodes.length; i += 1) {
nodeName = flowNodes[i].name;
if (structuredNodes.has(nodeName)) {
continue;
}
structuredNodes.add(nodeName);
// require the actual node and check it was loaded properly
const nodeConstr = nodes[nodeName];
nodeConstr.constructorFunction = requireNode(nodeConstr.path);
if (nodeConstr.constructorFunction) {
xible.addNode(nodeConstr);
}
}
structuredNodes = null;
}
function passableError(err) {
return {
constructorName: err.constructor ? err.constructor.name : undefined,
code: err.code,
message: err.message,
stack: err.stack
};
}
// error handling
let errorTrapped = false;
async function onError(err) {
if (flowInstance.listenerCount('error') && !errorTrapped) {
errorTrapped = true;
flowInstance.emit('error', err);
return;
}
console.error(err);
if (process.connected) {
process.send({
method: 'stop',
error: passableError(err)
});
process.exit(1);
} else if (flowInstance) {
await flowInstance.stopChild(1);
}
}
// always stop on unhandled promise rejections
process.on('unhandledRejection', onError);
process.on('uncaughtException', onError);
// init message handler
process.on('message', async (message) => {
switch (message.method) {
case 'init': {
xible = new Xible({
child: true
}, message.config);
try {
initNodes(message.flow.nodes, message.nodes);
await xible.init();
// add the typedefs
for (const typeDefName in message.typeDefs) {
xible.TypeDef.add(message.typeDefs[typeDefName]);
}
flow = new xible.Flow();
await flow.initJson(message.flow);
flowInstance = flow.createInstance();
flowInstance._id = message.flowInstanceId;
} catch (err) {
console.error(err);
process.send({
method: 'initerr',
error: passableError(err)
});
process.exit(1);
}
// inform the master that we initialized
if (process.connected) {
process.send({
method: 'initialized'
});
}
break;
}
case 'start': {
flowInstance.params = message.params || {};
flowInstance.directNodes = message.directNodes;
try {
if (message.directNodes) {
flowInstance.directed = true;
await flowInstance.directChild(message.directNodes);
} else {
await flowInstance.startChild();
}
if (!flowInstance.directed) {
process.channel.unref();
}
// inform the master that we actually started
if (process.connected) {
process.send({
method: 'started'
});
}
} catch (err) {
console.error(err);
if (process.connected) {
process.send({
method: 'stop',
error: passableError(err)
});
}
if (flowInstance) {
flowInstance.stopChild();
}
}
break;
}
case 'stop': {
if (flowInstance) {
flowInstance.stopChild();
}
break;
}
case 'directNodes': {
if (flowInstance) {
flowInstance.directChild(message.directNodes);
}
break;
}
}
});
// inform the master we have finished init
if (process.connected) {
process.send({
method: 'initializing'
});
}