-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathagent_worker.js
62 lines (52 loc) · 1.61 KB
/
agent_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
'use strict';
/**
* agent worker is child_process forked by master.
*
* agent worker only exit in two cases:
* - receive signal SIGTERM, exit code 0 (exit gracefully)
* - receive disconnect event, exit code 110 (maybe master exit in accident)
*/
// $ node agent_worker.js options
const options = JSON.parse(process.argv[2]);
if (options.require) {
// inject
options.require.forEach(mod => {
require(mod);
});
}
let AgentWorker;
if (options.startMode === 'worker_threads') {
AgentWorker = require('./utils/mode/impl/worker_threads/agent').AgentWorker;
} else {
AgentWorker = require('./utils/mode/impl/process/agent').AgentWorker;
}
const debug = require('util').debuglog('egg-cluster:agent_worker');
const ConsoleLogger = require('egg-logger').EggConsoleLogger;
const consoleLogger = new ConsoleLogger({ level: process.env.EGG_AGENT_WORKER_LOGGER_LEVEL });
const Agent = require(options.framework).Agent;
debug('new Agent with options %j', options);
let agent;
try {
agent = new Agent(options);
} catch (err) {
consoleLogger.error(err);
throw err;
}
function startErrorHandler(err) {
consoleLogger.error(err);
consoleLogger.error('[agent_worker] start error, exiting with code:1');
AgentWorker.kill();
}
agent.ready(err => {
// don't send started message to master when start error
if (err) return;
agent.removeListener('error', startErrorHandler);
AgentWorker.send({ action: 'agent-start', to: 'master' });
});
// exit if agent start error
agent.once('error', startErrorHandler);
AgentWorker.gracefulExit({
logger: consoleLogger,
label: 'agent_worker',
beforeExit: () => agent.close(),
});