diff --git a/hydro/entry/unzip.js b/hydro/entry/unzip.js index 4833dba3..2ca98d39 100644 --- a/hydro/entry/unzip.js +++ b/hydro/entry/unzip.js @@ -119,9 +119,8 @@ async function load() { } } } - setTimeout(() => { - process.exit(0); - }, 0); + console.log('Unzip done.'); + process.exit(0); } module.exports = load; diff --git a/hydro/loader.js b/hydro/loader.js index 8c77d65f..757bba44 100644 --- a/hydro/loader.js +++ b/hydro/loader.js @@ -12,22 +12,21 @@ async function terminate() { process.exit(0); } -async function fork(args) { - if (args) { - cluster.setupMaster({ args }); - const r = cluster.fork(); - cluster.setupMaster({ args: [] }); - return r; - } +async function fork(args = []) { + cluster.setupMaster({ args, exec: __filename }); return cluster.fork(); } async function entry(config) { if (config.entry) { if (config.newProcess) { - const process = await fork([`--entry=${config.entry}`]); - await new Promise((resolve) => { - process.on('exit', resolve); + const p = await fork([`--entry=${config.entry}`]); + await new Promise((resolve, reject) => { + p.on('exit', resolve); + p.on('error', (err) => { + p.kill(); + reject(err); + }); }); } else { const loader = require(`./entry/${config.entry}`); @@ -119,6 +118,7 @@ async function load() { } }); const cnt = await entry({ entry: 'master' }); + console.log('Master started'); cluster.on('exit', (worker, code, signal) => { console.log(`Worker ${worker.process.pid} ${worker.id} exit: ${code} ${signal}`); }); diff --git a/hydro/service/bus.js b/hydro/service/bus.js index 684b95de..500a145b 100644 --- a/hydro/service/bus.js +++ b/hydro/service/bus.js @@ -1,4 +1,5 @@ const { EventEmitter } = require('events'); +const cluster = require('cluster'); const bus = new EventEmitter(); @@ -26,7 +27,8 @@ function unsubscribe(events, handler, funcName) { } function publish(event, payload, isMaster = true) { - if (isMaster && process.send) { + // Process forked by pm2 would also have process.send + if (isMaster && process.send && !cluster.isMaster) { process.send({ event: 'bus', eventName: event,