diff --git a/README.md b/README.md index 6bba9c9..067b120 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ deploy tool for egg project. ## Install ```bash -$ npm i egg-scripts --save +$ npm i egg-scriptsx --save ``` ## Usage @@ -18,7 +18,8 @@ Add `eggctl` to `package.json` scripts: { "scripts": { "start": "eggctl start --daemon", - "stop": "eggctl stop" + "stop": "eggctl stop", + "reload": "eggctl reload --type=all" } } ``` @@ -27,8 +28,9 @@ Then run as: - `npm start` - `npm stop` +- `npm run reload` -**Note:** `egg-scripts` is not recommended to install global, you should install and use it as npm scripts. +**Note:** `egg-scriptsx` is not recommended to install global, you should install and use it as npm scripts. ## Command diff --git a/bin/egg-scripts.js b/bin/egg-scriptsx.js old mode 100755 new mode 100644 similarity index 100% rename from bin/egg-scripts.js rename to bin/egg-scriptsx.js diff --git a/index.js b/index.js index 5fd74e0..c9091e4 100644 --- a/index.js +++ b/index.js @@ -6,7 +6,7 @@ const Command = require('./lib/command'); class EggScripts extends Command { constructor(rawArgv) { super(rawArgv); - this.usage = 'Usage: egg-scripts [command] [options]'; + this.usage = 'Usage: egg-scriptsx [command] [options]'; // load directory this.load(path.join(__dirname, 'lib/cmd')); @@ -17,3 +17,4 @@ module.exports = exports = EggScripts; exports.Command = Command; exports.StartCommand = require('./lib/cmd/start'); exports.StopCommand = require('./lib/cmd/stop'); +exports.ReloadCommand = require('./lib/cmd/reload'); diff --git a/lib/cmd/reload.js b/lib/cmd/reload.js new file mode 100644 index 0000000..ea33318 --- /dev/null +++ b/lib/cmd/reload.js @@ -0,0 +1,101 @@ +'use strict'; + +const path = require('path'); +const util = require('util'); +const sleep = require('mz-modules/sleep'); +const Command = require('../command'); +const isWin = process.platform === 'win32'; +const osRelated = { + titleTemplate: isWin ? '\\"title\\":\\"%s\\"' : '"title":"%s"', + appWorkerPath: isWin ? 'egg-cluster\\lib\\app_worker.js' : 'egg-cluster/lib/app_worker.js', + agentWorkerPath: isWin ? 'egg-cluster\\lib\\agent_worker.js' : 'egg-cluster/lib/agent_worker.js', +}; + +class ReloadCommand extends Command { + + constructor(rawArgv) { + super(rawArgv); + this.usage = 'Usage: egg-scriptsx reload [--title=example] [--type=agent|app]'; + this.serverBin = path.join(__dirname, '../start-cluster'); + this.options = { + title: { + description: 'process title description, use for kill grep', + type: 'string', + }, + type:{ + description: `process egg app type, use reload['agent'||'app']`, + type: 'string', + }, + }; + } + + get description() { + return 'Reload egg-worker server'; + } + + * run(context) { + const { argv } = context; + + this.logger.info(`stopping egg application ${argv.title ? `with --title=${argv.title}` : ''}`); + + // node /Users/tz/Workspaces/eggjs/egg-scriptsx/lib/start-cluster {"title":"egg-server","workers":4,"port":7001,"baseDir":"/Users/tz/Workspaces/eggjs/test/showcase","framework":"/Users/tz/Workspaces/eggjs/test/showcase/node_modules/egg"} + let processList = yield this.helper.findNodeProcess(item => { + const cmd = item.cmd; + return argv.title ? + cmd.includes('start-cluster') && cmd.includes(util.format(osRelated.titleTemplate, argv.title)) : + cmd.includes('start-cluster'); + }); + let pids = processList.map(x => x.pid); + + if (pids.length) { + this.logger.info('got master pid %j', pids); + //this.helper.kill(pids); + // wait for 5s to confirm whether any worker process did not kill by master + yield sleep('2s'); + } else { + this.logger.warn('can\'t detect any running egg process'); + } + + + // node --debug-port=5856 /Users/tz/Workspaces/eggjs/test/showcase/node_modules/_egg-cluster@1.8.0@egg-cluster/lib/agent_worker.js {"framework":"/Users/tz/Workspaces/eggjs/test/showcase/node_modules/egg","baseDir":"/Users/tz/Workspaces/eggjs/test/showcase","port":7001,"workers":2,"plugins":null,"https":false,"key":"","cert":"","title":"egg-server","clusterPort":52406} + // node /Users/tz/Workspaces/eggjs/test/showcase/node_modules/_egg-cluster@1.8.0@egg-cluster/lib/app_worker.js {"framework":"/Users/tz/Workspaces/eggjs/test/showcase/node_modules/egg","baseDir":"/Users/tz/Workspaces/eggjs/test/showcase","port":7001,"workers":2,"plugins":null,"https":false,"key":"","cert":"","title":"egg-server","clusterPort":52406} + if(argv.type === 'agent'){ + processList = yield this.helper.findNodeProcess(item => { + const cmd = item.cmd; + return argv.title ? + ( cmd.includes(osRelated.agentWorkerPath)) && cmd.includes(util.format(osRelated.titleTemplate, argv.title)) : + ( cmd.includes(osRelated.agentWorkerPath)); + }); + } else if(argv.type === 'app'){ + processList = yield this.helper.findNodeProcess(item => { + const cmd = item.cmd; + return argv.title ? + (cmd.includes(osRelated.appWorkerPath)) && cmd.includes(util.format(osRelated.titleTemplate, argv.title)) : + (cmd.includes(osRelated.appWorkerPath)); + }); + } else { + processList = yield this.helper.findNodeProcess(item => { + const cmd = item.cmd; + return argv.title ? + (cmd.includes(osRelated.appWorkerPath)|| cmd.includes(osRelated.agentWorkerPath)) && cmd.includes(util.format(osRelated.titleTemplate, argv.title)) : + (cmd.includes(osRelated.appWorkerPath)|| cmd.includes(osRelated.agentWorkerPath)); + }); + } + + pids = processList.map(x => x.pid); + + if (pids.length) { + this.logger.info('got worker/agent pids %j that is not killed by master', pids); + + for( let i =0; i { const cmd = item.cmd; return argv.title ? diff --git a/lib/command.js b/lib/command.js index bd53436..38a2a21 100644 --- a/lib/command.js +++ b/lib/command.js @@ -29,7 +29,7 @@ class Command extends BaseCommand { }; this.logger = new Logger({ - prefix: '[egg-scripts] ', + prefix: '[egg-scriptsx] ', time: false, }); } diff --git a/package.json b/package.json index ebc1499..e94bb06 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,11 @@ { - "name": "egg-scripts", - "version": "2.11.1", + "name": "egg-scriptsx", + "version": "2.11.3", "description": "deploy tool for egg project", "main": "index.js", "bin": { - "egg-scripts": "bin/egg-scripts.js", - "eggctl": "bin/egg-scripts.js" + "egg-scriptsx": "bin/egg-scriptsx.js", + "eggctlx": "bin/egg-scriptsx.js" }, "dependencies": { "await-event": "^2.1.0",