nrk / redis-lua

A Lua client library for the redis key value storage system.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to handle redis down

iwanbk opened this issue · comments

Hi,
When redis down, connect function will failed without giving any return value.
How to handle this situation?
Thanks

The library rises a Lua error when it's unable to connect to the specified server, but you can use pcall() when calling redis.connect() if you need to gracefully handle such failures. Here is an example:

local redis = require 'redis'
local connected, client = pcall(redis.connect, '127.0.0.1', 6390)

if not connected then
  -- `client` contains the error message when `connected` is false
  print(client)
  os.exit()
end

-- Do stuff with redis.

Are there any ways to handle a redis server shutdown, such as rebooting it?

If I try to run a query with client:get("blabla"), I get a "closed" error (or something like that). Is there any way to know about the shutdown before the error and how best to handle such a shutdown in code?