Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6dd1594

Browse files
atian25popomore
authored andcommitted
docs: fix egg-scripts (eggjs#1552)
1 parent 46ed6fa commit 6dd1594

3 files changed

Lines changed: 46 additions & 44 deletions

File tree

docs/source/en/core/deployment.md

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,49 @@ Reusable package brings a few pros in:
2727

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

30-
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`.
30+
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`.
3131

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

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

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

38-
```js
39-
// dispatch.js
40-
const egg = require('egg');
40+
Then add `npm scripts` to `package.json`:
4141

42-
egg.startCluster({
43-
baseDir: __dirname,
44-
});
42+
```json
43+
{
44+
"scripts": {
45+
"start": "egg-scripts start --daemon",
46+
"stop": "egg-scripts stop"
47+
}
48+
}
4549
```
4650

47-
### Dispatch in background
51+
Then we are able to use `npm start` and `npm stop` to manage application.
52+
53+
> Note: `egg-scripts` don't support Windows.
4854
49-
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.
55+
### Start
5056

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

55-
IMPORTANT:
56-
- `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).
57-
- Launching directly when the application run in docker instance.
58-
- Egg usually initialize instances as many as cores, which can leverage the capability of the cpu.
61+
Options:
62+
63+
- `--port=7001` http server port, will use `process.env.PORT`, default to `7001`.
64+
- `--daemon` whether run at background, so you don't need `nohup`. Ignore this when the application run in docker instance.
65+
- `--env=prod` then framework env, will use `process.env.EGG_SERVER_ENV`, default to `prod`
66+
- `--workers=2` worker count, default to cpu cores, which can leverage the capability of the cpu.
67+
- `--title=egg-server-showcase` convenient for `ps + grep`, default to `egg-server-${appname}`.
68+
- `--framework=yadan` config `egg.framework` at `package.json` or pass this args, when you are using [Custom Framework](../advanced/framework.md).
5969

60-
### Dispatch with arguments
70+
More about [egg-scripts] and [egg-cluster] documents.
71+
72+
#### Dispatch with arguments
6173

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

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

7587
[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.
7688

77-
### Dispatch from extended framework
89+
### Stop
90+
91+
```bash
92+
$ egg-scripts stop
93+
```
94+
95+
This command will kill master process which will handler and notice worker and agent to gracefull exit.
7896

79-
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:
97+
Also you can manually call `ps -eo "pid,command" | grep "--type=egg-server"` to find master process then `kill` without `-9`.
8098

81-
```js
82-
// dispatch.js
83-
const path = require('path');
84-
const egg = require('egg');
85-
86-
egg.startCluster({
87-
baseDir: __dirname,
88-
customEgg: path.join(__dirname, 'node_modules/yadan'),
89-
});
90-
```
99+
[egg-cluster]: https://github.com/eggjs/egg-cluster
100+
[egg-scripts]: https://github.com/eggjs/egg-scripts
101+
[pm2]: https://github.com/Unitech/pm2

docs/source/zh-cn/core/cluster-and-ipc.md

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -197,22 +197,10 @@ module.exports = app => {
197197

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

200-
Master 进程的稳定性是极高的,线上运行时我们只需要在后台运行通过 `egg.startCluster` 启动的 Master 进程就可以了,不再需要使用 [pm2] 等进程守护模块。
201-
202-
```js
203-
// dispatch.js
204-
const egg = require('egg');
205-
206-
const workers = Number(process.argv[2] || require('os').cpus().length);
207-
egg.startCluster({
208-
workers,
209-
baseDir: __dirname,
210-
});
211-
```
200+
Master 进程的稳定性是极高的,线上运行时我们只需要通过 [egg-scripts] 后台运行通过 `egg.startCluster` 启动的 Master 进程就可以了,不再需要使用 [pm2] 等进程守护模块。
212201

213202
```bash
214-
# 后台运行 Master 进程
215-
$ EGG_SERVER_ENV=prod nohup node dispatch.js&
203+
$ egg-scripts start --daemon
216204
```
217205

218206
#### Agent
@@ -444,4 +432,5 @@ module.exports = agent => {
444432

445433
[pm2]: https://github.com/Unitech/pm2
446434
[egg-cluster]: https://github.com/eggjs/egg-cluster
435+
[egg-scripts]: https://github.com/eggjs/egg-scripts
447436
[graceful]: https://github.com/node-modules/graceful

docs/source/zh-cn/core/deployment.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ $ npm i egg-scripts --save
5151

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

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

5658
```bash

0 commit comments

Comments
 (0)