Node.js process.exit()不会在createReadStream打开的情况下退出
我有一个通过EAGI与Asterisk进行通信的程序。 Asterisk打开我的Node.js应用程序,并通过STDIN发送数据,程序通过STDOUT发送Asterisk命令。当用户挂断电话时,Node.js进程将收到SIGHUP命令。这被拦截以便清洁器退出。此功能正在运行。
星号还会在fd 3(STDERR + 1)上发送RAW音频数据。 Node.js进程会正确拦截数据,并且能够读取音频,将其转换或进行其他任何需要完成的操作。但是,当在fd 3上创建createReadStream时,Node.js进程将不会退出,而是迅速成为僵尸。如果我注释掉createReadStream代码,则Node.js会按预期退出。
如何使Node.js像预期的那样通过process.exit()函数退出?我正在使用Node.js版本v0.10.30。
Node.js createReadStream代码:
// 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函数
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;
值得注意的是
星号进程挂断
和
成功退出程序。
当createReadStream读取fd 3时从不显示在日志文件中,但是如果不是,则不这样做。
回答如下:我发现钩住SIGHUP并打开fd 3导致即使调用process.exit()时程序也无法关闭。这真的很奇怪。
我为解决此问题所做的工作是监听流程的“退出”事件。在“退出”事件中,我使用SIGTERM手动杀死了自己的进程。这足以停止整个程序。我发现这实际上甚至与Winston记录器异常记录器一起工作也很好。 Winston可以将异常写入日志文件,然后成功退出。
结果代码:
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()”,该函数将在上述事件上调用该函数。
我希望这对某人有帮助。
Node.js process.exit()不会在createReadStream打开的情况下退出
我有一个通过EAGI与Asterisk进行通信的程序。 Asterisk打开我的Node.js应用程序,并通过STDIN发送数据,程序通过STDOUT发送Asterisk命令。当用户挂断电话时,Node.js进程将收到SIGHUP命令。这被拦截以便清洁器退出。此功能正在运行。
星号还会在fd 3(STDERR + 1)上发送RAW音频数据。 Node.js进程会正确拦截数据,并且能够读取音频,将其转换或进行其他任何需要完成的操作。但是,当在fd 3上创建createReadStream时,Node.js进程将不会退出,而是迅速成为僵尸。如果我注释掉createReadStream代码,则Node.js会按预期退出。
如何使Node.js像预期的那样通过process.exit()函数退出?我正在使用Node.js版本v0.10.30。
Node.js createReadStream代码:
// 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函数
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;
值得注意的是
星号进程挂断
和
成功退出程序。
当createReadStream读取fd 3时从不显示在日志文件中,但是如果不是,则不这样做。
回答如下:我发现钩住SIGHUP并打开fd 3导致即使调用process.exit()时程序也无法关闭。这真的很奇怪。
我为解决此问题所做的工作是监听流程的“退出”事件。在“退出”事件中,我使用SIGTERM手动杀死了自己的进程。这足以停止整个程序。我发现这实际上甚至与Winston记录器异常记录器一起工作也很好。 Winston可以将异常写入日志文件,然后成功退出。
结果代码:
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()”,该函数将在上述事件上调用该函数。
我希望这对某人有帮助。