Check if redis is running -> node js

萝らか妹 提交于 2021-01-20 18:11:26

问题


I just wanted to know, at the beginning of my NodeJS process, if Redis is started or not (so users session will be stored or not).

Here is what I have for the moment :

var session = require('express-session');
var RedisStore = require('connect-redis')(session);
var redis = require("redis");
var client = redis.createClient(global.redis.host, global.redis.port);

// Check if redis is running
var redisIsReady = false;
client.on('error', function(err) {
    redisIsReady = false;
    console.log('redis is not running');
    console.log(err);
});
client.on('ready', function() {
    redisIsReady = true;
    console.log('redis is running');
});

// Here I use express-session, but I want to set a store only if redis is ready
    sessOptions = {
        [...]
    }
    // Store setting
    if (redisIsReady) {
        sessOptions.store = new RedisStore({
            host: global.redis.host,
            port: global.redis.port
        });
    } else {
        console.log("redis is not running - sessions won't be written to disk");
    }
// app.use(session(sessOptions))

Here is what it's output in both cases (when Redis is running or not) :

redis is not running - sessions won't be written to disk
redis is not running
Error: Redis connection to 6379:localhost failed - connect ENOENT

So I have 2 questions :

  1. How can I do to check if redis is running before setting my SessionStore (is there anyway to check synchronously if redis is running) ?

  2. Why does it giving me an error even when redis is running ?

THANKS!


Note : I'm using default host/port (localhost, 6379) and the RedisStore works as expected.

Note 2 : I'm using Windows... but don't be affraid, it should have the same behavior!

Update : Question #2 answered by vmx => Thanks!

Update 2 : Question #1 answered by Nathan => Thanks!


回答1:


How I have done this in the past is in between setting up the redis connection via

var client = redis.createClient(global.redis.port, global.redis.host);

and actually starting my application, whether that be express or a custom app, I just do a very simple query, such as:

client.get(this.testKey, function(err,res) {
  if(err) 
    throw err;

  if(res === expectedValue)
    return startApp();
});

Essentially just put the code to start your app inside of the callback to a redis query, and you will then know if redis is running based on the result.




回答2:


2 issues, first the call to create Redis client is in wrong order, the createClient() takes port first, then the host, there's another optional options argument.

var client = redis.createClient(global.redis.port, global.redis.host);

Secondly, you will still not be able to achieve what you are trying to;

  1. The ready event is triggered asynchronously, it may not have received the ready response by the time you check for the redisIsReady flag.
  2. The flag redisIsReady will be set to true, but by now your session initialization code would have probably already executed.

You will have to wait before you get an error or ready event from redis before you initialize your session object.

Hope this helps.




回答3:


You can also check using node redis api:

if (client.connected){
  ...
} else {
  ...
}



回答4:


You can simply use sudo service redis-server status to check the current running status of redis on your local machine. Hope it helps.



来源:https://stackoverflow.com/questions/24231963/check-if-redis-is-running-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!