Skip to content

Commit cfd0d2f

Browse files
authored
refactor: modify the directory of logDir (#8)
- export this.child - export getFrameworkPath - support customize stdout and stderr
1 parent 49d8033 commit cfd0d2f

File tree

5 files changed

+108
-48
lines changed

5 files changed

+108
-48
lines changed

lib/cmd/start.js

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ class StartCommand extends Command {
4444
description: 'whether run at background daemon mode',
4545
type: 'boolean',
4646
},
47+
stdout: {
48+
description: 'A file that stdout redirect to',
49+
type: 'string',
50+
},
51+
stderr: {
52+
description: 'A file that stderr redirect to',
53+
type: 'string',
54+
},
4755
};
4856
}
4957

@@ -53,6 +61,8 @@ class StartCommand extends Command {
5361

5462
* run(context) {
5563
const argv = Object.assign({}, context.argv);
64+
const HOME = homedir();
65+
const logDir = path.join(HOME, 'logs');
5666

5767
// egg-script start
5868
// egg-script start ./server
@@ -63,46 +73,47 @@ class StartCommand extends Command {
6373

6474
const isDaemon = argv.daemon;
6575

66-
argv.framework = utils.getFrameworkPath({
76+
argv.framework = yield this.getFrameworkPath({
6777
framework: argv.framework,
6878
baseDir,
6979
});
7080

71-
const env = context.env;
72-
env.PWD = baseDir;
73-
env.HOME = homedir();
74-
env.NODE_ENV = 'production';
75-
76-
// cli argv -> process.env.EGG_SERVER_ENV -> `undefined` then egg will use `prod`
77-
if (argv.env) {
78-
// if undefined, should not pass key due to `spwan`, https://github.com/nodejs/node/blob/master/lib/child_process.js#L470
79-
env.EGG_SERVER_ENV = argv.env;
80-
argv.env = undefined;
81-
}
82-
8381
const pkgInfo = require(path.join(baseDir, 'package.json'));
84-
const logDir = path.join(env.HOME, 'logs', pkgInfo.name);
85-
8682
argv.title = argv.title || `egg-server-${pkgInfo.name}`;
8783

84+
argv.stdout = argv.stdout || path.join(logDir, 'master-stdout.log');
85+
argv.stderr = argv.stderr || path.join(logDir, 'master-stderr.log');
86+
87+
const env = context.env;
88+
env.HOME = HOME;
89+
env.NODE_ENV = 'production';
90+
8891
// adjust env for win
89-
let envPath = env.PATH || env.Path;
92+
const envPath = env.PATH || env.Path;
9093
if (envPath) {
9194
// for nodeinstall
92-
envPath = path.join(baseDir, 'node_modules/.bin') + path.delimiter + envPath;
95+
env.PATH = path.join(baseDir, 'node_modules/.bin') + path.delimiter + envPath;
9396
}
9497

9598
// for alinode
9699
env.ENABLE_NODE_LOG = 'YES';
97100
env.NODE_LOG_DIR = env.NODE_LOG_DIR || path.join(logDir, 'alinode');
98101
yield mkdirp(env.NODE_LOG_DIR);
99102

103+
// cli argv -> process.env.EGG_SERVER_ENV -> `undefined` then egg will use `prod`
104+
if (argv.env) {
105+
// if undefined, should not pass key due to `spwan`, https://github.com/nodejs/node/blob/master/lib/child_process.js#L470
106+
env.EGG_SERVER_ENV = argv.env;
107+
argv.env = undefined;
108+
}
109+
100110
// remove unused properties, alias had been remove by `removeAlias`
101111
argv._ = undefined;
102112
argv.$0 = undefined;
103113
argv.daemon = undefined;
104114

105115
const options = {
116+
cwd: baseDir,
106117
execArgv: context.execArgv,
107118
env,
108119
stdio: 'inherit',
@@ -117,11 +128,11 @@ class StartCommand extends Command {
117128
// whether run in the background.
118129
if (isDaemon) {
119130
this.logger.info(`save log file to ${logDir}`);
120-
const { stdout, stderr } = yield getRotatelog(logDir);
131+
const [ stdout, stderr ] = yield [ getRotatelog(argv.stdout), getRotatelog(argv.stderr) ];
121132
options.stdio = [ 'ignore', stdout, stderr, 'ipc' ];
122133
options.detached = true;
123134

124-
const child = spawn('node', eggArgs, options);
135+
const child = this.child = spawn('node', eggArgs, options);
125136
child.on('message', msg => {
126137
if (msg && msg.action === 'egg-ready') {
127138
this.logger.info(`egg started on ${msg.data.address}`);
@@ -135,32 +146,24 @@ class StartCommand extends Command {
135146
this.helper.spawn('node', eggArgs, options);
136147
}
137148
}
138-
}
139149

140-
function* getRotatelog(logDir) {
141-
const stdoutPath = path.join(logDir, 'master-stdout.log');
142-
const stderrPath = path.join(logDir, 'master-stderr.log');
150+
* getFrameworkPath(params) {
151+
return utils.getFrameworkPath(params);
152+
}
143153

144-
// format style: .20150602.193100
145-
const timestamp = moment().format('.YYYYMMDD.HHmmss');
154+
}
146155

147-
yield mkdirp(logDir);
156+
function* getRotatelog(logfile) {
157+
yield mkdirp(path.dirname(logfile));
148158

149-
/* istanbul ignore else */
150-
if (yield fs.exists(stdoutPath)) {
159+
if (yield fs.exists(logfile)) {
160+
// format style: .20150602.193100
161+
const timestamp = moment().format('.YYYYMMDD.HHmmss');
151162
// Note: rename last log to next start time, not when last log file created
152-
yield fs.rename(stdoutPath, stdoutPath + timestamp);
153-
}
154-
155-
/* istanbul ignore else */
156-
if (yield fs.exists(stderrPath)) {
157-
yield fs.rename(stderrPath, stderrPath + timestamp);
163+
yield fs.rename(logfile, logfile + timestamp);
158164
}
159165

160-
return yield {
161-
stdout: fs.open(stdoutPath, 'a'),
162-
stderr: fs.open(stderrPath, 'a'),
163-
};
166+
return yield fs.open(logfile, 'a');
164167
}
165168

166169
module.exports = StartCommand;

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@
3232
"node": ">=6.0.0"
3333
},
3434
"scripts": {
35-
"test": "npm run lint -- --fix && npm run test-local",
35+
"pkgfiles": "egg-bin pkgfiles",
36+
"test": "npm run lint -- --fix && npm run pkgfiles && npm run test-local",
3637
"test-local": "egg-bin test",
3738
"cov": "egg-bin cov",
3839
"lint": "eslint .",
39-
"ci": "egg-bin pkgfiles --check && npm run lint && npm run cov",
40+
"ci": "npm run pkgfiles -- --check && npm run lint && npm run cov",
4041
"autod": "autod"
4142
},
4243
"files": [
@@ -57,4 +58,4 @@
5758
},
5859
"author": "TZ <[email protected]>",
5960
"license": "MIT"
60-
}
61+
}

test/fixtures/example/app/router.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ module.exports = app => {
88
app.get('/env', function* () {
99
this.body = app.config.env + ', ' + app.config.pre;
1010
});
11+
12+
app.get('/path', function* () {
13+
this.body = process.env.PATH;
14+
});
1115
};

test/start.test.js

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,24 @@ const sleep = require('mz-modules/sleep');
77
const rimraf = require('mz-modules/rimraf');
88
const mkdirp = require('mz-modules/mkdirp');
99
const coffee = require('coffee');
10-
const homedir = require('node-homedir');
1110
const httpclient = require('urllib');
1211
const mm = require('mm');
1312
const utils = require('./utils');
1413

1514
describe('test/start.test.js', () => {
1615
const eggBin = require.resolve('../bin/egg-scripts.js');
1716
const fixturePath = path.join(__dirname, 'fixtures/example');
18-
const homePath = homedir();
19-
const logDir = path.join(homePath, 'logs/example');
17+
const homePath = path.join(__dirname, 'fixtures/home');
18+
const logDir = path.join(homePath, 'logs');
2019
const waitTime = '10s';
2120

21+
before(function* () {
22+
yield mkdirp(homePath);
23+
});
24+
after(function* () {
25+
yield rimraf(homePath);
26+
});
27+
beforeEach(() => mm(process.env, 'MOCK_HOME_DIR', homePath));
2228
afterEach(() => mm.restore);
2329

2430
describe('start without daemon', () => {
@@ -262,8 +268,43 @@ describe('test/start.test.js', () => {
262268
assert(app.stdout.includes('## EGG_SERVER_ENV is not pass'));
263269
assert(app.stdout.includes('## CUSTOM_ENV: pre'));
264270
assert(app.stdout.match(/custom-framework started on http:\/\/127\.0\.0\.1:7001/));
265-
const result = yield httpclient.request('http://127.0.0.1:7001/env');
271+
let result = yield httpclient.request('http://127.0.0.1:7001/env');
266272
assert(result.data.toString() === 'pre, true');
273+
result = yield httpclient.request('http://127.0.0.1:7001/path');
274+
assert(result.data.toString().match(new RegExp(`^${fixturePath}/node_modules/.bin${path.delimiter}`)));
275+
});
276+
});
277+
278+
describe('--stdout --stderr', () => {
279+
let app;
280+
281+
before(function* () {
282+
yield utils.cleanup(fixturePath);
283+
yield rimraf(logDir);
284+
yield mkdirp(logDir);
285+
});
286+
287+
after(function* () {
288+
app.proc.kill('SIGTERM');
289+
yield utils.cleanup(fixturePath);
290+
yield rimraf(path.join(fixturePath, 'stdout.log'));
291+
yield rimraf(path.join(fixturePath, 'stderr.log'));
292+
});
293+
294+
it('should start', function* () {
295+
const stdout = path.join(fixturePath, 'stdout.log');
296+
const stderr = path.join(fixturePath, 'stderr.log');
297+
app = coffee.fork(eggBin, [ 'start', '--workers=1', '--daemon', `--stdout=${stdout}`, `--stderr=${stderr}`, fixturePath ]);
298+
// app.debug();
299+
app.expect('code', 0);
300+
301+
yield sleep(waitTime);
302+
303+
let content = yield fs.readFile(stdout, 'utf-8');
304+
assert(content.match(/custom-framework started on http:\/\/127\.0\.0\.1:7001/));
305+
306+
content = yield fs.readFile(stderr, 'utf-8');
307+
assert(content === '');
267308
});
268309
});
269310

@@ -337,4 +378,5 @@ describe('test/start.test.js', () => {
337378
assert(result.data.toString() === 'hi, egg');
338379
});
339380
});
381+
340382
});

test/stop.test.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,28 @@ const assert = require('assert');
55
const fs = require('mz/fs');
66
const sleep = require('mz-modules/sleep');
77
const rimraf = require('mz-modules/rimraf');
8+
const mkdirp = require('mz-modules/mkdirp');
89
const coffee = require('coffee');
9-
const homedir = require('node-homedir');
1010
const httpclient = require('urllib');
11+
const mm = require('mm');
1112
const utils = require('./utils');
1213

1314
describe('test/stop.test.js', () => {
1415
const eggBin = require.resolve('../bin/egg-scripts.js');
1516
const fixturePath = path.join(__dirname, 'fixtures/example');
16-
const homePath = homedir();
17-
const logDir = path.join(homePath, 'logs/example');
17+
const homePath = path.join(__dirname, 'fixtures/home');
18+
const logDir = path.join(homePath, 'logs');
1819
const waitTime = '10s';
1920

21+
before(function* () {
22+
yield mkdirp(homePath);
23+
});
24+
after(function* () {
25+
yield rimraf(homePath);
26+
});
27+
beforeEach(() => mm(process.env, 'MOCK_HOME_DIR', homePath));
28+
afterEach(() => mm.restore);
29+
2030
describe('stop without daemon', () => {
2131
let app;
2232
let killer;

0 commit comments

Comments
 (0)