Skip to content

Commit

Permalink
Refactor configuration and fix styles
Browse files Browse the repository at this point in the history
  • Loading branch information
mhenrixon committed Nov 3, 2014
1 parent f7fc14c commit 3fd7623
Show file tree
Hide file tree
Showing 28 changed files with 310 additions and 233 deletions.
36 changes: 36 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

Lint/HandleExceptions:
Enabled: true

Lint/UselessAssignment:
Enabled: true

Metrics/AbcSize:
Max: 38

Metrics/CyclomaticComplexity:
Max: 7

Metrics/LineLength:
Max: 108

Metrics/MethodLength:
Max: 13

Metrics/PerceivedComplexity:
Max: 8

Style/AccessorMethodName:
Enabled: true

Style/ConstantName:
Enabled: true

Style/Documentation:
Enabled: false

Style/FileName:
Enabled: true

Style/GlobalVars:
Enabled: true
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ Requiring the gem in your gemfile should be sufficient to enable unique jobs.

### Finer Control over Uniqueness

Sometimes it is desired to have a finer control over which arguments are used in determining uniqueness of the job, and others may be _transient_. For this use-case, you need to set `SidekiqUniqueJobs::Config.unique_args_enabled` to true in an initializer, and then defined either `unique_args` method, or a ruby proc.
Sometimes it is desired to have a finer control over which arguments are used in determining uniqueness of the job, and others may be _transient_. For this use-case, you need to set `SidekiqUniqueJobs.config.unique_args_enabled` to true in an initializer, and then defined either `unique_args` method, or a ruby proc.

The unique_args method need to return an array of values to use for uniqueness check.

```ruby
SidekiqUniqueJobs::Config.unique_args_enabled = true
SidekiqUniqueJobs.config.unique_args_enabled = true
```

The method or the proc can return a modified version of args without the transient arguments included, as shown below:
Expand Down
10 changes: 6 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/usr/bin/env rake
require "rubygems"
require "bundler/setup"
require "bundler/gem_tasks"
require 'rubygems'
require 'bundler/setup'
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
require 'rubocop/rake_task'

RuboCop::RakeTask.new(:style)
RSpec::Core::RakeTask.new(:spec)

task :default => :spec
task default: [:spec, :style]
11 changes: 0 additions & 11 deletions lib/sidekiq-unique-jobs.rb

This file was deleted.

44 changes: 0 additions & 44 deletions lib/sidekiq-unique-jobs/config.rb

This file was deleted.

30 changes: 0 additions & 30 deletions lib/sidekiq-unique-jobs/payload_helper.rb

This file was deleted.

27 changes: 27 additions & 0 deletions lib/sidekiq_unique_jobs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'yaml' if RUBY_VERSION.include?('2.0.0')
require 'sidekiq_unique_jobs/middleware'
require 'sidekiq_unique_jobs/version'
require 'sidekiq_unique_jobs/config'
require 'sidekiq_unique_jobs/payload_helper'
require 'ostruct'

module SidekiqUniqueJobs
module_function

def config
@config ||= Config.new(
unique_prefix: 'sidekiq_unique',
unique_args_enabled: false,
default_expiration: 30 * 60,
default_unlock_order: :after_yield
)
end

def unique_args_enabled?
config.unique_args_enabled
end

def configure
yield configuration
end
end
47 changes: 47 additions & 0 deletions lib/sidekiq_unique_jobs/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module SidekiqUniqueJobs
class Config < OpenStruct
CONFIG_ACCESSORS = [
:unique_prefix,
:unique_args_enabled,
:default_expiration,
:default_unlock_order
]

class << self
warn('This method has been deprecated. See readme for information')
CONFIG_ACCESSORS.each do |method|
define_method(method) do
warn('This method has been deprecated. See readme for information')
config.send(method)
end

define_method("#{method}=") do |obj|
warn('This method has been deprecated. See readme for information')
config.send("#{method}=", obj)
end
end

def unique_args_enabled?
warn('This method has been deprecated. See readme for information')
config.unique_args_enabled
end

def config
SidekiqUniqueJobs.config
end
end

def testing_enabled?
if Sidekiq.const_defined?('Testing') && Sidekiq::Testing.enabled?
require 'sidekiq_unique_jobs/testing'
return true
end

false
end

def unique_args_enabled?
config.unique_args_enabled
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

Sidekiq.configure_server do |config|
config.server_middleware do |chain|
require 'sidekiq-unique-jobs/middleware/server/unique_jobs'
require 'sidekiq_unique_jobs/middleware/server/unique_jobs'
chain.add SidekiqUniqueJobs::Middleware::Server::UniqueJobs
end
config.client_middleware do |chain|
require 'sidekiq-unique-jobs/middleware/client/unique_jobs'
require 'sidekiq_unique_jobs/middleware/client/unique_jobs'
chain.add SidekiqUniqueJobs::Middleware::Client::UniqueJobs
end

end

Sidekiq.configure_client do |config|
config.client_middleware do |chain|
require 'sidekiq-unique-jobs/middleware/client/unique_jobs'
require 'sidekiq_unique_jobs/middleware/client/unique_jobs'
chain.add SidekiqUniqueJobs::Middleware::Client::UniqueJobs
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module Middleware
module Client
module Connectors
class Connector
def self.eligible?(redis_pool = nil)
raise 'Should be implemented in a sub class'
def self.eligible?(_redis_pool = nil)
fail 'Should be implemented in a sub class'
end

def self.review_unique(worker_class, item, queue, redis_pool = nil)
Expand Down Expand Up @@ -33,14 +33,14 @@ def unique_for_connection?
conn.watch(payload_hash)

if conn.get(payload_hash).to_i == 1 ||
(conn.get(payload_hash).to_i == 2 && item['at'])
(conn.get(payload_hash).to_i == 2 && item['at'])
# if the job is already queued, or is already scheduled and
# we're trying to schedule again, abort
conn.unwatch
else
# if the job was previously scheduled and is now being queued,
# or we've never seen it before
expires_at = unique_job_expiration || SidekiqUniqueJobs::Config.default_expiration
expires_at = unique_job_expiration || SidekiqUniqueJobs.config.default_expiration
expires_at = ((Time.at(item['at']) - Time.now.utc) + expires_at).to_i if item['at']

unique = conn.multi do
Expand All @@ -52,7 +52,7 @@ def unique_for_connection?
end

def conn
raise 'Should be implemented in a sub class'
fail 'Should be implemented in a sub class'
end

def payload_hash
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'sidekiq-unique-jobs/middleware/client/connectors/connector'
require 'sidekiq_unique_jobs/middleware/client/connectors/connector'

module SidekiqUniqueJobs
module Middleware
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require 'sidekiq-unique-jobs/middleware/client/connectors/connector'
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)
def self.eligible?(_redis_pool = nil)
true
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require 'sidekiq-unique-jobs/middleware/client/connectors/connector'
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?
def self.eligible?(_redis_pool = nil)
SidekiqUniqueJobs.config.testing_enabled? && Sidekiq::Testing.fake?
end

private
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
require 'sidekiq-unique-jobs/middleware/client/connectors/connector'
require 'sidekiq-unique-jobs/middleware/server/unique_jobs'
require 'sidekiq_unique_jobs/middleware/client/connectors/connector'
require 'sidekiq_unique_jobs/middleware/server/unique_jobs'

module SidekiqUniqueJobs
module Middleware
module Client
module Connectors
class TestingInline < Connector
def self.eligible?(redis_pool = nil)
Config.testing_enabled? && Sidekiq::Testing.inline?
def self.eligible?(_redis_pool = nil)
SidekiqUniqueJobs.config.testing_enabled? && Sidekiq::Testing.inline?
end

def review_unique
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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/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'

module SidekiqUniqueJobs
module Middleware
module Client
class UniqueJobs
Connectors = [
CONNECTORS = [
Connectors::TestingFake,
Connectors::TestingInline,
Connectors::RedisPool,
Expand Down Expand Up @@ -36,17 +36,16 @@ def unique_enabled?
end

def connector
Connectors.detect { |conn| conn.eligible?(redis_pool) }
CONNECTORS.detect { |conn| conn.eligible?(redis_pool) }
end

# Attempt to constantize a string worker_class argument, always
# failing back to the original argument.
def worker_class_constantize(worker_class)
if worker_class.is_a?(String)
worker_class.constantize rescue worker_class
else
worker_class
end
return worker_class unless worker_class.is_a?(String)
worker_class.constantize
rescue NameError
worker_class
end
end
end
Expand Down
Loading

0 comments on commit 3fd7623

Please sign in to comment.