think-cluster
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -7,3 +7,5 @@ const cluster = require('cluster'); | ||
const BIND_EVENT = Symbol('bind-event'); | ||
const MESSENGER = 'think-messenger'; | ||
const mapPromise = new Map(); | ||
@@ -38,6 +40,29 @@ let taskId = 1; | ||
bindEvent() { | ||
if (process[BIND_EVENT]) return; | ||
process[BIND_EVENT] = true; | ||
if (cluster.isMaster) { | ||
cluster.on('message', (worker, message) => { | ||
if (message && message.act === MESSENGER) { | ||
const workers = this.getWorkers(message.target, worker); | ||
if (!message || message.act !== MESSENGER) return; | ||
const workers = this.getWorkers(message.target, worker); | ||
if (message.map) { | ||
if (message.mapReturn) { | ||
const map = mapPromise.get(message.action); | ||
map.get(worker).resolve(message.data); | ||
} else { | ||
const map = new Map(); | ||
mapPromise.set(message.action, map); | ||
const promises = workers.map(worker => { | ||
worker.send(message); | ||
const defer = helper.defer(); | ||
map.set(worker, defer); | ||
return defer.promise; | ||
}); | ||
Promise.all(promises).then(data => { | ||
mapPromise.delete(message.action); | ||
message.data = data; | ||
message.action = `${message.action}_ret`; | ||
worker.send(message); | ||
}); | ||
} | ||
} else { | ||
workers.forEach(worker => worker.send(message)); | ||
@@ -48,3 +73,12 @@ } | ||
process.on('message', message => { | ||
if (message && message.act === MESSENGER) { | ||
if (!message || message.act !== MESSENGER) return; | ||
if (message.map && !message.data) { | ||
const listener = this.listeners(message.action)[0]; | ||
assert(helper.isFunction(listener), `${message.action} listener must be a function`); | ||
message.mapReturn = true; | ||
Promise.resolve(listener()).then(data => { | ||
message.data = data; | ||
process.send(message); | ||
}); | ||
} else { | ||
this.emit(message.action, message.data); | ||
@@ -70,2 +104,19 @@ } | ||
/** | ||
* map worker task, return worker exec result | ||
* @param {String} action | ||
*/ | ||
map(action) { | ||
const defer = helper.defer(); | ||
process.send({ | ||
act: MESSENGER, | ||
action, | ||
map: true, | ||
target: 'all' | ||
}); | ||
this.once(`${action}_ret`, data => { | ||
defer.resolve(data); | ||
}); | ||
return defer.promise; | ||
} | ||
/** | ||
* this method will be deprecated | ||
@@ -72,0 +123,0 @@ * @param {Function} callback |
@@ -83,2 +83,4 @@ const cluster = require('cluster'); | ||
worker.once('listening', address => { | ||
// add prev pid to process.env | ||
env.THINK_PREV_PID = worker.process.pid; | ||
deferred.resolve({worker, address}); | ||
@@ -85,0 +87,0 @@ }); |
{ | ||
"name": "think-cluster", | ||
"description": "Cluster manage for ThinkJS", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "welefen", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
50491
1764