Skip to content

Commit

Permalink
Merge pull request resque#958 from oliverbarnes/client_refactor
Browse files Browse the repository at this point in the history
Client refactor
  • Loading branch information
steveklabnik committed Apr 18, 2013
2 parents 5319ef2 + d8a80cb commit 7cb22de
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/resque.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def coder
:after_perform=

def to_s
"Resque Client connected to #{redis_id}"
"Resque Backend connected to #{redis_id}"
end

# If 'inline' is true Resque will call #perform method inline
Expand Down
20 changes: 10 additions & 10 deletions lib/resque/client.rb → lib/resque/backend.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
##
# Resque::Client is a wrapper around all things Redis.
# Resque::Backend is a wrapper around all things Redis.
#
# This provides a level of indirection so that the rest of our code
# doesn't need to know anything about Redis, and allows us to someday
# maybe even move away from Redis to another backend if we need to.
# maybe even move away from Redis to another data store if we need to.
#
# Also helps because we can mock this out in our tests. Only mock
# stuff you own.
#
# Also, we can theoretically have multiple Redis/Resques going on
# one project.
module Resque
class Client
class Backend

# This error is thrown if we have a problem connecting to
# the back end.
ConnectionError = Class.new(StandardError)

attr_reader :backend, :logger
attr_reader :store, :logger

def initialize(backend, logger)
@backend = backend
def initialize(store, logger)
@store = store
@logger = logger
end

# Reconnects to the backend
# Reconnects to the store
#
# Maybe your backend died, maybe you've just forked. Whatever the
# reason, this method will attempt to reconnect to the backend.
# Maybe your store died, maybe you've just forked. Whatever the
# reason, this method will attempt to reconnect to the store.
#
# If it can't connect, it will attempt to rety the connection after
# sleeping, and after 3 failures will throw an exception.
def reconnect
tries = 0
begin
backend.client.reconnect
store.client.reconnect
rescue Redis::BaseConnectionError
tries += 1

Expand Down
2 changes: 1 addition & 1 deletion lib/resque/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Config
# 2. A 'hostname:port:db' String (to select the Redis db)
# 3. A 'hostname:port/namespace' String (to set the Redis namespace)
# 4. A Redis URL String 'redis://host:port'
# 5. An instance of `Redis`, `Redis::Client`, `Redis::DistRedis`,
# 5. An instance of `Redis`, `Redis::Backend`, `Redis::DistRedis`,
# or `Redis::Namespace`.
def redis=(server)
return if server == "" or server.nil?
Expand Down
4 changes: 2 additions & 2 deletions lib/resque/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require 'resque/core_ext/hash'
require 'resque/worker_registry'
require 'resque/errors'
require 'resque/client'
require 'resque/backend'

module Resque
# A Resque Worker processes jobs. On platforms that support fork(2),
Expand Down Expand Up @@ -62,7 +62,7 @@ def initialize(queues = [], options = {})
@paused = nil
@cant_fork = false

@client = @options.fetch(:client) { Client.new(Resque.redis, Resque.logger) }
@client = @options.fetch(:client) { Backend.new(Resque.redis, Resque.logger) }

validate_queues
end
Expand Down
11 changes: 6 additions & 5 deletions test/resque/client_test.rb → test/resque/backend_test.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
require 'test_helper'
require 'tempfile'

require 'resque/client'
require 'resque/backend'

describe Resque::Client do
describe Resque::Backend do
let(:logger) { Logger.new(Tempfile.new('resque-log')) }

describe "#new" do
it "needs a Redis to be built" do
redis = MiniTest::Mock.new
client = Resque::Client.new(redis, logger)
backend = Resque::Backend.new(redis, logger)

assert_same client.backend.__id__, redis.__id__
assert_same backend.store.__id__, redis.__id__
end
end

Expand All @@ -30,7 +31,7 @@ def reconnect
end
end.new

client = Resque::Client.new(redis, logger)
client = Resque::Backend.new(redis, logger)

# not actually stubbing right now?
Kernel.stub(:sleep, nil) do
Expand Down

0 comments on commit 7cb22de

Please sign in to comment.