Permalink
Browse files

Add RedisReconnect middleware.

  • Loading branch information...
1 parent b69cd86 commit dc4978a3b6139bed23c41de1ac74f2ab502c6cab @myronmarston myronmarston committed Feb 1, 2013
Showing with 73 additions and 0 deletions.
  1. +22 −0 lib/qless/middleware/redis_reconnect.rb
  2. +51 −0 spec/unit/middleware/redis_reconnect_spec.rb
@@ -0,0 +1,22 @@
+module Qless
+ module Middleware
+ module RedisReconnect
+ def self.new(*redis_connections)
+ Module.new do
+ define_singleton_method :to_s do
+ "Qless::Middleware::RedisReconnect(#{redis_connections.map(&:id).join(', ')})"
+ end
+
+ define_method :around_perform do |job|
+ redis_connections.each do |redis|
+ redis.client.reconnect
+ end
+
+ super(job)
+ end
+ end
+ end
+ end
+ end
+end
+
@@ -0,0 +1,51 @@
+require 'spec_helper'
+require 'qless/middleware/redis_reconnect'
+require 'redis'
+require 'qless/worker'
+
+module Qless
+ module Middleware
+ describe RedisReconnect do
+ let(:url_1) { "redis://localhost:1234/2" }
+ let(:url_2) { "redis://localhost:4321/3" }
+
+ it 'includes the redis connection strings in its description' do
+ redis_1 = Redis.new(url: url_1)
+ redis_2 = Redis.new(url: url_2)
+ middleware = Qless::Middleware::RedisReconnect.new(redis_1, redis_2)
+
+ expect(middleware.inspect).to include(url_1, url_2)
+ expect(middleware.to_s).to include(url_1, url_2)
+ end
+
+ def perform(job)
+ worker = Qless::Worker.new(stub)
+ worker.extend Qless::Middleware::Sentry
+ worker.perform(job)
+ end
+
+ it 'reconnects to the given redis clients before performing the job' do
+ events = []
+
+ stub_const("MyJob", Class.new {
+ define_singleton_method :perform do |job|
+ events << :performed
+ end
+ })
+
+ redis_connections = 1.upto(2).map do |i|
+ client = fire_double("Redis::Client")
+ client.stub(:reconnect) { events << :"reconnect_#{i}" }
+ fire_double("Redis", client: client)
+ end
+
+ worker = Qless::Worker.new(stub)
+ worker.extend Qless::Middleware::RedisReconnect.new(*redis_connections)
+ worker.perform(Qless::Job.build(stub.as_null_object, MyJob))
+
+ expect(events).to eq([:reconnect_1, :reconnect_2, :performed])
+ end
+ end
+ end
+end
+

0 comments on commit dc4978a

Please sign in to comment.