-
Notifications
You must be signed in to change notification settings - Fork 37
refactor: modify the directory of logDir #8
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,14 @@ class StartCommand extends Command { | |
description: 'whether run at background daemon mode', | ||
type: 'boolean', | ||
}, | ||
stdout: { | ||
description: 'A file that stdout redirect to', | ||
type: 'string', | ||
}, | ||
stderr: { | ||
description: 'A file that stderr redirect to', | ||
type: 'string', | ||
}, | ||
}; | ||
} | ||
|
||
|
@@ -53,6 +61,8 @@ class StartCommand extends Command { | |
|
||
* run(context) { | ||
const argv = Object.assign({}, context.argv); | ||
const HOME = homedir(); | ||
const logDir = path.join(HOME, 'logs'); | ||
|
||
// egg-script start | ||
// egg-script start ./server | ||
|
@@ -63,46 +73,47 @@ class StartCommand extends Command { | |
|
||
const isDaemon = argv.daemon; | ||
|
||
argv.framework = utils.getFrameworkPath({ | ||
argv.framework = yield this.getFrameworkPath({ | ||
framework: argv.framework, | ||
baseDir, | ||
}); | ||
|
||
const env = context.env; | ||
env.PWD = baseDir; | ||
env.HOME = homedir(); | ||
env.NODE_ENV = 'production'; | ||
|
||
// cli argv -> process.env.EGG_SERVER_ENV -> `undefined` then egg will use `prod` | ||
if (argv.env) { | ||
// if undefined, should not pass key due to `spwan`, https://github.com/nodejs/node/blob/master/lib/child_process.js#L470 | ||
env.EGG_SERVER_ENV = argv.env; | ||
argv.env = undefined; | ||
} | ||
|
||
const pkgInfo = require(path.join(baseDir, 'package.json')); | ||
const logDir = path.join(env.HOME, 'logs', pkgInfo.name); | ||
|
||
argv.title = argv.title || `egg-server-${pkgInfo.name}`; | ||
|
||
argv.stdout = argv.stdout || path.join(logDir, 'master-stdout.log'); | ||
argv.stderr = argv.stderr || path.join(logDir, 'master-stderr.log'); | ||
|
||
const env = context.env; | ||
env.HOME = HOME; | ||
env.NODE_ENV = 'production'; | ||
|
||
// adjust env for win | ||
let envPath = env.PATH || env.Path; | ||
const envPath = env.PATH || env.Path; | ||
if (envPath) { | ||
// for nodeinstall | ||
envPath = path.join(baseDir, 'node_modules/.bin') + path.delimiter + envPath; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个原来这样设置有啥用 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不是把 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 但是没加到 PATH There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 哈... 写漏了。这块当时还在想怎么写测试 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个测不了,nyc 有 hack There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 之前想过一种,调用 nodeinstall 下载一个 node 版本,然后 app 里面 print 下,但麻烦就放弃了。 |
||
env.PATH = path.join(baseDir, 'node_modules/.bin') + path.delimiter + envPath; | ||
} | ||
|
||
// for alinode | ||
env.ENABLE_NODE_LOG = 'YES'; | ||
env.NODE_LOG_DIR = env.NODE_LOG_DIR || path.join(logDir, 'alinode'); | ||
yield mkdirp(env.NODE_LOG_DIR); | ||
|
||
// cli argv -> process.env.EGG_SERVER_ENV -> `undefined` then egg will use `prod` | ||
if (argv.env) { | ||
// if undefined, should not pass key due to `spwan`, https://github.com/nodejs/node/blob/master/lib/child_process.js#L470 | ||
env.EGG_SERVER_ENV = argv.env; | ||
argv.env = undefined; | ||
} | ||
|
||
// remove unused properties, alias had been remove by `removeAlias` | ||
argv._ = undefined; | ||
argv.$0 = undefined; | ||
argv.daemon = undefined; | ||
|
||
const options = { | ||
cwd: baseDir, | ||
execArgv: context.execArgv, | ||
env, | ||
stdio: 'inherit', | ||
|
@@ -117,11 +128,11 @@ class StartCommand extends Command { | |
// whether run in the background. | ||
if (isDaemon) { | ||
this.logger.info(`save log file to ${logDir}`); | ||
const { stdout, stderr } = yield getRotatelog(logDir); | ||
const [ stdout, stderr ] = yield [ getRotatelog(argv.stdout), getRotatelog(argv.stderr) ]; | ||
options.stdio = [ 'ignore', stdout, stderr, 'ipc' ]; | ||
options.detached = true; | ||
|
||
const child = spawn('node', eggArgs, options); | ||
const child = this.child = spawn('node', eggArgs, options); | ||
child.on('message', msg => { | ||
if (msg && msg.action === 'egg-ready') { | ||
this.logger.info(`egg started on ${msg.data.address}`); | ||
|
@@ -135,32 +146,24 @@ class StartCommand extends Command { | |
this.helper.spawn('node', eggArgs, options); | ||
} | ||
} | ||
} | ||
|
||
function* getRotatelog(logDir) { | ||
const stdoutPath = path.join(logDir, 'master-stdout.log'); | ||
const stderrPath = path.join(logDir, 'master-stderr.log'); | ||
* getFrameworkPath(params) { | ||
return utils.getFrameworkPath(params); | ||
} | ||
|
||
// format style: .20150602.193100 | ||
const timestamp = moment().format('.YYYYMMDD.HHmmss'); | ||
} | ||
|
||
yield mkdirp(logDir); | ||
function* getRotatelog(logfile) { | ||
yield mkdirp(path.dirname(logfile)); | ||
|
||
/* istanbul ignore else */ | ||
if (yield fs.exists(stdoutPath)) { | ||
if (yield fs.exists(logfile)) { | ||
// format style: .20150602.193100 | ||
const timestamp = moment().format('.YYYYMMDD.HHmmss'); | ||
// Note: rename last log to next start time, not when last log file created | ||
yield fs.rename(stdoutPath, stdoutPath + timestamp); | ||
} | ||
|
||
/* istanbul ignore else */ | ||
if (yield fs.exists(stderrPath)) { | ||
yield fs.rename(stderrPath, stderrPath + timestamp); | ||
yield fs.rename(logfile, logfile + timestamp); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里其实如果能重命名为上次启动的时间,而不是本次的时间会更好一点,避免误导。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 标记为重启时间也还行 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 只是感觉会误导的,一般不都是 不过这里面应该也没什么太多有价值的 log。 |
||
} | ||
|
||
return yield { | ||
stdout: fs.open(stdoutPath, 'a'), | ||
stderr: fs.open(stderrPath, 'a'), | ||
}; | ||
return yield fs.open(logfile, 'a'); | ||
} | ||
|
||
module.exports = StartCommand; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PWD
不传了?看到下面 options 传了