Skip to content

Commit

Permalink
docs, feat: add process signal docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Oct 17, 2017
1 parent d80d830 commit c79d4bd
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions idl/zh-cn/process.idl
Expand Up @@ -8,7 +8,7 @@
## 进程事件
process 模块对象是 EventEmitter 的实例,可以通过注册事件监听器响应进程级别的事件。

### beforeExit
### beforeExit 事件
**当 fibjs 的任务已经为空,并且没有额外的工作被添加进来,事件 `beforeExit` 会被触发**
```JavaScript
process.on('beforeExit', exitCode => {});
Expand All @@ -17,12 +17,28 @@

process.exitCode 作为唯一的参数值传递给 `beforeExit` 事件监听器的回调函数。如果进程由于显式的原因而将要终止,例如直接调用 process.exit 或抛出未捕获的异常,`beforeExit`事件不会被触发。

### exit
### exit 事件
**当 fibjs 退出时,事件 `exit` 会被触发,一旦所有与 `exit` 事件绑定的监听器执行完成,进程会终止**
```JavaScript
process.on('exit', exitCode => {});
```
`exit` 事件监听器的回调函数,只有一个入参,这个参数的值可以是 process.exitCode 的属性值,或者是调用 process.exit 方法时传入的 `exitCode` 值。

### Signal 事件
**当 fibjs 进程接收到一个信号时,会触发信号事件,目前支持的信号有 SIGINT 和 SIGTERM。每个事件名称,以信号名称的大写表示 (比如事件'SIGINT' 对应信号 SIGINT)。**

信号事件不同于其它进程事件,信号事件是抢占的,当信号发生时,无论当前在 io 操作,还是 JavaScript 运算,都会尽快触发相应事件。比如你可以用下面的代码,中断当前应用,并输出运行状态:
```JavaScript
var coroutine = require('coroutine');

process.on('SIGINT', () => {
coroutine.fibers.forEach(f => console.error("Fiber %d:\n%s", f.id, f.stack));
process.exit();
});
```
信号名称及其意义如下:
* SIGINT:在终端运行时,可以被所有平台支持,通常可以通过 CTRL+C 触发。
* SIGTERM:当进程被 kill 时触发此信号。Windows 下不支持。
*/
module process : EventEmitter
{
Expand Down

0 comments on commit c79d4bd

Please sign in to comment.