-
-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify file structure and improve naming
- Loading branch information
Showing
23 changed files
with
453 additions
and
459 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'sidekiq_unique_jobs/client/strategies/unique' | ||
require 'sidekiq_unique_jobs/client/strategies/testing_inline' | ||
|
||
module SidekiqUniqueJobs | ||
module Client | ||
class Middleware | ||
STRATEGIES = [ | ||
Strategies::TestingInline, | ||
Strategies::Unique | ||
] | ||
|
||
attr_reader :item, :worker_class, :redis_pool | ||
|
||
def call(worker_class, item, queue, redis_pool = nil) | ||
@worker_class = SidekiqUniqueJobs.worker_class_constantize(worker_class) | ||
@item = item | ||
@redis_pool = redis_pool | ||
|
||
if unique_enabled? | ||
strategy.review(worker_class, item, queue, redis_pool, log_duplicate_payload?) { yield } | ||
else | ||
yield | ||
end | ||
end | ||
|
||
private | ||
|
||
def unique_enabled? | ||
worker_class.get_sidekiq_options['unique'] || item['unique'] | ||
end | ||
|
||
def log_duplicate_payload? | ||
worker_class.get_sidekiq_options['log_duplicate_payload'] || item['log_duplicate_payload'] | ||
end | ||
|
||
def strategy | ||
STRATEGIES.detect(&:elegible?) | ||
end | ||
end | ||
end | ||
end |
23 changes: 23 additions & 0 deletions
23
lib/sidekiq_unique_jobs/client/strategies/testing_inline.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'sidekiq_unique_jobs/server/middleware' | ||
|
||
module SidekiqUniqueJobs | ||
module Client | ||
module Strategies | ||
class TestingInline < Unique | ||
def self.elegible? | ||
SidekiqUniqueJobs.config.inline_testing_enabled? | ||
end | ||
|
||
def review | ||
_middleware.call(worker_class.new, item, queue, redis_pool) do | ||
super | ||
end | ||
end | ||
|
||
def _middleware | ||
SidekiqUniqueJobs::Server::Middleware.new | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
require 'digest' | ||
require 'sidekiq_unique_jobs/connectors' | ||
|
||
REQUIRE_FILES = lambda do | ||
if SidekiqUniqueJobs.config.testing_enabled? && Sidekiq::Testing.fake? | ||
require 'sidekiq_unique_jobs/sidekiq_test_overrides' | ||
end | ||
end | ||
|
||
module SidekiqUniqueJobs | ||
module Client | ||
module Strategies | ||
class Unique | ||
def self.elegible? | ||
true | ||
end | ||
|
||
def self.review(worker_class, item, queue, redis_pool = nil, log_duplicate_payload = false) | ||
new(worker_class, item, queue, redis_pool, log_duplicate_payload).review do | ||
yield | ||
end | ||
end | ||
|
||
def initialize(worker_class, item, queue, redis_pool = nil, log_duplicate_payload = false) | ||
@worker_class = SidekiqUniqueJobs.worker_class_constantize(worker_class) | ||
@item = item | ||
@queue = queue | ||
@redis_pool = redis_pool | ||
@log_duplicate_payload = log_duplicate_payload | ||
REQUIRE_FILES.call | ||
end | ||
|
||
def review | ||
item['unique_hash'] = payload_hash | ||
|
||
unless unique_for_connection? | ||
Sidekiq.logger.warn "payload is not unique #{item}" if @log_duplicate_payload | ||
return | ||
end | ||
|
||
yield | ||
end | ||
|
||
private | ||
|
||
attr_reader :item, :worker_class, :redis_pool, :queue, :log_duplicate_payload | ||
|
||
def unique_for_connection? | ||
send("#{storage_method}_unique_for?") | ||
end | ||
|
||
def storage_method | ||
SidekiqUniqueJobs.config.unique_storage_method | ||
end | ||
|
||
def old_unique_for? | ||
unique = nil | ||
connection do |conn| | ||
conn.watch(payload_hash) | ||
pid = conn.get(payload_hash).to_i | ||
if pid == 1 || (pid == 2 && item['at']) | ||
# if the job is already queued, or is already scheduled and | ||
# we're trying to schedule again, abort | ||
conn.unwatch | ||
else | ||
unique = conn.multi do | ||
# set value of 2 for scheduled jobs, 1 for queued jobs. | ||
conn.setex(payload_hash, expires_at, item['jid']) | ||
end | ||
end | ||
end | ||
unique | ||
end | ||
|
||
def new_unique_for? | ||
connection do |conn| | ||
return conn.set(payload_hash, item['jid'], nx: true, ex: expires_at) | ||
end | ||
end | ||
|
||
def expires_at | ||
# if the job was previously scheduled and is now being queued, | ||
# or we've never seen it before | ||
ex = unique_job_expiration || SidekiqUniqueJobs.config.default_expiration | ||
ex = ((Time.at(item['at']) - Time.now.utc) + ex).to_i if item['at'] | ||
ex | ||
end | ||
|
||
def connection(&block) | ||
SidekiqUniqueJobs::Connectors.connection(redis_pool, &block) | ||
end | ||
|
||
def payload_hash | ||
SidekiqUniqueJobs.get_payload(item['class'], item['queue'], item['args']) | ||
end | ||
|
||
def unique_job_expiration | ||
worker_class.get_sidekiq_options['unique_job_expiration'] | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
require 'sidekiq_unique_jobs/lib' | ||
|
||
begin | ||
require 'mock_redis' | ||
rescue LoadError | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 0 additions & 25 deletions
25
lib/sidekiq_unique_jobs/middleware/client/strategies/testing_inline.rb
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.