Skip to content

Commit c79d4bd

Browse files
committed
docs, feat: add process signal docs.
1 parent d80d830 commit c79d4bd

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

idl/zh-cn/process.idl

+18-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
## 进程事件
99
process 模块对象是 EventEmitter 的实例,可以通过注册事件监听器响应进程级别的事件。
1010

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

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

20-
### exit
20+
### exit 事件
2121
**当 fibjs 退出时,事件 `exit` 会被触发,一旦所有与 `exit` 事件绑定的监听器执行完成,进程会终止**
2222
```JavaScript
2323
process.on('exit', exitCode => {});
2424
```
2525
`exit` 事件监听器的回调函数,只有一个入参,这个参数的值可以是 process.exitCode 的属性值,或者是调用 process.exit 方法时传入的 `exitCode` 值。
26+
27+
### Signal 事件
28+
**当 fibjs 进程接收到一个信号时,会触发信号事件,目前支持的信号有 SIGINT 和 SIGTERM。每个事件名称,以信号名称的大写表示 (比如事件'SIGINT' 对应信号 SIGINT)。**
29+
30+
信号事件不同于其它进程事件,信号事件是抢占的,当信号发生时,无论当前在 io 操作,还是 JavaScript 运算,都会尽快触发相应事件。比如你可以用下面的代码,中断当前应用,并输出运行状态:
31+
```JavaScript
32+
var coroutine = require('coroutine');
33+
34+
process.on('SIGINT', () => {
35+
coroutine.fibers.forEach(f => console.error("Fiber %d:\n%s", f.id, f.stack));
36+
process.exit();
37+
});
38+
```
39+
信号名称及其意义如下:
40+
* SIGINT:在终端运行时,可以被所有平台支持,通常可以通过 CTRL+C 触发。
41+
* SIGTERM:当进程被 kill 时触发此信号。Windows 下不支持。
2642
*/
2743
module process : EventEmitter
2844
{

0 commit comments

Comments
 (0)