redis-server 离线崩溃节点
redis-server offline crashes node
我在这里玩得很开心。我已经阅读了这么多文档,以及其他人关于同一主题的问题,但找不到如何阻止这种情况发生的答案。我故意关闭 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。然后我调出一个终端 window 并输入 "redis-cli shutdown" 以强制 redis 服务器离线。一旦我这样做,node.js 终端 window 就会一遍又一遍地循环同样的错误。
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('error', ... 处理程序,但每次服务器只是坐在那里循环说有一个 "Unhandled 'error' event"
有人可以帮我解决这个问题吗?我不明白这怎么可能是未处理的错误,出于某种原因,这在我看来没有意义。
预先感谢您提供的任何可能的帮助。我已经在互联网上浏览了几个小时,试图找到任何其他可能的选择,他们都说同样的话,使用 io.on('error', 或 io.on('uncaughtException',或 process.on('uncaughtException',我拥有它们,但它仍然出错并使节点崩溃。
感谢@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
}
我用这条线解决了这个问题:
io.of('/').adapter.on('error', function(error){
console.log('error: ', error);
});
我在这里玩得很开心。我已经阅读了这么多文档,以及其他人关于同一主题的问题,但找不到如何阻止这种情况发生的答案。我故意关闭 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。然后我调出一个终端 window 并输入 "redis-cli shutdown" 以强制 redis 服务器离线。一旦我这样做,node.js 终端 window 就会一遍又一遍地循环同样的错误。
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('error', ... 处理程序,但每次服务器只是坐在那里循环说有一个 "Unhandled 'error' event" 有人可以帮我解决这个问题吗?我不明白这怎么可能是未处理的错误,出于某种原因,这在我看来没有意义。 预先感谢您提供的任何可能的帮助。我已经在互联网上浏览了几个小时,试图找到任何其他可能的选择,他们都说同样的话,使用 io.on('error', 或 io.on('uncaughtException',或 process.on('uncaughtException',我拥有它们,但它仍然出错并使节点崩溃。
感谢@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
}
我用这条线解决了这个问题:
io.of('/').adapter.on('error', function(error){
console.log('error: ', error);
});