Skip to content

Commit

Permalink
Refactorize connectors to use them in client and server
Browse files Browse the repository at this point in the history
  • Loading branch information
salrepe committed Nov 3, 2014
1 parent eb2faf7 commit c6e6f31
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 117 deletions.
16 changes: 16 additions & 0 deletions lib/sidekiq-unique-jobs/connectors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'sidekiq-unique-jobs/connectors/testing'
require 'sidekiq-unique-jobs/connectors/redis_pool'
require 'sidekiq-unique-jobs/connectors/sidekiq_redis'

module SidekiqUniqueJobs
module Connectors
ConnectorTypes= [Testing, RedisPool, SidekiqRedis]

def self.conn(redis_pool = nil)
ConnectorTypes.each do |connector|
conn = connector.conn(redis_pool)
return conn if conn
end
end
end
end
10 changes: 10 additions & 0 deletions lib/sidekiq-unique-jobs/connectors/redis_pool.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module SidekiqUniqueJobs
module Connectors
class RedisPool
def self.conn(redis_pool = nil)
return if redis_pool.nil?
redis_pool.with { |conn| conn }
end
end
end
end
9 changes: 9 additions & 0 deletions lib/sidekiq-unique-jobs/connectors/sidekiq_redis.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module SidekiqUniqueJobs
module Connectors
class SidekiqRedis
def self.conn(redis_pool = nil)
Sidekiq.redis { |conn| conn }
end
end
end
end
10 changes: 10 additions & 0 deletions lib/sidekiq-unique-jobs/connectors/testing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module SidekiqUniqueJobs
module Connectors
class Testing
def self.conn(redis_pool = nil)
return unless Config.testing_enabled?
SidekiqUniqueJobs.redis_mock { |conn| conn }
end
end
end
end
21 changes: 0 additions & 21 deletions lib/sidekiq-unique-jobs/middleware/client/connectors/redis_pool.rb

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'sidekiq-unique-jobs/middleware/server/unique_jobs'

module SidekiqUniqueJobs
module Middleware
module Client
module Strategies
class TestingInline < Unique
def self.elegible?
Config.testing_enabled? && Sidekiq::Testing.inline?
end

def review
SidekiqUniqueJobs::Middleware::Server::UniqueJobs.new.call(worker_class.new, item, queue, redis_pool) do
super
end
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
require 'digest'
require 'sidekiq-unique-jobs/connectors'

module SidekiqUniqueJobs
module Middleware
module Client
module Connectors
class Connector
def self.eligible?(redis_pool = nil)
raise 'Should be implemented in a sub class'
module Strategies
class Unique
def self.elegible?
true
end

def self.review_unique(worker_class, item, queue, redis_pool = nil)
new(worker_class, item, queue, redis_pool).review_unique { yield }
def self.review(worker_class, item, queue, redis_pool = nil)
new(worker_class, item, queue, redis_pool).review { yield }
end

def initialize(worker_class, item, queue, redis_pool = nil)
Expand All @@ -18,7 +21,7 @@ def initialize(worker_class, item, queue, redis_pool = nil)
@redis_pool = redis_pool
end

def review_unique
def review
item['unique_hash'] = payload_hash
return unless unique_for_connection?
yield
Expand Down Expand Up @@ -52,7 +55,7 @@ def unique_for_connection?
end

def conn
raise 'Should be implemented in a sub class'
SidekiqUniqueJobs::Connectors.conn(redis_pool)
end

def payload_hash
Expand Down
20 changes: 6 additions & 14 deletions lib/sidekiq-unique-jobs/middleware/client/unique_jobs.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
require 'digest'
require 'sidekiq-unique-jobs/middleware/client/connectors/testing_fake'
require 'sidekiq-unique-jobs/middleware/client/connectors/testing_inline'
require 'sidekiq-unique-jobs/middleware/client/connectors/redis_pool'
require 'sidekiq-unique-jobs/middleware/client/connectors/sidekiq_redis'
require 'sidekiq-unique-jobs/middleware/client/strategies/unique'
require 'sidekiq-unique-jobs/middleware/client/strategies/testing_inline'

module SidekiqUniqueJobs
module Middleware
module Client
class UniqueJobs
Connectors = [
Connectors::TestingFake,
Connectors::TestingInline,
Connectors::RedisPool,
Connectors::SidekiqRedis
]
Strategies = [Strategies::TestingInline, Strategies::Unique]

attr_reader :item, :worker_class, :redis_pool

Expand All @@ -23,7 +15,7 @@ def call(worker_class, item, queue, redis_pool = nil)
@redis_pool = redis_pool

if unique_enabled?
connector.review_unique(worker_class, item, queue, redis_pool) { yield }
strategy.review(worker_class, item, queue, redis_pool) { yield }
else
yield
end
Expand All @@ -35,8 +27,8 @@ def unique_enabled?
worker_class.get_sidekiq_options['unique'] || item['unique']
end

def connector
Connectors.detect { |conn| conn.eligible?(redis_pool) }
def strategy
Strategies.detect { |s| s.elegible? }
end

# Attempt to constantize a string worker_class argument, always
Expand Down
9 changes: 4 additions & 5 deletions lib/sidekiq-unique-jobs/middleware/server/unique_jobs.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'digest'
require 'sidekiq-unique-jobs/connectors'

module SidekiqUniqueJobs
module Middleware
Expand Down Expand Up @@ -52,17 +53,15 @@ def payload_hash(item)
end

def unlock(payload_hash)
connector.del(payload_hash)
conn.del(payload_hash)
end

def logger
Sidekiq.logger
end

def connector
return SidekiqUniqueJobs.redis_mock { |conn| conn } if Config.testing_enabled?
return redis_pool.with { |conn| conn } if redis_pool
Sidekiq.redis { |conn| conn }
def conn
SidekiqUniqueJobs::Connectors.conn(redis_pool)
end
end
end
Expand Down

0 comments on commit c6e6f31

Please sign in to comment.