diff --git a/lib/sidekiq-unique-jobs/connectors.rb b/lib/sidekiq-unique-jobs/connectors.rb new file mode 100644 index 000000000..7e3405292 --- /dev/null +++ b/lib/sidekiq-unique-jobs/connectors.rb @@ -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 diff --git a/lib/sidekiq-unique-jobs/connectors/redis_pool.rb b/lib/sidekiq-unique-jobs/connectors/redis_pool.rb new file mode 100644 index 000000000..80f434f78 --- /dev/null +++ b/lib/sidekiq-unique-jobs/connectors/redis_pool.rb @@ -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 diff --git a/lib/sidekiq-unique-jobs/connectors/sidekiq_redis.rb b/lib/sidekiq-unique-jobs/connectors/sidekiq_redis.rb new file mode 100644 index 000000000..cfc513f66 --- /dev/null +++ b/lib/sidekiq-unique-jobs/connectors/sidekiq_redis.rb @@ -0,0 +1,9 @@ +module SidekiqUniqueJobs + module Connectors + class SidekiqRedis + def self.conn(redis_pool = nil) + Sidekiq.redis { |conn| conn } + end + end + end +end diff --git a/lib/sidekiq-unique-jobs/connectors/testing.rb b/lib/sidekiq-unique-jobs/connectors/testing.rb new file mode 100644 index 000000000..615234768 --- /dev/null +++ b/lib/sidekiq-unique-jobs/connectors/testing.rb @@ -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 diff --git a/lib/sidekiq-unique-jobs/middleware/client/connectors/redis_pool.rb b/lib/sidekiq-unique-jobs/middleware/client/connectors/redis_pool.rb deleted file mode 100644 index 225e3ae57..000000000 --- a/lib/sidekiq-unique-jobs/middleware/client/connectors/redis_pool.rb +++ /dev/null @@ -1,21 +0,0 @@ -require 'sidekiq-unique-jobs/middleware/client/connectors/connector' - -module SidekiqUniqueJobs - module Middleware - module Client - module Connectors - class RedisPool < Connector - def self.eligible?(redis_pool = nil) - !redis_pool.nil? - end - - private - - def conn - redis_pool.with { |conn| conn } - end - end - end - end - end -end diff --git a/lib/sidekiq-unique-jobs/middleware/client/connectors/sidekiq_redis.rb b/lib/sidekiq-unique-jobs/middleware/client/connectors/sidekiq_redis.rb deleted file mode 100644 index fa1e88c60..000000000 --- a/lib/sidekiq-unique-jobs/middleware/client/connectors/sidekiq_redis.rb +++ /dev/null @@ -1,21 +0,0 @@ -require 'sidekiq-unique-jobs/middleware/client/connectors/connector' - -module SidekiqUniqueJobs - module Middleware - module Client - module Connectors - class SidekiqRedis < Connector - def self.eligible?(redis_pool = nil) - true - end - - private - - def conn - Sidekiq.redis { |conn| conn } - end - end - end - end - end -end diff --git a/lib/sidekiq-unique-jobs/middleware/client/connectors/testing_fake.rb b/lib/sidekiq-unique-jobs/middleware/client/connectors/testing_fake.rb deleted file mode 100644 index e5d6b18f7..000000000 --- a/lib/sidekiq-unique-jobs/middleware/client/connectors/testing_fake.rb +++ /dev/null @@ -1,21 +0,0 @@ -require 'sidekiq-unique-jobs/middleware/client/connectors/connector' - -module SidekiqUniqueJobs - module Middleware - module Client - module Connectors - class TestingFake < Connector - def self.eligible?(redis_pool = nil) - Config.testing_enabled? && Sidekiq::Testing.fake? - end - - private - - def conn - SidekiqUniqueJobs.redis_mock { |conn| conn } - end - end - end - end - end -end diff --git a/lib/sidekiq-unique-jobs/middleware/client/connectors/testing_inline.rb b/lib/sidekiq-unique-jobs/middleware/client/connectors/testing_inline.rb deleted file mode 100644 index 122565111..000000000 --- a/lib/sidekiq-unique-jobs/middleware/client/connectors/testing_inline.rb +++ /dev/null @@ -1,27 +0,0 @@ -require 'sidekiq-unique-jobs/middleware/client/connectors/connector' - -module SidekiqUniqueJobs - module Middleware - module Client - module Connectors - class TestingInline < Connector - def self.eligible?(redis_pool = nil) - Config.testing_enabled? && Sidekiq::Testing.inline? - end - - def review_unique - Server::UniqueJobs.new.call(worker_class.new, item, queue, redis_pool) do - super - end - end - - private - - def conn - SidekiqUniqueJobs.redis_mock { |conn| conn } - end - end - end - end - end -end diff --git a/lib/sidekiq-unique-jobs/middleware/client/strategies/testing_inline.rb b/lib/sidekiq-unique-jobs/middleware/client/strategies/testing_inline.rb new file mode 100644 index 000000000..01c15e887 --- /dev/null +++ b/lib/sidekiq-unique-jobs/middleware/client/strategies/testing_inline.rb @@ -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 diff --git a/lib/sidekiq-unique-jobs/middleware/client/connectors/connector.rb b/lib/sidekiq-unique-jobs/middleware/client/strategies/unique.rb similarity index 82% rename from lib/sidekiq-unique-jobs/middleware/client/connectors/connector.rb rename to lib/sidekiq-unique-jobs/middleware/client/strategies/unique.rb index dc69f0479..f2eefbc24 100644 --- a/lib/sidekiq-unique-jobs/middleware/client/connectors/connector.rb +++ b/lib/sidekiq-unique-jobs/middleware/client/strategies/unique.rb @@ -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) @@ -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 @@ -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 diff --git a/lib/sidekiq-unique-jobs/middleware/client/unique_jobs.rb b/lib/sidekiq-unique-jobs/middleware/client/unique_jobs.rb index 3dc1d3187..e994b200f 100644 --- a/lib/sidekiq-unique-jobs/middleware/client/unique_jobs.rb +++ b/lib/sidekiq-unique-jobs/middleware/client/unique_jobs.rb @@ -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 @@ -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 @@ -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 diff --git a/lib/sidekiq-unique-jobs/middleware/server/unique_jobs.rb b/lib/sidekiq-unique-jobs/middleware/server/unique_jobs.rb index d36036423..c981d29b7 100644 --- a/lib/sidekiq-unique-jobs/middleware/server/unique_jobs.rb +++ b/lib/sidekiq-unique-jobs/middleware/server/unique_jobs.rb @@ -1,4 +1,5 @@ require 'digest' +require 'sidekiq-unique-jobs/connectors' module SidekiqUniqueJobs module Middleware @@ -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