-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworker.js
279 lines (237 loc) · 7.52 KB
/
worker.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
const Channel = require('./channel');
const http = require('http');
const objectAssignDeep = require('object-assign-deep');
const querystring = require('querystring');
const path = require('path');
const eetase = require('eetase');
eetase(process);
const socketClusterServer = require('socketcluster-server');
const fs = require('fs');
const Updater = require('./updater');
const argv = require('minimist')(process.argv.slice(2));
const DEFAULT_MODULE_ALIAS = 'chain';
const SOCKET_REPLACED_CODE = 4500;
const MODULE_ALIAS = argv['ldem-module-alias'];
let ipcTimeout = argv['ldem-ipc-timeout'];
let ackTimeout = argv['ldem-ack-timeout'];
(async () => {
await process.listener('disconnect').once();
process.exit();
})();
(async () => {
let masterInitResult;
try {
masterInitResult = await process.listener('message').once(ipcTimeout);
} catch (error) {
process.exit(1);
}
let [{appConfig, moduleConfig, moduleUpdates, moduleActiveUpdate}] = masterInitResult;
// Update ipcTimeout and ackTimeout using the config option at the module scope
ipcTimeout = moduleConfig.ipcTimeout;
ackTimeout = moduleConfig.ackTimeout;
let componentsConfig = moduleConfig.components;
let loggerConfig = componentsConfig.logger;
let Logger = require(loggerConfig.loggerLibPath);
let logger = new Logger({
...loggerConfig,
processStream: process,
processType: 'worker',
processAlias: MODULE_ALIAS
});
let updater = new Updater({
processStream: process,
updates: moduleUpdates,
activeUpdate: moduleActiveUpdate
});
let TargetModuleClass = require(path.resolve(process.cwd(), moduleConfig.modulePath));
let defaultModuleConfig = TargetModuleClass.defaults || {};
if (defaultModuleConfig.default != null) {
defaultModuleConfig = defaultModuleConfig.default;
}
// For backwards compatibility with Lisk modules.
let targetModuleConfig = objectAssignDeep({}, defaultModuleConfig, moduleConfig);
let targetModuleAppConfig = appConfig;
let targetModule;
try {
targetModule = new TargetModuleClass({
processStream: process,
logger,
alias: MODULE_ALIAS,
config: targetModuleConfig,
appConfig: targetModuleAppConfig,
updates: moduleUpdates,
updater
});
} catch (error) {
logger.fatal(error);
process.exit(1);
}
// For backward compatibility.
targetModule.options = targetModuleConfig;
targetModule.appConfig = targetModuleAppConfig;
let moduleRedirects = targetModuleConfig.moduleRedirects;
let unixSocketPrefix = targetModuleConfig.unixSocketNamePrefix || 'ldem';
let targetModuleDependencies = TargetModuleClass.dependencies || targetModule.dependencies;
function getUnixSocketPath(moduleAlias) {
return `/tmp/${unixSocketPrefix}_${moduleAlias}.sock`;
}
let ipcPath = getUnixSocketPath(MODULE_ALIAS);
try {
fs.unlinkSync(ipcPath);
} catch (error) {}
let httpServer = eetase(http.createServer());
let agServer = socketClusterServer.attach(httpServer);
(async () => {
for await (let {error} of agServer.listener('error')) {
logger.error(error);
}
})();
let inboundModuleSockets = {};
let moduleActionNames = Object.keys(targetModule.actions);
async function handleRPC(actionName, request) {
let moduleAction = targetModule.actions[actionName];
let isActionPublic = moduleAction.isPublic;
let moduleActionHandler = moduleAction.handler;
let requestData = request.data || {};
if (requestData.isPublic && !isActionPublic) {
let error = new Error(
`The ${
actionName
} action of the ${
MODULE_ALIAS
} module is not public`
);
request.error(error);
return;
}
let result;
try {
result = await moduleActionHandler({
isPublic: !!requestData.isPublic,
params: requestData.params
});
} catch (error) {
logger.debug(error);
let rpcError = new Error(
`The ${actionName} action invoked on the ${
MODULE_ALIAS
} module failed because of the following error: ${error.message}`
);
rpcError.name = 'RPCError';
rpcError.sourceError = {
...error,
message: error.message,
stack: requestData.isPublic ? undefined : error.stack
};
request.error(rpcError);
return;
}
request.end(result);
}
(async () => {
for await (let {socket} of agServer.listener('connection')) {
let query = socket.request.url.split('?')[1];
let sourceModule = querystring.parse(query || '').source;
socket.sourceModule = sourceModule;
let existingModuleSocket = inboundModuleSockets[sourceModule];
if (existingModuleSocket) {
existingModuleSocket.disconnect(
SOCKET_REPLACED_CODE,
`Connection from module ${
sourceModule
} to module ${
MODULE_ALIAS
} was replaced by a newer connection`
);
}
inboundModuleSockets[sourceModule] = socket;
(async () => {
for await (let {error} of socket.listener('error')) {
logger.warn(error);
}
})();
for (let actionName of moduleActionNames) {
(async () => {
for await (let request of socket.procedure(actionName)) {
await handleRPC(actionName, request);
}
})();
}
(async () => {
for await (let request of socket.procedure('ping')) {
let requestData = request.data || {};
if (requestData.isWorkerAction) {
request.end();
}
}
})();
}
})();
(async () => {
for await (let {socket, code} of agServer.listener('disconnection')) {
if (code !== SOCKET_REPLACED_CODE) {
delete inboundModuleSockets[socket.sourceModule];
}
}
})();
httpServer.listen(ipcPath);
try {
await httpServer.listener('listening').once(ipcTimeout);
} catch (err) {
let error = new Error(
`IPC server on module ${MODULE_ALIAS} failed to listen before timeout of ${ipcTimeout} milliseconds`
);
logger.fatal(error);
process.exit(1);
}
process.send({
event: 'workerHandshake',
dependencies: targetModuleDependencies
});
let result;
try {
result = await process.listener('message').once(ipcTimeout);
} catch (err) {
let error = new Error(
`The ${MODULE_ALIAS} module did not receive a masterHandshake event before timeout of ${ipcTimeout} milliseconds`
);
logger.fatal(error);
process.exit(1);
}
let [masterHandshake] = result;
let {dependencies, dependents, appDependentMap} = masterHandshake;
let channel = new Channel({
moduleAlias: MODULE_ALIAS,
moduleActions: moduleActionNames,
moduleRedirects,
dependencies,
dependents,
modulePathFunction: getUnixSocketPath,
exchange: agServer.exchange,
inboundModuleSockets,
connectTimeout: ipcTimeout,
subscribeTimeout: ipcTimeout,
ackTimeout,
allowPublishingWithoutAlias: targetModule.options.allowPublishingWithoutAlias,
defaultTargetModuleAlias: targetModule.options.defaultTargetModuleAlias
});
(async () => {
for await (let {error} of channel.listener('error')) {
logger.error(error);
}
})();
(async () => {
for await (let {action, request} of channel.listener('rpc')) {
await handleRPC(action, request);
}
})();
try {
await targetModule.load(channel, targetModule.options, targetModule.appConfig, appDependentMap);
} catch (error) {
logger.fatal(error);
process.exit(1);
}
process.send({
event: 'moduleReady'
});
})();