Skip to content

add reload cmd #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ deploy tool for egg project.
## Install

```bash
$ npm i egg-scripts --save
$ npm i egg-scriptsx --save
```

## Usage
Expand All @@ -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"
}
}
```
Expand 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

Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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');
101 changes: 101 additions & 0 deletions lib/cmd/reload.js
Original file line number Diff line number Diff line change
@@ -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/[email protected]@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/[email protected]@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<pids.length;i++ ){
this.logger.info(`Kill app_worker ${pids[i]}`);
this.helper.kill([pids[i]],'SIGKILL');
yield sleep('5s');
}
}

this.logger.info('stopped');
}
}

module.exports = ReloadCommand;
2 changes: 1 addition & 1 deletion lib/cmd/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const utils = require('egg-utils');
class StartCommand extends Command {
constructor(rawArgv) {
super(rawArgv);
this.usage = 'Usage: egg-scripts start [options] [baseDir]';
this.usage = 'Usage: egg-scriptsx start [options] [baseDir]';
this.serverBin = path.join(__dirname, '../start-cluster');

this.options = {
Expand Down
4 changes: 2 additions & 2 deletions lib/cmd/stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class StopCommand extends Command {

constructor(rawArgv) {
super(rawArgv);
this.usage = 'Usage: egg-scripts stop [--title=example]';
this.usage = 'Usage: egg-scriptsx stop [--title=example]';
this.serverBin = path.join(__dirname, '../start-cluster');
this.options = {
title: {
Expand All @@ -34,7 +34,7 @@ class StopCommand extends Command {

this.logger.info(`stopping egg application ${argv.title ? `with --title=${argv.title}` : ''}`);

// node /Users/tz/Workspaces/eggjs/egg-scripts/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"}
// 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 ?
Expand Down
2 changes: 1 addition & 1 deletion lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Command extends BaseCommand {
};

this.logger = new Logger({
prefix: '[egg-scripts] ',
prefix: '[egg-scriptsx] ',
time: false,
});
}
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down