Skip to content

Commit

Permalink
docs: fix egg-scripts (#1552)
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 authored and popomore committed Oct 20, 2017
1 parent 46ed6fa commit 6dd1594
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 44 deletions.
71 changes: 41 additions & 30 deletions docs/source/en/core/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,49 @@ Reusable package brings a few pros in:

Node.js(`>= 6.0.0`) is required so that you should make sure it is pre-installed in runtime environment.

Egg takes `egg-cluster` to create [Master](https://github.com/eggjs/egg/blob/master/docs/source/en/core/cluster-and-ipc.md#master) process, which you can rely on to secure the application instead of daemon manager like [pm2](https://github.com/Unitech/pm2). The API is also really convenient for developers to achieve that, just `egg.startCluster`.
Egg takes `egg-cluster` to create [Master](https://github.com/eggjs/egg/blob/master/docs/source/en/core/cluster-and-ipc.md#master) process, which you can rely on to secure the application instead of daemon manager like [pm2]. The API is also really convenient for developers to achieve that, just `egg.startCluster`.

More about [egg-cluster](https://github.com/eggjs/egg-cluster#options)
And framework also provide [egg-scripts] for developers to start/stop application at prod mode.

### Dispatch file
Firstly, we need to import `egg-scripts` as `dependencies`:

Create a new dispatch file in the root directory, for instance `dispatch.js`:
```bash
$ npm i egg-scripts --save
```

```js
// dispatch.js
const egg = require('egg');
Then add `npm scripts` to `package.json`:

egg.startCluster({
baseDir: __dirname,
});
```json
{
"scripts": {
"start": "egg-scripts start --daemon",
"stop": "egg-scripts stop"
}
}
```

### Dispatch in background
Then we are able to use `npm start` and `npm stop` to manage application.

> Note: `egg-scripts` don't support Windows.
Once you create dispatch file, you can launch the application and redirect standard output to `stdout.log` as well as error details to `stderr.log`, which are really useful for debugging later.
### Start

```bash
$ EGG_SERVER_ENV=prod nohup node dispatch.js > stdout.log 2> stderr.log &
$ egg-scripts start --port=7001 --daemon --title=egg-server-showcase
```

IMPORTANT:
- `EGG_SERVER_ENV` in production must be `prod`, for more information about [runtime environment](https://github.com/eggjs/egg/blob/master/docs/source/en/basics/env.md).
- Launching directly when the application run in docker instance.
- Egg usually initialize instances as many as cores, which can leverage the capability of the cpu.
Options:

- `--port=7001` http server port, will use `process.env.PORT`, default to `7001`.
- `--daemon` whether run at background, so you don't need `nohup`. Ignore this when the application run in docker instance.
- `--env=prod` then framework env, will use `process.env.EGG_SERVER_ENV`, default to `prod`
- `--workers=2` worker count, default to cpu cores, which can leverage the capability of the cpu.
- `--title=egg-server-showcase` convenient for `ps + grep`, default to `egg-server-${appname}`.
- `--framework=yadan` config `egg.framework` at `package.json` or pass this args, when you are using [Custom Framework](../advanced/framework.md).

### Dispatch with arguments
More about [egg-scripts] and [egg-cluster] documents.

#### Dispatch with arguments

Arguments of dispatch can be configured in `config.{env}.js`.

Expand All @@ -74,17 +86,16 @@ exports.cluster = {

[server.listen](https://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback) supports arguments including `path`, `port` and `hostname` to change dispatching behavior. One thing you should know is that the `port` in `egg.startCluster` will override the one in application config.

### Dispatch from extended framework
### Stop

```bash
$ egg-scripts stop
```

This command will kill master process which will handler and notice worker and agent to gracefull exit.

What about dispatch from your framework extended from [Egg's Application](https://github.com/eggjs/egg/blob/master/docs/source/en/advanced/framework.md) like `yadan`? Egg offers `customEgg` to change the entry:
Also you can manually call `ps -eo "pid,command" | grep "--type=egg-server"` to find master process then `kill` without `-9`.

```js
// dispatch.js
const path = require('path');
const egg = require('egg');

egg.startCluster({
baseDir: __dirname,
customEgg: path.join(__dirname, 'node_modules/yadan'),
});
```
[egg-cluster]: https://github.com/eggjs/egg-cluster
[egg-scripts]: https://github.com/eggjs/egg-scripts
[pm2]: https://github.com/Unitech/pm2
17 changes: 3 additions & 14 deletions docs/source/zh-cn/core/cluster-and-ipc.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,22 +197,10 @@ module.exports = app => {

在这个模型下,Master 进程承担了进程管理的工作(类似 [pm2]),不运行任何业务代码,我们只需要运行起一个 Master 进程它就会帮我们搞定所有的 Worker、Agent 进程的初始化以及重启等工作了。

Master 进程的稳定性是极高的,线上运行时我们只需要在后台运行通过 `egg.startCluster` 启动的 Master 进程就可以了,不再需要使用 [pm2] 等进程守护模块。

```js
// dispatch.js
const egg = require('egg');

const workers = Number(process.argv[2] || require('os').cpus().length);
egg.startCluster({
workers,
baseDir: __dirname,
});
```
Master 进程的稳定性是极高的,线上运行时我们只需要通过 [egg-scripts] 后台运行通过 `egg.startCluster` 启动的 Master 进程就可以了,不再需要使用 [pm2] 等进程守护模块。

```bash
# 后台运行 Master 进程
$ EGG_SERVER_ENV=prod nohup node dispatch.js&
$ egg-scripts start --daemon
```

#### Agent
Expand Down Expand Up @@ -444,4 +432,5 @@ module.exports = agent => {

[pm2]: https://github.com/Unitech/pm2
[egg-cluster]: https://github.com/eggjs/egg-cluster
[egg-scripts]: https://github.com/eggjs/egg-scripts
[graceful]: https://github.com/node-modules/graceful
2 changes: 2 additions & 0 deletions docs/source/zh-cn/core/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ $ npm i egg-scripts --save

这样我们就可以通过 `npm start``npm stop` 命令启动或停止应用。

> 注意:`egg-scripts` 不支持 Windows 系统。
### 启动命令

```bash
Expand Down

0 comments on commit 6dd1594

Please sign in to comment.