-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathstart_client.js
executable file
·61 lines (50 loc) · 1.58 KB
/
start_client.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
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const path = require('path');
const nounou = require('nounou');
const clientPath = path.join(__dirname, 'client.js');
const argv = process.argv.slice(2);
function printUsage() {
console.log(`参数错误。用法示例:
agentx <config.json> start agentx with config.json
agentx -v --version display agentx version
agentx -h --help display this help and exit
`);
}
if (argv.length < 1) {
printUsage();
process.exit(1);
}
const input = argv[0];
if (input === '-v' || input === '--version') {
console.log(require('./package.json').version);
process.exit(0);
}
if (input === '-h' || input === '--help') {
printUsage();
process.exit(0);
}
if (!fs.existsSync(input)) {
console.log(`
${input} is not a valid json file.
`);
printUsage();
process.exit(1);
}
const pid = process.pid;
nounou(clientPath, {
args: [input],
count: 1
}).on('fork', function (client) {
console.log(`[${Date()}] [client:${client.pid}] new client start`);
}).on('disconnect', function (client) {
console.error(`[${Date()}] [${pid}] client:${client.pid} disconnect, suicide: ${client.suicide}.`);
}).on('expectedExit', function (client, code, signal) {
console.log(`[${Date()}] [${pid}], client ${client.pid} died (code: ${code}, signal: ${signal})`);
}).on('unexpectedExit', function (client, code, signal) {
const message = `client ${client.pid} died (code: ${code}, signal: ${signal})`;
const err = new Error(message);
err.name = 'ClientDiedError';
console.error(`[${Date()}] [${process.pid}] client exit: ${err.stack}`);
});