redis-server offline崩溃节点

时间:2016-04-28 17:04:15

标签: javascript node.js socket.io socket.io-redis redis-server

我在这里度过了一段时间。我已经阅读了大量文档和其他人对同一主题的问题,但无法找到如何阻止这种情况发生的答案。我故意关闭redis服务器,以确保应用程序在生产过程中不会完全核,但我做的任何事情似乎都不会阻止它发生。这是我的代码现在的一个非常简单的例子......

var cluster = require('cluster');
var express = require('express');
var http = require('http');
var redis = require('redis');
var redisAdapter = require('socket.io-redis');
var sticky = require('sticky-session');
var port = process.env.PORT || 3333;
var workers = 3;
var app = express();
var server = http.createServer(app);
var io = require('socket.io')(server);

if(!sticky.listen(server, 3333, {workers: workers})) {
    server.once('listening', () => {
        console.log('server started on port ' + port);
        process.on('exit', () => {
            io.emit('error', {errorType: 'SERVER_SHUTDOWN'});
        });
        process.on('uncaughtException', (err) => {
            console.log('uncaught exception occurred');
            console.log(err);
        });
    });
} else {
    addRedisAdapter(io);
    addIOEventHandler(io);
}

function addRedisAdapter(io) {
    var redisUrl = process.env.REDISTOGO_URL || 'redis://127.0.0.1:6379';
    var redisOptions = require('parse-redis-url')(redis).parse(redisUrl);
    var pub = redis.createClient(redisOptions.port, redisOptions.host, { returnBuffers: true});
    var sub = redis.createClient(redisOptions.port, redisOptions.host, {returnBuffers: true});
    io.on('error', (err) => {
        console.log(err);
    });
    io.on('uncaughtException', (err) => {
        console.log(err);
    });
    io.adapter(redisAdapter({pubClient: pub, subClient: sub}));
    console.log('redis adapter started');
}
function addIOEventHandler(io) {
    //all the events for io
}

现在,我启动redis-server,然后用这个服务器启动node.js。然后我打开一个终端窗口,输入" redis-cli shutdown"强制redis服务器下线。一旦我这样做,node.js终端窗口就会一遍又一遍地循环遍历相同的错误。

events.js:154
  throw er; // Unhandled 'error' event
  ^

Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
    at Object.exports._errnoException (util.js:893:11)
    at exports._exceptionWithHostPort (util.js:916:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1075:14)
redis adapter started with url: redis://127.0.0.1:6379

我有两个process.on(' uncaughtException' ...和io.on(' uncaughtException' ...以及io.on('错误& #39;,...处理程序,但每次服务器只是坐在那里循环说有一个"未处理的'错误'事件" 有人可以帮帮我吗。我不知道这可能是一个未处理的错误,但出于某种原因,它在我的脑海里并没有意义。 提前感谢您提供任何可能的帮助。我一直在寻找互联网几个小时试图寻找任何其他可能的选择,他们都说同样的事情,使用io.on('错误'或io.on(' uncaughtException&#39) ;或者是process.on(' uncaughtException',我拥有它们并且它仍然出错并崩溃节点。

2 个答案:

答案 0 :(得分:3)

感谢@SadiRubaiyet指出我正确的方向。我不得不向pub和sub添加一个错误事件。一旦我这样做,当redis关闭时不再有节点崩溃。这是编辑过的代码......

var cluster = require('cluster');
var express = require('express');
var http = require('http');
var redis = require('redis');
var redisAdapter = require('socket.io-redis');
var sticky = require('sticky-session');
var port = process.env.PORT || 3333;
var workers = 3;
var app = express();
var server = http.createServer(app);
var io = require('socket.io')(server);

if(!sticky.listen(server, 3333, {workers: workers})) {
    server.once('listening', () => {
        console.log('server started on port ' + port);
        process.on('exit', () => {
            io.emit('error', {errorType: 'SERVER_SHUTDOWN'});
        });
        process.on('uncaughtException', (err) => {
            console.log('uncaught exception occurred');
            console.log(err);
        });
    });
} else {
    addRedisAdapter(io);
    addIOEventHandler(io);
}

function addRedisAdapter(io) {
    var redisUrl = process.env.REDISTOGO_URL || 'redis://127.0.0.1:6379';
    var redisOptions = require('parse-redis-url')(redis).parse(redisUrl);
    var pub = redis.createClient(redisOptions.port, redisOptions.host, { returnBuffers: true});
    var sub = redis.createClient(redisOptions.port, redisOptions.host, {returnBuffers: true});
    io.on('error', (err) => {
        console.log(err);
    });
    io.on('uncaughtException', (err) => {
        console.log(err);
    });
    /*HERE IS THE NEW CODE THAT MADE IT WORK*/
    pub.on('error', (err) => {
        console.log('error from pub');
        console.log(err);
    });
    sub.on('error', (err) => {
        console.log('error from sub);
        console.log(err);
    });
    /*END*/
    io.adapter(redisAdapter({pubClient: pub, subClient: sub}));
    console.log('redis adapter started');
}
function addIOEventHandler(io) {
    //all the events for io
}

答案 1 :(得分:0)

我使用此行解决了此问题:

 io.of('/').adapter.on('error', function(error){
          console.log('error: ', error);            
       });