nrk / redis-lua

A Lua client library for the redis key value storage system.
MIT License
731 stars 239 forks source link

How to handle redis down #21

Closed iwanbk closed 11 years ago

iwanbk commented 11 years ago

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

nrk commented 11 years ago

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.
AMD-NICK commented 8 months ago

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?