-
Notifications
You must be signed in to change notification settings - Fork 37
feat: [BREAKING_CHANGE] check the status of app when start on daemon #9
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
2cbc410
05386bd
e83592a
cc465dc
5e773be
bc8dbe9
ded4b50
9235c39
3a11919
a3ce0df
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 |
---|---|---|
@@ -1,2 +1 @@ | ||
test/fixtures | ||
coverage |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
logs/ | ||
npm-debug.log | ||
node_modules/ | ||
/node_modules | ||
coverage/ | ||
.idea/ | ||
run/ | ||
.DS_Store | ||
*.swp | ||
!test/fixtures/example/node_modules | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,11 @@ | |
|
||
const path = require('path'); | ||
const mkdirp = require('mz-modules/mkdirp'); | ||
const sleep = require('mz-modules/sleep'); | ||
const homedir = require('node-homedir'); | ||
const utils = require('egg-utils'); | ||
const fs = require('mz/fs'); | ||
const { exec } = require('mz/child_process'); | ||
const moment = require('moment'); | ||
const spawn = require('child_process').spawn; | ||
const Command = require('../command'); | ||
|
@@ -33,7 +35,7 @@ class StartCommand extends Command { | |
default: process.env.PORT, | ||
}, | ||
env: { | ||
description: 'egg server env, default to `process.env.EGG_SERVER_ENV`', | ||
description: 'server env, default to `process.env.EGG_SERVER_ENV`', | ||
default: process.env.EGG_SERVER_ENV, | ||
}, | ||
framework: { | ||
|
@@ -52,6 +54,11 @@ class StartCommand extends Command { | |
description: 'A file that stderr redirect to', | ||
type: 'string', | ||
}, | ||
timeout: { | ||
description: 'a timeout for start when daemon', | ||
type: 'number', | ||
default: 300 * 1000, | ||
}, | ||
}; | ||
} | ||
|
||
|
@@ -78,6 +85,8 @@ class StartCommand extends Command { | |
baseDir, | ||
}); | ||
|
||
this.frameworkName = yield this.getFrameworkName(argv.framework); | ||
|
||
const pkgInfo = require(path.join(baseDir, 'package.json')); | ||
argv.title = argv.title || `egg-server-${pkgInfo.name}`; | ||
|
||
|
@@ -120,27 +129,32 @@ class StartCommand extends Command { | |
detached: false, | ||
}; | ||
|
||
this.logger.info(`starting egg application at ${baseDir}`); | ||
this.logger.info('Starting %s application at %s', this.frameworkName, baseDir); | ||
|
||
const eggArgs = [ this.serverBin, JSON.stringify(argv), `--title=${argv.title}` ]; | ||
this.logger.info('run node %s', eggArgs.join(' ')); | ||
this.logger.info('Run node %s', eggArgs.join(' ')); | ||
|
||
// whether run in the background. | ||
if (isDaemon) { | ||
this.logger.info(`save log file to ${logDir}`); | ||
this.logger.info(`Save log file to ${logDir}`); | ||
const [ stdout, stderr ] = yield [ getRotatelog(argv.stdout), getRotatelog(argv.stderr) ]; | ||
options.stdio = [ 'ignore', stdout, stderr, 'ipc' ]; | ||
options.detached = true; | ||
|
||
const child = this.child = spawn('node', eggArgs, options); | ||
this.isReady = false; | ||
child.on('message', msg => { | ||
if (msg && msg.action === 'egg-ready') { | ||
this.logger.info(`egg started on ${msg.data.address}`); | ||
this.isReady = true; | ||
this.logger.info('%s started on %s', this.frameworkName, msg.data.address); | ||
child.unref(); | ||
child.disconnect(); | ||
process.exit(0); | ||
} | ||
}); | ||
|
||
// check start status | ||
yield this.checkStatus(argv); | ||
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. 哪里读文件了? |
||
} else { | ||
// signal event had been handler at common-bin helper | ||
this.helper.spawn('node', eggArgs, options); | ||
|
@@ -151,6 +165,52 @@ class StartCommand extends Command { | |
return utils.getFrameworkPath(params); | ||
} | ||
|
||
* getFrameworkName(framework) { | ||
const pkgPath = path.join(framework, 'package.json'); | ||
let name = 'egg'; | ||
try { | ||
const pkg = require(pkgPath); | ||
if (pkg.name) name = pkg.name; | ||
} catch (_) { | ||
/* istanbul next */ | ||
} | ||
return name; | ||
} | ||
|
||
* checkStatus({ stderr, timeout }) { | ||
let count = 0; | ||
let isSuccess = true; | ||
timeout = timeout / 1000; | ||
while (!this.isReady) { | ||
try { | ||
const stat = yield fs.stat(stderr); | ||
if (stat && stat.size > 0) { | ||
const [ stdout ] = yield exec('tail -n 100 ' + stderr); | ||
this.logger.error(stdout); | ||
this.logger.error('Start failed, see %s', stderr); | ||
isSuccess = false; | ||
break; | ||
} | ||
} catch (_) { | ||
// nothing | ||
} | ||
|
||
if (count >= timeout) { | ||
this.logger.error('Start failed, %ds timeout', timeout); | ||
isSuccess = false; | ||
break; | ||
} | ||
|
||
yield sleep(1000); | ||
this.logger.log('Wait Start: %d...', ++count); | ||
} | ||
|
||
if (!isSuccess) { | ||
this.child.kill('SIGTERM'); | ||
yield sleep(1000); | ||
process.exit(1); | ||
} | ||
} | ||
} | ||
|
||
function* getRotatelog(logfile) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,5 @@ | |
exports.cluster = { | ||
listen: { | ||
port: 8000, | ||
} | ||
} | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict'; | ||
|
||
exports.keys = '123456'; | ||
|
||
exports.logger = { | ||
level: 'WARN', | ||
consoleLevel: 'WARN', | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "example", | ||
"dependencies": { | ||
"egg": "^1.0.0" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
|
||
const sleep = require('mz-modules/sleep'); | ||
|
||
module.exports = app => { | ||
if (process.env.ERROR) { | ||
app.logger.error(new Error(process.env.ERROR)); | ||
} | ||
|
||
app.beforeStart(function* () { | ||
yield sleep(process.env.WAIT_TIME); | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict'; | ||
|
||
exports.keys = '123456'; | ||
|
||
exports.logger = { | ||
level: 'WARN', | ||
consoleLevel: 'WARN', | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "example", | ||
"version": "1.0.0", | ||
"dependencies": { | ||
"egg": "^1.0.0" | ||
}, | ||
"egg": { | ||
"framework": "custom-framework" | ||
} | ||
} |
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.
算不算 break?之前好像有人在 win 使用。