ioredis
ioredis copied to clipboard
Redis connection isn't retrying if it fails on initial connection
Hey folks,
I'm running into an issue where if I try to connect to a Redis instance and it's not online ioredis doesn't try to reconnect to Redis. If it succeeds in the initial connection and the disconnect afterwards it does correctly retry and eventually recover.
This is what my code looks like:
function createRedis(name = 'unknown') {
const connection = new Redis(config.redis.url, {
reconnectOnError(error) {
console.log(`Redis \`${name}\` errored occured. Reconnecting...`, error);
return true;
},
});
connection.on('connect', () => {
console.log(`Redis \`${name}\` connected`);
});
connection.on('ready', () => {
console.log(`Redis \`${name}\` is ready`);
});
connection.on('redis', () => {
console.log(`Redis \`${name}\` is ready`);
});
connection.on('error', (error) => {
console.error(`Redis \`${name}\` experienced an error`, error);
if (error.name === 'MaxRetriesPerRequestError') {
console.error(`Critical Redis error: ${error.message}. Shutting down.`);
process.exit(1);
}
});
connection.on('reconnecting', () => {
console.log(`Redis \`${name}\` is reconnecting`);
});
return connection;
}
Hey @avigoldman 👋
I tried this but it actually works for me. When the server is not started and I call createRedis, it shows "is reconnecting" and once I launch the server, it shows "is ready" successfully. Anything I missed?