我有一个通过 EAGI 与 Asterisk 通信的程序。 Asterisk 打开我的 Node.js 应用程序并通过 STDIN 向其发送数据,程序通过 STDOUT 发送 Asterisk 命令。当用户挂断电话时,Node.js 进程会收到 SIGHUP 命令。这是为了清理者退出而拦截的。此功能正在运行。
Asterisk 还在 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() );
信号代码:
process
.on( 'SIGHUP', function() {
log.message.info( "[%s] Asterisk process hung up.", that.callerid );
that.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;
值得注意的是
星号进程挂起
And
成功退出程序。
当 createReadStream 正在读取 fd 3 时,永远不会出现在日志文件中,但当不是时,它们就会出现。