Ole*_*gas 18 unix-socket node.js forever
我有一个NodeJS应用程序,它设置一个UNIX套接字来公开一些进程间通信通道(某种监视的东西).UNIX套接字文件放在os.tmpdir()
文件夹中(即/tmp/app-monitor.sock
).
var net = require('net');
var server = net.createServer(...);
server.listen('/tmp/app-monitor.sock', ...);
Run Code Online (Sandbox Code Playgroud)
我使用信号处理(SIGINT,SITERM等...)来正常关闭我的服务器并删除套接字文件.
function shutdown() {
server.close(); // socket file is automatically removed here
process.exit();
}
process.on('SIGINT', shutdown);
// and so on
Run Code Online (Sandbox Code Playgroud)
我的应用程序正在运行forever start ...
以监视它的生命周期.
我的forever restartall
命令有问题.当永远这样做时restartall
,使用a SIGKILL
来终止所有子进程.SIGKILL
无法通过进程处理,因此我的应用程序在没有任何关闭过程的情况下死亡
问题是套接字文件在SIGKILL
使用时不会被删除.重新启动子进程后,无法启动新服务器导致' listen
调用将导致EADDRINUSE
错误.
我无法在应用启动过程中删除现有的套接字文件导致'我不知道它是真正的工作套接字还是先前不干净关闭的痕迹.
所以,问题是......处理这种情况的更好方法是什么(SIGKILL和UNIX-socket服务器)?
Old*_*Pro 40
正如其他人提到的那样,你不能做任何回应SIGKILL的事情,这通常是为什么forever
(以及其他所有人)不应该使用SIGKILL,除非在极端情况下.所以你能做的最好就是在另一个过程中清理.
我建议你在开始时清理一下.当你得到EADDRINUSE
,尝试连接到套接字.如果套接字连接成功,则另一台服务器正在运行,因此该实例应该退出.如果连接失败,则可以安全地取消链接套接字文件并创建一个新文件.
var fs = require('fs');
var net = require('net');
var server = net.createServer(function(c) { //'connection' listener
console.log('server connected');
c.on('end', function() {
console.log('server disconnected');
});
c.write('hello\r\n');
c.pipe(c);
});
server.on('error', function (e) {
if (e.code == 'EADDRINUSE') {
var clientSocket = new net.Socket();
clientSocket.on('error', function(e) { // handle error trying to talk to server
if (e.code == 'ECONNREFUSED') { // No other server listening
fs.unlinkSync('/tmp/app-monitor.sock');
server.listen('/tmp/app-monitor.sock', function() { //'listening' listener
console.log('server recovered');
});
}
});
clientSocket.connect({path: '/tmp/app-monitor.sock'}, function() {
console.log('Server running, giving up...');
process.exit();
});
}
});
server.listen('/tmp/app-monitor.sock', function() { //'listening' listener
console.log('server bound');
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8744 次 |
最近记录: |