How to handle errors in Ruby while connecting to Redis? - ruby

I have a code in Ruby (as a Logstash plugin) in which I connect to Redis and get information like what is suggested in (How to use redis inside logstash filter?) as below:
require "redis"
$redis = Redis.new(host: "REDIS_HOST", port: REDIS_PORT, db: REDIS_DB)
#my_data = $redis.hget("DATA_KEY", "HASH_FIELD").to_i
Everything works OK generally; however, I want to know:
1- Which possible errors might be faced by the code while connecting to Redis or getting data (e.g. Redis is unreachable or DATA_KEY or HASH_FIELD does not exist)
2- How should we handle such errors?

Related

requests_cache: connecting using REDIS_URL

I am trying to cache my requests using requests_cache and redis like so:
requests_cache.install_cache(
'requests_cache', backend='redis', expire_after=600
)
and when Redis is run on localhost:6379, everything is fine and works out of the box.
However when I deploy my app to Heroku where there is a REDIS_URL environment variable, the above command fails because obviousle REDIS_URL does not point to localhost:
Error 111 connecting to localhost:6379. Connection refused.
So the question is, how do I make it work on Heroku? the docs aren't clear on the subject.
You have to pass an additional argument to install_cache called connection which will be of StrictRedis type. So I guess create it like that:
r = redis.StrictRedis(host='REDIS_URL', port=6379, db=0)
requests_cache.install_cache(
'requests_cache', backend='redis', expire_after=600, connection=r
)
Or something similar, depending on how much information REDIS_URL contains (protocol, port etc.)

Sending URL params via EventMachine.connect

Hi good people of StackOverflow.
I am having issues with EventMachine.connect, I am able to specify host name and port but not sent additional params in my connection string.
Here is my code:
EM.connect 'localhost', 61613, StompListener
Now I am experiencing issues with STOMP connection to ActiveMQ server, basically, connection keeps dying from time to time and I need to reconnect. I have found following info where Apache ActiveMQ suggests
using transport.keepAlive=true for these cases. But I am not able to figure out how to add this URL params to EM.connect, do you know how?

Ruby Mongo Driver 2.0.0 connection status

Read the docs, searched the stack, but I can't find a very simple
function: How do I check if the connection has been established or not?
establishing DB connection with
db = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'test')
From time to time I forget to launch the mongodb database and then the driver tries to connect and well... does not say's a lot about the connection itself.
Is there some kind of .isConnected? method or anything to find
the connection status of current Mongo::Client instance?
Thanks in advance
Version 1.x of the Mongo ruby driver did expose a connected? method on Mongo::Connection, but it's been moved in v2 to live on Mongo::Server, however its meaning has changed somewhat.
Here's an example of using this method:
mongo = Mongo::Client.new('mongodb://localhost/mycollection')
#=> #<Mongo::Client:0x70279909414900 cluster=localhost:27017>
mongo.cluster.servers.first.connected?
#=> true
In v2 of the Mongo ruby driver, it will raise an exception when trying to use an invalid connection, for example, Mongo::Error::NoServerAvailable, based on the connection parameters about timeouts. For your purposes of simply checking whether the connection has been established, maybe something like:
mongo = Mongo::Client.new('mongodb://localhost/mycollection')
mongo.list_databases => # raise error or return an array

Redis server reports Reading from client: Connection reset on amazon ec2 c1.medium instance

I run redis2.4.16 on ec2 medium instance, the persistent is standard ebs, and i checked the redis log , found there is some log report "Reading from client: Connection reset " occurs every few hours, all my clients and server are in the same zone:ap-northeast-1a, and the operation system is ubuntu server 12.04. The client is the jredis + spring data redis 1.0.0.M4,Anyone can figure this out or give some advice, thanks!
below is the redis info command result:
redis_version:2.4.16
redis_git_sha1:00000000
redis_git_dirty:0
arch_bits:64
multiplexing_api:epoll
gcc_version:4.5.2
process_id:3265
uptime_in_seconds:2658600
uptime_in_days:30
lru_clock:561139
used_cpu_sys:29421.34
used_cpu_user:10731.37
used_cpu_sys_children:20022.24
used_cpu_user_children:75702.79
connected_clients:44
connected_slaves:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
used_memory:1111572800
used_memory_human:1.04G
used_memory_rss:1133101056
used_memory_peak:1112071512
used_memory_peak_human:1.04G
mem_fragmentation_ratio:1.02
mem_allocator:jemalloc-3.0.0
loading:0
aof_enabled:0
changes_since_last_save:1343
bgsave_in_progress:0
last_save_time:1368760178
bgrewriteaof_in_progress:0
total_connections_received:904643
total_commands_processed:592333133
expired_keys:0
evicted_keys:0
keyspace_hits:443393839
keyspace_misses:30383206
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:359082
vm_enabled:0
role:master
slave0:xxx,online
db0:keys=364558,expires=0
As you can see from logs, redis tries to communicate with a client that has closed its connection.
Thats probably because some of your client are not closing the connection with redis after they are done with it.
This can eventually lead redis to run out of connections (depending on your connection limits and the amount of traffic you have)
An easy solution for this is to set a connection timeout (0 as 'no timeout' by default) in redis.conf so that redis will close opened connection after X seconds.
Note: you should include the output of redis config get * when asking this kind of questions ;)

Redis on Heroku

I am using Redis Objects with Redis To Go on Heroku. I have a counter on a model, like this:
class Performance < ActiveRecord::Base
include Redis::Objects
counter :tickets_sold, start: 0
end
Accessing this value from Heroku console is working great as well.
irb(main):002:0> Performance.last.tickets_sold.value
Performance Load (3.9ms) SELECT `performances`.* FROM `performances` ORDER BY `performances`.`id` DESC LIMIT 1
=> 0
I confirmed that Redis.current is present:
irb(main):003:0> Redis.current
=> # Redis client v2.2.2 connected to redis://ray.redistogo.com:9023/0 (Redis v2.4.11)
However, accessing the same counter from a template on the website runs into a Errno::ECONNREFUSED error.
Connection refused - Unable to connect to Redis on 127.0.0.1:6379
Why is it trying to connect to the local Redis url? Inspecting Redis.current on the website is also failing with the connection error above. Considering that the same command is working just fine from the Heroku console, I'm a little puzzled as to what's going on here. I hope someone has seen this before and knows how to solve it...

Resources