Skip to content

Commit 4c41319

Browse files
atian25popomore
authored andcommitted
feat: support stop --title (#16)
* feat: support stop --title * feat: remove stop baseDir
1 parent e1a11c2 commit 4c41319

File tree

5 files changed

+148
-84
lines changed

5 files changed

+148
-84
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ $ eggctl start [options] [baseDir]
4646
- `baseDir` - directory of application, default to `process.cwd()`.
4747
- **Options**
4848
- `port` - listening port, default to `process.env.PORT`, if unset, egg will use `7001` as default.
49-
- `title` - process title description, use for kill grep, default to `egg-server-APPNAME`.
49+
- `title` - process title description, use for kill grep, default to `egg-server-${APP_NAME}`.
5050
- `workers` - numbers of app workers, default to `process.env.EGG_WORKERS`, if unset, egg will use `os.cpus().length` as default.
5151
- `daemon` - whether run at background daemon mode.
5252
- `framework` - specify framework that can be absolute path or npm package, default to auto detect.
@@ -61,9 +61,8 @@ Stop egg gracefull.
6161

6262
```bash
6363
# stop egg
64-
$ eggctl stop [baseDir]
65-
# eggctl stop ./server
64+
$ eggctl stop [--title=example]
6665
```
6766

68-
- **Arguments**
69-
- `baseDir` - directory of application, default to `process.cwd()`.
67+
- **Options**
68+
- `title` - process title description, use for kill grep.

lib/cmd/start.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class StartCommand extends Command {
1919

2020
this.options = {
2121
title: {
22-
description: 'process title description, use for kill grep, default to `egg-server-APPNAME`',
22+
description: 'process title description, use for kill grep, default to `egg-server-${APP_NAME}`',
2323
type: 'string',
2424
},
2525
workers: {

lib/cmd/stop.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ class StopCommand extends Command {
88

99
constructor(rawArgv) {
1010
super(rawArgv);
11-
this.usage = 'Usage: egg-scripts stop [baseDir]';
11+
this.usage = 'Usage: egg-scripts stop [--title=example]';
1212
this.serverBin = path.join(__dirname, '../start-cluster');
13+
this.options = {
14+
title: {
15+
description: 'process title description, use for kill grep',
16+
type: 'string',
17+
},
18+
};
1319
}
1420

1521
get description() {
@@ -25,19 +31,14 @@ class StopCommand extends Command {
2531

2632
const { argv } = context;
2733

28-
// egg-script stop
29-
// egg-script stop ./server
30-
// egg-script stop /opt/app
31-
let baseDir = argv._[0] || context.cwd;
32-
if (!path.isAbsolute(baseDir)) baseDir = path.join(context.cwd, baseDir);
33-
argv.baseDir = baseDir;
34-
35-
this.logger.info(`stopping egg application at ${baseDir}`);
34+
this.logger.info(`stopping egg application ${argv.title ? `with --title=${argv.title}` : ''}`);
3635

3736
// 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"}
3837
let processList = yield this.helper.findNodeProcess(item => {
3938
const cmd = item.cmd;
40-
return cmd.includes('start-cluster');
39+
return argv.title ?
40+
cmd.includes('start-cluster') && cmd.includes(`"title":"${argv.title}"`) :
41+
cmd.includes('start-cluster');
4142
});
4243
let pids = processList.map(x => x.pid);
4344

@@ -55,7 +56,9 @@ class StopCommand extends Command {
5556
// 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}
5657
processList = yield this.helper.findNodeProcess(item => {
5758
const cmd = item.cmd;
58-
return cmd.includes('egg-cluster/lib/app_worker.js') || cmd.includes('egg-cluster/lib/agent_worker.js');
59+
return argv.title ?
60+
(cmd.includes('egg-cluster/lib/app_worker.js') || cmd.includes('egg-cluster/lib/agent_worker.js')) && cmd.includes(`"title":"${argv.title}"`) :
61+
(cmd.includes('egg-cluster/lib/app_worker.js') || cmd.includes('egg-cluster/lib/agent_worker.js'));
5962
});
6063
pids = processList.map(x => x.pid);
6164

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,24 @@
99
},
1010
"dependencies": {
1111
"common-bin": "^2.7.1",
12-
"egg-utils": "^2.2.0",
13-
"moment": "^2.19.1",
12+
"egg-utils": "^2.3.0",
13+
"moment": "^2.19.2",
1414
"mz": "^2.7.0",
1515
"mz-modules": "^2.0.0",
1616
"node-homedir": "^1.1.0",
1717
"runscript": "^1.3.0",
1818
"zlogger": "^1.1.0"
1919
},
2020
"devDependencies": {
21-
"autod": "^2.10.1",
21+
"autod": "^3.0.1",
2222
"coffee": "^4.1.0",
23-
"egg": "^1.9.0",
23+
"egg": "^1.11.0",
2424
"egg-bin": "^4.3.5",
2525
"egg-ci": "^1.8.0",
26-
"eslint": "^4.8.0",
26+
"eslint": "^4.11.0",
2727
"eslint-config-egg": "^5.1.1",
2828
"mm": "^2.2.0",
29-
"urllib": "^2.25.0",
29+
"urllib": "^2.25.1",
3030
"webstorm-disable-index": "^1.2.0"
3131
},
3232
"engines": {

test/stop.test.js

Lines changed: 123 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -49,67 +49,23 @@ describe('test/stop.test.js', () => {
4949
yield utils.cleanup(fixturePath);
5050
});
5151

52-
describe('full path', () => {
53-
it('should stop', function* () {
54-
killer = coffee.fork(eggBin, [ 'stop', fixturePath ]);
55-
killer.debug();
56-
killer.expect('code', 0);
57-
58-
// yield killer.end();
59-
yield sleep(waitTime);
60-
61-
// make sure is kill not auto exist
62-
assert(!app.stdout.includes('exist by env'));
63-
64-
assert(app.stdout.includes('[master] receive signal SIGTERM, closing'));
65-
assert(app.stdout.includes('[master] exit with code:0'));
66-
assert(app.stdout.includes('[app_worker] exit with code:0'));
67-
// assert(app.stdout.includes('[agent_worker] exit with code:0'));
68-
assert(killer.stdout.includes(`[egg-scripts] stopping egg application at ${fixturePath}`));
69-
assert(killer.stdout.match(/got master pid \["\d+\"\]/i));
70-
});
71-
});
52+
it('should stop', function* () {
53+
killer = coffee.fork(eggBin, [ 'stop', fixturePath ]);
54+
killer.debug();
55+
killer.expect('code', 0);
7256

73-
describe('relative path', () => {
74-
it('should stop', function* () {
75-
killer = coffee.fork(eggBin, [ 'stop', path.relative(process.cwd(), fixturePath) ]);
76-
killer.debug();
77-
killer.expect('code', 0);
78-
79-
// yield killer.end();
80-
yield sleep(waitTime);
81-
82-
// make sure is kill not auto exist
83-
assert(!app.stdout.includes('exist by env'));
84-
85-
assert(app.stdout.includes('[master] receive signal SIGTERM, closing'));
86-
assert(app.stdout.includes('[master] exit with code:0'));
87-
assert(app.stdout.includes('[app_worker] exit with code:0'));
88-
// assert(app.stdout.includes('[agent_worker] exit with code:0'));
89-
assert(killer.stdout.includes(`[egg-scripts] stopping egg application at ${fixturePath}`));
90-
assert(killer.stdout.match(/got master pid \["\d+\"\]/i));
91-
});
92-
});
57+
// yield killer.end();
58+
yield sleep(waitTime);
59+
60+
// make sure is kill not auto exist
61+
assert(!app.stdout.includes('exist by env'));
9362

94-
describe('without baseDir', () => {
95-
it('should stop', function* () {
96-
killer = coffee.fork(eggBin, [ 'stop' ], { cwd: fixturePath });
97-
killer.debug();
98-
killer.expect('code', 0);
99-
100-
// yield killer.end();
101-
yield sleep(waitTime);
102-
103-
// make sure is kill not auto exist
104-
assert(!app.stdout.includes('exist by env'));
105-
106-
assert(app.stdout.includes('[master] receive signal SIGTERM, closing'));
107-
assert(app.stdout.includes('[master] exit with code:0'));
108-
assert(app.stdout.includes('[app_worker] exit with code:0'));
109-
// assert(app.stdout.includes('[agent_worker] exit with code:0'));
110-
assert(killer.stdout.includes(`[egg-scripts] stopping egg application at ${fixturePath}`));
111-
assert(killer.stdout.match(/got master pid \["\d+\"\]/i));
112-
});
63+
assert(app.stdout.includes('[master] receive signal SIGTERM, closing'));
64+
assert(app.stdout.includes('[master] exit with code:0'));
65+
assert(app.stdout.includes('[app_worker] exit with code:0'));
66+
// assert(app.stdout.includes('[agent_worker] exit with code:0'));
67+
assert(killer.stdout.includes('[egg-scripts] stopping egg application'));
68+
assert(killer.stdout.match(/got master pid \["\d+\"\]/i));
11369
});
11470
});
11571

@@ -132,7 +88,7 @@ describe('test/stop.test.js', () => {
13288
it('should stop', function* () {
13389
yield coffee.fork(eggBin, [ 'stop', fixturePath ])
13490
.debug()
135-
.expect('stdout', new RegExp(`\\[egg-scripts] stopping egg application at ${fixturePath}`))
91+
.expect('stdout', /\[egg-scripts] stopping egg application/)
13692
.expect('stdout', /got master pid \["\d+\"\]/i)
13793
.expect('code', 0)
13894
.end();
@@ -159,10 +115,116 @@ describe('test/stop.test.js', () => {
159115
yield utils.cleanup(fixturePath);
160116
yield coffee.fork(eggBin, [ 'stop', fixturePath ])
161117
.debug()
162-
.expect('stdout', new RegExp(`\\[egg-scripts] stopping egg application at ${fixturePath}`))
118+
.expect('stdout', /\[egg-scripts] stopping egg application/)
119+
.expect('stderr', /can't detect any running egg process/)
120+
.expect('code', 0)
121+
.end();
122+
});
123+
});
124+
125+
describe('stop --title', () => {
126+
let app;
127+
let killer;
128+
129+
beforeEach(function* () {
130+
yield utils.cleanup(fixturePath);
131+
app = coffee.fork(eggBin, [ 'start', '--workers=2', '--title=example', fixturePath ]);
132+
// app.debug();
133+
app.expect('code', 0);
134+
yield sleep(waitTime);
135+
136+
assert(app.stderr === '');
137+
assert(app.stdout.match(/custom-framework started on http:\/\/127\.0\.0\.1:7001/));
138+
const result = yield httpclient.request('http://127.0.0.1:7001');
139+
assert(result.data.toString() === 'hi, egg');
140+
});
141+
142+
afterEach(function* () {
143+
app.proc.kill('SIGTERM');
144+
yield utils.cleanup(fixturePath);
145+
});
146+
147+
it('should stop', function* () {
148+
yield coffee.fork(eggBin, [ 'stop', '--title=random', fixturePath ])
149+
.debug()
150+
.expect('stdout', /\[egg-scripts] stopping egg application with --title=random/)
163151
.expect('stderr', /can't detect any running egg process/)
164152
.expect('code', 0)
165153
.end();
154+
155+
killer = coffee.fork(eggBin, [ 'stop', '--title=example' ], { cwd: fixturePath });
156+
killer.debug();
157+
killer.expect('code', 0);
158+
159+
// yield killer.end();
160+
yield sleep(waitTime);
161+
162+
// make sure is kill not auto exist
163+
assert(!app.stdout.includes('exist by env'));
164+
165+
assert(app.stdout.includes('[master] receive signal SIGTERM, closing'));
166+
assert(app.stdout.includes('[master] exit with code:0'));
167+
assert(app.stdout.includes('[app_worker] exit with code:0'));
168+
// assert(app.stdout.includes('[agent_worker] exit with code:0'));
169+
assert(killer.stdout.includes('[egg-scripts] stopping egg application with --title=example'));
170+
assert(killer.stdout.match(/got master pid \["\d+\"\]/i));
171+
});
172+
});
173+
174+
describe('stop all', () => {
175+
let app;
176+
let app2;
177+
let killer;
178+
179+
beforeEach(function* () {
180+
yield utils.cleanup(fixturePath);
181+
app = coffee.fork(eggBin, [ 'start', '--workers=2', '--title=example', fixturePath ]);
182+
// app.debug();
183+
app.expect('code', 0);
184+
185+
app2 = coffee.fork(eggBin, [ 'start', '--workers=2', '--title=test', '--port=7002', fixturePath ]);
186+
app2.expect('code', 0);
187+
188+
yield sleep(waitTime);
189+
190+
assert(app.stderr === '');
191+
assert(app.stdout.match(/custom-framework started on http:\/\/127\.0\.0\.1:7001/));
192+
const result = yield httpclient.request('http://127.0.0.1:7001');
193+
assert(result.data.toString() === 'hi, egg');
194+
195+
assert(app2.stderr === '');
196+
assert(app2.stdout.match(/custom-framework started on http:\/\/127\.0\.0\.1:7002/));
197+
const result2 = yield httpclient.request('http://127.0.0.1:7002');
198+
assert(result2.data.toString() === 'hi, egg');
199+
});
200+
201+
afterEach(function* () {
202+
app.proc.kill('SIGTERM');
203+
app2.proc.kill('SIGTERM');
204+
yield utils.cleanup(fixturePath);
205+
});
206+
207+
it('should stop', function* () {
208+
killer = coffee.fork(eggBin, [ 'stop' ], { cwd: fixturePath });
209+
killer.debug();
210+
killer.expect('code', 0);
211+
212+
// yield killer.end();
213+
yield sleep(waitTime);
214+
215+
// make sure is kill not auto exist
216+
assert(!app.stdout.includes('exist by env'));
217+
assert(app.stdout.includes('[master] receive signal SIGTERM, closing'));
218+
assert(app.stdout.includes('[master] exit with code:0'));
219+
assert(app.stdout.includes('[app_worker] exit with code:0'));
220+
// assert(app.stdout.includes('[agent_worker] exit with code:0'));
221+
assert(killer.stdout.includes('[egg-scripts] stopping egg application'));
222+
assert(killer.stdout.match(/got master pid \["\d+\","\d+\"\]/i));
223+
224+
assert(!app2.stdout.includes('exist by env'));
225+
assert(app2.stdout.includes('[master] receive signal SIGTERM, closing'));
226+
assert(app2.stdout.includes('[master] exit with code:0'));
227+
assert(app2.stdout.includes('[app_worker] exit with code:0'));
166228
});
167229
});
168230
});

0 commit comments

Comments
 (0)