Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
131 lines (130 loc) · 5.54 KB
/
Copy pathindex.js
File metadata and controls
131 lines (130 loc) · 5.54 KB
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
/* eslint no-console:0, no-process-exit:0 */
const log = process.env.WHY_IS_NODE_RUNNING && require('why-is-node-running'); // eslint-disable-line no-process-env
const methods = require('./methods');
const {load} = require('ut-config');
const vfs = require('./vfs');
const { dirname } = require('path');
module.exports = {
getConfig: load,
runParams: async function(params = {}, test, config) {
if (!config) config = await load(params);
const method = config.params.method;
let cluster;
if (config.cluster) {
cluster = require('./serverRequire')('cluster');
if (cluster.isMaster) {
const workerCount = config.cluster.workers || require('os').cpus().length;
for (let i = 0; i < workerCount; i += 1) {
cluster.setupMaster({args: [...process.argv.slice(2), `--service=${config.service}(${i})`]});
cluster.fork();
}
if (!['unit', 'load'].includes(method)) return Promise.resolve();
} else {
if (config.runBroker) { // ensure that multiple brokers don't try to use the same socket / pipe.
if (typeof config.broker.socket === 'string') {
config.broker.socketPid = true;
} else if (typeof config.broker.socket === 'number') {
config.broker.socket += cluster.worker.id;
} else if (config.broker.socket.port) {
config.broker.socket.port += cluster.worker.id;
} else {
const printableConfigValue = require('./serverRequire')('util').inspect(config.broker.socket);
throw new Error(`Unsupported broker.socket configuration: ${printableConfigValue}`);
}
}
config.console && config.console.port && (config.console.port = config.console.port + cluster.worker.id);
}
}
const main = params.main || require('./serverRequire')(params.resolve('./' + config.params.app));
return methods[method](main, config, test, vfs, cluster);
},
run: async function(params, test, assert) {
if (process.type === 'browser') {
return require('./serverRequire')('ut-front/electron')({main: params.root});
}
let config = {service: 'undefined'};
try {
config = await load(params);
const result = await this.runParams(params, test, config);
async function stop() {
try {
await result.stop();
} finally {
if (log) setTimeout(log, 10000);
}
}
function terminate(signal) {
result.logger?.fatal?.(new Error('Terminating process with ' + signal));
stop();
}
process.send && process.send('ready');
if (
(config.run && config.run.stop) ||
(!process.browser && require('./serverRequire').utCompile && require('./serverRequire').utCompile.compiling)
) {
await stop();
} else if (!test && process.getMaxListeners) {
if (process.getMaxListeners() < 15) process.setMaxListeners(15);
process.once('SIGTERM', terminate);
process.once('SIGINT', terminate);
}
return result;
} catch (err) {
if (assert) {
assert.threw(err);
} else if (err && err.message !== 'silent') {
console.error(JSON.stringify({
error: {...err, stack: err.stack && err.stack.split && err.stack.split('\n')},
level: 50,
service: config.service,
pid: process.pid,
hostname: require('os').hostname(),
name: 'run',
context: 'run',
mtd: 'error',
$meta: {
method: 'utRun.run',
mtid: 'error'
},
msg: err.message,
time: (new Date()).toISOString(),
v: 0
}));
}
process.exit?.(1); // node
throw err; // browser
}
},
microservice(mod, req, fn) {
const run = params => module.exports.run({
version: req('./package.json').version,
root: dirname(mod.filename),
resolve: req.resolve,
defaultConfig: {
repl: false,
utPort: {
concurrency: 200,
...(params?.method !== 'unit') && {logLevel: 'debug'}
},
utBus: {
serviceBus: {
...(params?.method !== 'unit') && {logLevel: 'debug'},
jsonrpc: {
debug: true,
host: 'localhost',
port: 8090
}
}
},
run: {
...(params?.method !== 'unit') && {logLevel: 'debug'}
}
},
...params
});
if (!fn) throw new Error('Missing parameter: microservice function');
fn.run = run;
if (require.main === mod) setImmediate(() => run(process.argv[3]?.match(/^[a-z]+$/) ? {} : {defaultOverlays: 'microservice'}));
return fn;
}
};