egg-cluster
Advanced tools
Comparing version 1.12.6 to 1.13.0
1.13.0 / 2017-12-05 | ||
================== | ||
**features** | ||
* [[`366d9bb`](http://github.com/eggjs/egg-cluster/commit/366d9bbb40db2b920258210f51b0a15fe224974c)] - feat: add worker manager and check worker/agent status (#54) (Yiyu He <<dead_horse@qq.com>>) | ||
1.12.6 / 2017-11-21 | ||
@@ -3,0 +9,0 @@ ================== |
@@ -15,2 +15,3 @@ 'use strict'; | ||
const Manager = require('./utils/manager'); | ||
const parseOptions = require('./utils/options'); | ||
@@ -41,2 +42,3 @@ const Messenger = require('./utils/messenger'); | ||
this.options = parseOptions(options); | ||
this.workerManager = new Manager(); | ||
this.messenger = new Messenger(this); | ||
@@ -48,3 +50,2 @@ | ||
this.agentWorkerIndex = 0; | ||
this.agentWorker = null; | ||
this.closed = false; | ||
@@ -61,6 +62,2 @@ this[REALPORT] = this.options.port; | ||
this.log = function log(...args) { | ||
this.logger[this.logMethod](...args); | ||
}; | ||
// get the real framework info | ||
@@ -93,2 +90,5 @@ const frameworkPath = this.options.framework; | ||
this.messenger.send({ action, to: 'agent', data: this.options }); | ||
// start check agent and worker status | ||
this.workerManager.startCheck(); | ||
}); | ||
@@ -128,3 +128,2 @@ | ||
process.exit(1); | ||
return; | ||
} | ||
@@ -135,4 +134,20 @@ this.options.clusterPort = port; | ||
// exit when agent or worker exception | ||
this.workerManager.on('exception', ({ agent, worker }) => { | ||
const err = new Error(`[master] ${agent} agent and ${worker} worker(s) alive, exit to avoid unknown state`); | ||
err.name = 'ClusterWorkerExceptionError'; | ||
err.count = { agent, worker }; | ||
this.logger.error(err); | ||
process.exit(1); | ||
}); | ||
} | ||
log(...args) { | ||
this.logger[this.logMethod](...args); | ||
} | ||
get agentWorker() { | ||
return this.workerManager.agent; | ||
} | ||
startMasterSocketServer(cb) { | ||
@@ -157,3 +172,3 @@ // Create the outside facing server listening on our port. | ||
const workerNumbers = this.options.workers; | ||
const ws = Array.from(this.workers.keys()); | ||
const ws = this.workerManager.listWorkerIds(); | ||
@@ -168,3 +183,3 @@ let s = ''; | ||
const pid = ws[s % workerNumbers]; | ||
return this.workers.get(pid); | ||
return this.workerManager.getWorker(pid); | ||
} | ||
@@ -182,3 +197,4 @@ | ||
const agentWorker = this.agentWorker = childprocess.fork(agentWorkerFile, args, opt); | ||
const agentWorker = childprocess.fork(agentWorkerFile, args, opt); | ||
this.workerManager.setAgent(agentWorker); | ||
agentWorker.id = ++this.agentWorkerIndex; | ||
@@ -220,4 +236,2 @@ this.log('[master] agent_worker#%s:%s start with clusterPort:%s', | ||
this.workers = new Map(); | ||
const args = [ JSON.stringify(this.options) ]; | ||
@@ -237,3 +251,3 @@ this.log('[master] start appWorker with args %j', args); | ||
worker.disableRefork = true; | ||
this.workers.set(worker.process.pid, worker); | ||
this.workerManager.setWorker(worker); | ||
worker.on('message', msg => { | ||
@@ -309,3 +323,3 @@ if (typeof msg === 'string') msg = { action: msg, data: msg }; | ||
const agentWorker = this.agentWorker; | ||
this.agentWorker = null; | ||
this.workerManager.deleteAgent(this.agentWorker); | ||
@@ -346,3 +360,3 @@ const err = new Error(util.format('[master] agent_worker#%s:%s died (code: %s, signal: %s)', | ||
if (this.isStarted) { | ||
this.messenger.send({ action: 'egg-pids', to: 'agent', data: getListeningWorker(this.workers) }); | ||
this.messenger.send({ action: 'egg-pids', to: 'agent', data: this.workerManager.getListeningWorkerIds() }); | ||
} | ||
@@ -365,3 +379,3 @@ | ||
const worker = this.workers.get(data.workerPid); | ||
const worker = this.workerManager.getWorker(data.workerPid); | ||
@@ -390,5 +404,5 @@ if (!worker.isDevReload) { | ||
worker.removeAllListeners(); | ||
this.workers.delete(data.workerPid); | ||
this.workerManager.deleteWorker(data.workerPid); | ||
// send message to agent with alive workers | ||
this.messenger.send({ action: 'egg-pids', to: 'agent', data: getListeningWorker(this.workers) }); | ||
this.messenger.send({ action: 'egg-pids', to: 'agent', data: this.workerManager.getListeningWorkerIds() }); | ||
@@ -417,3 +431,3 @@ if (this.isAllAppWorkerStarted) { | ||
onAppStart(data) { | ||
const worker = this.workers.get(data.workerPid); | ||
const worker = this.workerManager.getWorker(data.workerPid); | ||
const address = data.address; | ||
@@ -433,3 +447,3 @@ | ||
to: 'agent', | ||
data: getListeningWorker(this.workers), | ||
data: this.workerManager.getListeningWorkerIds(), | ||
}); | ||
@@ -527,12 +541,2 @@ | ||
function getListeningWorker(workers) { | ||
const keys = []; | ||
for (const id of workers.keys()) { | ||
if (workers.get(id).state === 'listening') { | ||
keys.push(id); | ||
} | ||
} | ||
return keys; | ||
} | ||
function isProduction() { | ||
@@ -539,0 +543,0 @@ const serverEnv = process.env.EGG_SERVER_ENV; |
{ | ||
"name": "egg-cluster", | ||
"version": "1.12.6", | ||
"version": "1.13.0", | ||
"description": "cluster manager for egg", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
40583
11
857