打开createReadStream时,Node.js process.exit()将不会退出 [英] Node.js process.exit() will not exit with a createReadStream open

查看:146
本文介绍了打开createReadStream时,Node.js process.exit()将不会退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,可以通过EAGI与Asterisk通讯. Asterisk打开我的Node.js应用程序,并通过STDIN发送数据,程序通过STDOUT发送Asterisk命令.当用户挂断电话时,Node.js进程将收到SIGHUP命令.这被拦截以使清洁器退出.此功能正常工作.

I have a program that communicates with Asterisk via EAGI. Asterisk opens up my Node.js application and sends it data via STDIN and the program sends Asterisk commands via STDOUT. When a user hangs up, the Node.js process gets sent a SIGHUP command. This is intercepted for cleaner exiting. This functionality is working.

星号还会在fd 3(STDERR + 1)上发送RAW音频数据. Node.js进程会正确拦截数据,并能够读取音频,将其转换或进行其他任何需要完成的操作.但是,当在fd 3上创建createReadStream时,Node.js进程将不会退出,并迅速变为僵尸.如果我注释掉createReadStream代码,则Node.js会按预期退出.

Asterisk also sends RAW audio data on fd 3 (STDERR+1). The Node.js process intercepts the data properly, and is able to read the audio, convert it, or anything else that needs to be done. However, when the createReadStream is created on fd 3, the Node.js process will NOT exit and quickly becomes a Zombie. If I comment out the createReadStream code, Node.js exits as expected.

如何让Node.js像预期的那样通过process.exit()函数退出?我正在使用Node.js版本v0.10.30.

How can I get Node.js to exit with the process.exit() function like it's supposed to? I'm using Node.js version v0.10.30.

Node.js createReadStream代码:

Node.js createReadStream code:

// It was success
this.audioInStream = fs.createReadStream( null, { 'fd' : 3 } );

// Pipe the audio stream to a blackhole for now so it doesn't get queued up
this.audioInStream.pipe( blackhole() );

SIGHUP代码:

process
.on( 'SIGHUP', function() {
    log.message.info( "[%s] Asterisk process hung up.", that.callerid );
    that.exitWhenReady();
} );

exitWhenReady函数

exitWhenReady function

Index.prototype.exitWhenReady = function() {
    if( !this.canExit )
        return;

    log.message.info( "[%s] Exiting program successfully.", this.callerid );

    // Get rid of our streams
    this.audioInStream.unpipe();
    this.audioInStream.close();
    this.audioInStream.destroy();

    process.exit( 0 );
};

黑洞模块:

var inherits = require( 'util' ).inherits;
var Writable = require( 'stream' ).Writable;
var process = require( 'process' );

function Blackhole( opts ) {
    if( !(this instanceof Blackhole) )
        return( new Blackhole( opts ) );

    if( !opts )
        opts = {};

    Writable.call( this, opts );
}

inherits( Blackhole, Writable );

Blackhole.prototype._write = function( chunk, encoding, done ) {
    process.nextTick( done );
};

module.exports = Blackhole;

值得注意的是

星号进程挂起

Asterisk process hung up

还有

退出程序成功.

Exiting program successfully.

当createReadStream读取fd 3时,从不显示在日志文件中,但如果不是,则不会显示.

Never show up in log file when createReadStream is reading fd 3, but when it isn't they do.

推荐答案

我发现,钩住SIGHUP并打开fd 3会使程序即使在调用process.exit()时也无法关闭.这真的很奇怪.

I found that hooking the SIGHUP AND having fd 3 open caused the program to not close even when process.exit() was called. This was really strange.

我要解决此问题的方法是监听流程的退出"事件.在退出"事件中,我使用SIGTERM手动杀死了自己的进程.这足以停止整个程序.我发现这实际上甚至与Winston记录器异常记录器一起工作也很好. Winston可以将异常写入日志文件,然后成功退出.

What I did to fix this issue was listen to the process' "exit" event. In the "exit" event I manually killed my own process with a SIGTERM. This was sufficient for stopping the whole program. I found that this actually worked well with the Winston logger exception logger even. Winston would be able to write the exception to a log file and then exit successfully.

结果代码:

process
.on( 'SIGHUP', function() {
    log.message.info( "[%s] Asterisk process hung up.", that.callerid );
    that.exitWhenReady( true );
} )
.on( 'exit', function() {
    process.kill( process.pid, 'SIGTERM' );
} );

发送SIGHUP时,上述函数基本上会调用exitWhenReady().那会检查所有任务是否完成,一旦所有任务完成,它将调用"process.exit()",该函数将在上述事件上调用该函数.

The above function basically calls the exitWhenReady() when a SIGHUP is sent. That checks to see that all tasks are complete and once all tasks are complete it'll call "process.exit()" which calls the function on the above event.

我希望这对某人有帮助.

I hope this helps somebody.

这篇关于打开createReadStream时,Node.js process.exit()将不会退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆