sidekiq-unique-jobs 3.0.2 → 3.0.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sidekiq-unique-jobs might be problematic. Click here for more details.

Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +36 -0
  3. data/.travis.yml +0 -2
  4. data/Appraisals +2 -1
  5. data/CHANGELOG.md +4 -4
  6. data/Gemfile +1 -3
  7. data/README.md +2 -2
  8. data/Rakefile +6 -4
  9. data/gemfiles/sidekiq_2.15.gemfile +1 -3
  10. data/gemfiles/sidekiq_2.16.gemfile +1 -3
  11. data/gemfiles/sidekiq_2.17.gemfile +1 -3
  12. data/gemfiles/sidekiq_3.0.gemfile +1 -3
  13. data/gemfiles/sidekiq_develop.gemfile +2 -4
  14. data/lib/sidekiq_unique_jobs.rb +27 -0
  15. data/lib/sidekiq_unique_jobs/config.rb +47 -0
  16. data/lib/sidekiq_unique_jobs/connectors.rb +16 -0
  17. data/lib/sidekiq_unique_jobs/connectors/redis_pool.rb +10 -0
  18. data/lib/sidekiq_unique_jobs/connectors/sidekiq_redis.rb +9 -0
  19. data/lib/sidekiq_unique_jobs/connectors/testing.rb +10 -0
  20. data/lib/{sidekiq-unique-jobs → sidekiq_unique_jobs}/middleware.rb +3 -3
  21. data/lib/sidekiq_unique_jobs/middleware/client/strategies/testing_inline.rb +25 -0
  22. data/lib/sidekiq_unique_jobs/middleware/client/strategies/unique.rb +72 -0
  23. data/lib/sidekiq_unique_jobs/middleware/client/unique_jobs.rb +48 -0
  24. data/lib/{sidekiq-unique-jobs → sidekiq_unique_jobs}/middleware/server/unique_jobs.rb +14 -13
  25. data/lib/sidekiq_unique_jobs/payload_helper.rb +42 -0
  26. data/lib/{sidekiq-unique-jobs → sidekiq_unique_jobs}/testing.rb +2 -2
  27. data/lib/{sidekiq-unique-jobs → sidekiq_unique_jobs}/version.rb +1 -1
  28. data/sidekiq-unique-jobs.gemspec +12 -11
  29. data/spec/lib/.sidekiq_testing_enabled_spec.rb.swp +0 -0
  30. data/spec/lib/client_spec.rb +56 -44
  31. data/spec/lib/middleware/server/unique_jobs_spec.rb +16 -16
  32. data/spec/lib/sidekiq_testing_enabled_spec.rb +90 -8
  33. data/spec/lib/unlock_order_spec.rb +3 -3
  34. data/spec/spec_helper.rb +5 -6
  35. data/spec/support/my_worker.rb +3 -3
  36. data/spec/support/sidekiq_meta.rb +3 -6
  37. data/spec/support/unique_worker.rb +3 -3
  38. metadata +35 -13
  39. data/lib/sidekiq-unique-jobs.rb +0 -11
  40. data/lib/sidekiq-unique-jobs/config.rb +0 -35
  41. data/lib/sidekiq-unique-jobs/middleware/client/unique_jobs.rb +0 -101
  42. data/lib/sidekiq-unique-jobs/payload_helper.rb +0 -30
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f8c45fa6d6faaedc2eb3df2cb7adf815542acfad
4
- data.tar.gz: adba5484ba2d2162db6cbf04bb3c73527c44cfa7
3
+ metadata.gz: ccc0e1c15fcc36a44e3e7717744929a8f23b2bac
4
+ data.tar.gz: 5151f5ddab6597438efed5cadbccde7ed7bce67c
5
5
  SHA512:
6
- metadata.gz: 24640f6eaa634d5b6aa0bcc0e532e72a9d255e4b71035527ec5edc0bc76d890c7ce1b50962ce74b6e114d02106d9913c0d88844a8172f32e0c47b579c5faa627
7
- data.tar.gz: 5db2d29daa350d4b7427bf90a5728b0ce57753bb907e176b9a65546fc2c39c2bbb37b9d0dde299b02bcc6be0f63183eaa19503a0a96586318a009d1acc836c13
6
+ metadata.gz: 409b65e0c55d399a5339491b5a216c670f2b398a4d5e36df9d82d8168f388a9981b0738cb0b33caa60a63296aeee4bc567756655735711293bde97e71827e37b
7
+ data.tar.gz: d6d4528fca55ed0cda6a21215f6fdc2de33c74a7ebcd4ad4b3e2d1c74c551c9c2648ab83a983c614429faea925b9c7e9bb563a808a4ed505c961876774894745
@@ -0,0 +1,36 @@
1
+
2
+ Lint/HandleExceptions:
3
+ Enabled: true
4
+
5
+ Lint/UselessAssignment:
6
+ Enabled: true
7
+
8
+ Metrics/AbcSize:
9
+ Max: 38
10
+
11
+ Metrics/CyclomaticComplexity:
12
+ Max: 7
13
+
14
+ Metrics/LineLength:
15
+ Max: 108
16
+
17
+ Metrics/MethodLength:
18
+ Max: 13
19
+
20
+ Metrics/PerceivedComplexity:
21
+ Max: 8
22
+
23
+ Style/AccessorMethodName:
24
+ Enabled: true
25
+
26
+ Style/ConstantName:
27
+ Enabled: true
28
+
29
+ Style/Documentation:
30
+ Enabled: false
31
+
32
+ Style/FileName:
33
+ Enabled: true
34
+
35
+ Style/GlobalVars:
36
+ Enabled: true
@@ -22,6 +22,4 @@ notifications:
22
22
  - mikael@zoolutions.se
23
23
  matrix:
24
24
  allow_failures:
25
- - rvm: jruby-19mode
26
25
  - rvm: rbx-19mode
27
- - rvm: 2.0.0
data/Appraisals CHANGED
@@ -1,5 +1,6 @@
1
1
  appraise "sidekiq-develop" do
2
2
  gem 'sidekiq', github: 'mperham/sidekiq'
3
+ gem 'sidekiq', '3.0', :platform => :mri_19
3
4
  end
4
5
 
5
6
  appraise "sidekiq-2.17" do
@@ -16,4 +17,4 @@ end
16
17
 
17
18
  appraise "sidekiq-3.0" do
18
19
  gem "sidekiq", '3.0'
19
- end
20
+ end
@@ -5,10 +5,10 @@
5
5
  - Update travis with redis-server
6
6
 
7
7
  ## v2.6.5
8
- - via @sax - possibility to set which arguments should be counted as unique - https://github.com/form26/sidekiq-unique-jobs/pull/12
9
- - via @eduardosasso - possibility to set which arguments should be counted as unique - https://github.com/form26/sidekiq-unique-jobs/pull/11
10
- - via @KensoDev - configuration of default expiration - https://github.com/form26/sidekiq-unique-jobs/pull/9
8
+ - via @sax - possibility to set which arguments should be counted as unique - https://github.com/form26/sidekiq_unique_jobs/pull/12
9
+ - via @eduardosasso - possibility to set which arguments should be counted as unique - https://github.com/form26/sidekiq_unique_jobs/pull/11
10
+ - via @KensoDev - configuration of default expiration - https://github.com/form26/sidekiq_unique_jobs/pull/9
11
11
 
12
12
  ## v2.1.0
13
13
 
14
- Extracted the unique jobs portion from sidekiq main repo since @mperham dropped support for it.
14
+ Extracted the unique jobs portion from sidekiq main repo since @mperham dropped support for it.
data/Gemfile CHANGED
@@ -2,6 +2,4 @@ source 'http://rubygems.org'
2
2
  gemspec
3
3
 
4
4
  gem 'appraisal', '~> 1.0.0'
5
- gem 'pry'
6
- gem 'pry-stack_explorer'
7
- gem 'pry-debugger'
5
+ gem 'pry', platform: :mri
data/README.md CHANGED
@@ -41,12 +41,12 @@ Requiring the gem in your gemfile should be sufficient to enable unique jobs.
41
41
 
42
42
  ### Finer Control over Uniqueness
43
43
 
44
- 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.
44
+ 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.
45
45
 
46
46
  The unique_args method need to return an array of values to use for uniqueness check.
47
47
 
48
48
  ```ruby
49
- SidekiqUniqueJobs::Config.unique_args_enabled = true
49
+ SidekiqUniqueJobs.config.unique_args_enabled = true
50
50
  ```
51
51
 
52
52
  The method or the proc can return a modified version of args without the transient arguments included, as shown below:
data/Rakefile CHANGED
@@ -1,9 +1,11 @@
1
1
  #!/usr/bin/env rake
2
- require "rubygems"
3
- require "bundler/setup"
4
- require "bundler/gem_tasks"
2
+ require 'rubygems'
3
+ require 'bundler/setup'
4
+ require 'bundler/gem_tasks'
5
5
  require 'rspec/core/rake_task'
6
+ require 'rubocop/rake_task'
6
7
 
8
+ RuboCop::RakeTask.new(:style)
7
9
  RSpec::Core::RakeTask.new(:spec)
8
10
 
9
- task :default => :spec
11
+ task default: [:spec, :style]
@@ -3,9 +3,7 @@
3
3
  source "http://rubygems.org"
4
4
 
5
5
  gem "appraisal", "~> 1.0.0"
6
- gem "pry"
7
- gem "pry-stack_explorer"
8
- gem "pry-debugger"
6
+ gem "pry", :platform => :mri
9
7
  gem "sidekiq", "2.15"
10
8
 
11
9
  gemspec :path => "../"
@@ -3,9 +3,7 @@
3
3
  source "http://rubygems.org"
4
4
 
5
5
  gem "appraisal", "~> 1.0.0"
6
- gem "pry"
7
- gem "pry-stack_explorer"
8
- gem "pry-debugger"
6
+ gem "pry", :platform => :mri
9
7
  gem "sidekiq", "2.16"
10
8
 
11
9
  gemspec :path => "../"
@@ -3,9 +3,7 @@
3
3
  source "http://rubygems.org"
4
4
 
5
5
  gem "appraisal", "~> 1.0.0"
6
- gem "pry"
7
- gem "pry-stack_explorer"
8
- gem "pry-debugger"
6
+ gem "pry", :platform => :mri
9
7
  gem "sidekiq", "2.17"
10
8
 
11
9
  gemspec :path => "../"
@@ -3,9 +3,7 @@
3
3
  source "http://rubygems.org"
4
4
 
5
5
  gem "appraisal", "~> 1.0.0"
6
- gem "pry"
7
- gem "pry-stack_explorer"
8
- gem "pry-debugger"
6
+ gem "pry", :platform => :mri
9
7
  gem "sidekiq", "3.0"
10
8
 
11
9
  gemspec :path => "../"
@@ -3,9 +3,7 @@
3
3
  source "http://rubygems.org"
4
4
 
5
5
  gem "appraisal", "~> 1.0.0"
6
- gem "pry"
7
- gem "pry-stack_explorer"
8
- gem "pry-debugger"
9
- gem "sidekiq", :github => "mperham/sidekiq"
6
+ gem "pry", :platform => :mri
7
+ gem "sidekiq", "3.0", :platform => :mri_19
10
8
 
11
9
  gemspec :path => "../"
@@ -0,0 +1,27 @@
1
+ require 'yaml' if RUBY_VERSION.include?('2.0.0')
2
+ require 'sidekiq_unique_jobs/middleware'
3
+ require 'sidekiq_unique_jobs/version'
4
+ require 'sidekiq_unique_jobs/config'
5
+ require 'sidekiq_unique_jobs/payload_helper'
6
+ require 'ostruct'
7
+
8
+ module SidekiqUniqueJobs
9
+ module_function
10
+
11
+ def config
12
+ @config ||= Config.new(
13
+ unique_prefix: 'sidekiq_unique',
14
+ unique_args_enabled: false,
15
+ default_expiration: 30 * 60,
16
+ default_unlock_order: :after_yield
17
+ )
18
+ end
19
+
20
+ def unique_args_enabled?
21
+ config.unique_args_enabled
22
+ end
23
+
24
+ def configure
25
+ yield configuration
26
+ end
27
+ end
@@ -0,0 +1,47 @@
1
+ module SidekiqUniqueJobs
2
+ class Config < OpenStruct
3
+ CONFIG_ACCESSORS = [
4
+ :unique_prefix,
5
+ :unique_args_enabled,
6
+ :default_expiration,
7
+ :default_unlock_order
8
+ ]
9
+
10
+ class << self
11
+ warn('This method has been deprecated. See readme for information')
12
+ CONFIG_ACCESSORS.each do |method|
13
+ define_method(method) do
14
+ warn('This method has been deprecated. See readme for information')
15
+ config.send(method)
16
+ end
17
+
18
+ define_method("#{method}=") do |obj|
19
+ warn('This method has been deprecated. See readme for information')
20
+ config.send("#{method}=", obj)
21
+ end
22
+ end
23
+
24
+ def unique_args_enabled?
25
+ warn('This method has been deprecated. See readme for information')
26
+ config.unique_args_enabled
27
+ end
28
+
29
+ def config
30
+ SidekiqUniqueJobs.config
31
+ end
32
+ end
33
+
34
+ def testing_enabled?
35
+ if Sidekiq.const_defined?('Testing') && Sidekiq::Testing.enabled?
36
+ require 'sidekiq_unique_jobs/testing'
37
+ return true
38
+ end
39
+
40
+ false
41
+ end
42
+
43
+ def unique_args_enabled?
44
+ config.unique_args_enabled
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,16 @@
1
+ require 'sidekiq_unique_jobs/connectors/testing'
2
+ require 'sidekiq_unique_jobs/connectors/redis_pool'
3
+ require 'sidekiq_unique_jobs/connectors/sidekiq_redis'
4
+
5
+ module SidekiqUniqueJobs
6
+ module Connectors
7
+ CONNECTOR_TYPES = [Testing, RedisPool, SidekiqRedis]
8
+
9
+ def self.conn(redis_pool = nil)
10
+ CONNECTOR_TYPES.each do |connector|
11
+ conn = connector.conn(redis_pool)
12
+ return conn if conn
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,10 @@
1
+ module SidekiqUniqueJobs
2
+ module Connectors
3
+ class RedisPool
4
+ def self.conn(redis_pool = nil)
5
+ return if redis_pool.nil?
6
+ redis_pool.with { |conn| conn }
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ module SidekiqUniqueJobs
2
+ module Connectors
3
+ class SidekiqRedis
4
+ def self.conn(_redis_pool = nil)
5
+ Sidekiq.redis { |conn| conn }
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ module SidekiqUniqueJobs
2
+ module Connectors
3
+ class Testing
4
+ def self.conn(_redis_pool = nil)
5
+ return unless SidekiqUniqueJobs.config.testing_enabled?
6
+ SidekiqUniqueJobs.redis_mock { |conn| conn }
7
+ end
8
+ end
9
+ end
10
+ end
@@ -2,11 +2,11 @@ require 'sidekiq'
2
2
 
3
3
  Sidekiq.configure_server do |config|
4
4
  config.server_middleware do |chain|
5
- require 'sidekiq-unique-jobs/middleware/server/unique_jobs'
5
+ require 'sidekiq_unique_jobs/middleware/server/unique_jobs'
6
6
  chain.add SidekiqUniqueJobs::Middleware::Server::UniqueJobs
7
7
  end
8
8
  config.client_middleware do |chain|
9
- require 'sidekiq-unique-jobs/middleware/client/unique_jobs'
9
+ require 'sidekiq_unique_jobs/middleware/client/unique_jobs'
10
10
  chain.add SidekiqUniqueJobs::Middleware::Client::UniqueJobs
11
11
  end
12
12
 
@@ -14,7 +14,7 @@ end
14
14
 
15
15
  Sidekiq.configure_client do |config|
16
16
  config.client_middleware do |chain|
17
- require 'sidekiq-unique-jobs/middleware/client/unique_jobs'
17
+ require 'sidekiq_unique_jobs/middleware/client/unique_jobs'
18
18
  chain.add SidekiqUniqueJobs::Middleware::Client::UniqueJobs
19
19
  end
20
20
  end
@@ -0,0 +1,25 @@
1
+ require 'sidekiq_unique_jobs/middleware/server/unique_jobs'
2
+
3
+ module SidekiqUniqueJobs
4
+ module Middleware
5
+ module Client
6
+ module Strategies
7
+ class TestingInline < Unique
8
+ def self.elegible?
9
+ SidekiqUniqueJobs.config.testing_enabled? && Sidekiq::Testing.inline?
10
+ end
11
+
12
+ def review
13
+ _middleware.call(worker_class.new, item, queue, redis_pool) do
14
+ super
15
+ end
16
+ end
17
+
18
+ def _middleware
19
+ SidekiqUniqueJobs::Middleware::Server::UniqueJobs.new
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,72 @@
1
+ require 'digest'
2
+ require 'sidekiq_unique_jobs/connectors'
3
+
4
+ module SidekiqUniqueJobs
5
+ module Middleware
6
+ module Client
7
+ module Strategies
8
+ class Unique
9
+ def self.elegible?
10
+ true
11
+ end
12
+
13
+ def self.review(worker_class, item, queue, redis_pool = nil)
14
+ new(worker_class, item, queue, redis_pool).review { yield }
15
+ end
16
+
17
+ def initialize(worker_class, item, queue, redis_pool = nil)
18
+ @worker_class = worker_class
19
+ @item = item
20
+ @queue = queue
21
+ @redis_pool = redis_pool
22
+ end
23
+
24
+ def review
25
+ item['unique_hash'] = payload_hash
26
+ return unless unique_for_connection?
27
+ yield
28
+ end
29
+
30
+ private
31
+
32
+ attr_reader :item, :worker_class, :redis_pool, :queue
33
+
34
+ def unique_for_connection?
35
+ unique = false
36
+ conn.watch(payload_hash)
37
+
38
+ if conn.get(payload_hash).to_i == 1 ||
39
+ (conn.get(payload_hash).to_i == 2 && item['at'])
40
+ # if the job is already queued, or is already scheduled and
41
+ # we're trying to schedule again, abort
42
+ conn.unwatch
43
+ else
44
+ # if the job was previously scheduled and is now being queued,
45
+ # or we've never seen it before
46
+ expires_at = unique_job_expiration || SidekiqUniqueJobs.config.default_expiration
47
+ expires_at = ((Time.at(item['at']) - Time.now.utc) + expires_at).to_i if item['at']
48
+
49
+ unique = conn.multi do
50
+ # set value of 2 for scheduled jobs, 1 for queued jobs.
51
+ conn.setex(payload_hash, expires_at, item['at'] ? 2 : 1)
52
+ end
53
+ end
54
+ unique
55
+ end
56
+
57
+ def conn
58
+ SidekiqUniqueJobs::Connectors.conn(redis_pool)
59
+ end
60
+
61
+ def payload_hash
62
+ SidekiqUniqueJobs::PayloadHelper.get_payload(item['class'], item['queue'], item['args'])
63
+ end
64
+
65
+ def unique_job_expiration
66
+ worker_class.get_sidekiq_options['unique_job_expiration']
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,48 @@
1
+ require 'sidekiq_unique_jobs/middleware/client/strategies/unique'
2
+ require 'sidekiq_unique_jobs/middleware/client/strategies/testing_inline'
3
+
4
+ module SidekiqUniqueJobs
5
+ module Middleware
6
+ module Client
7
+ class UniqueJobs
8
+ STRATEGIES = [
9
+ Strategies::TestingInline,
10
+ Strategies::Unique
11
+ ]
12
+
13
+ attr_reader :item, :worker_class, :redis_pool
14
+
15
+ def call(worker_class, item, queue, redis_pool = nil)
16
+ @worker_class = worker_class_constantize(worker_class)
17
+ @item = item
18
+ @redis_pool = redis_pool
19
+
20
+ if unique_enabled?
21
+ strategy.review(worker_class, item, queue, redis_pool) { yield }
22
+ else
23
+ yield
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def unique_enabled?
30
+ worker_class.get_sidekiq_options['unique'] || item['unique']
31
+ end
32
+
33
+ def strategy
34
+ STRATEGIES.detect(&:elegible?)
35
+ end
36
+
37
+ # Attempt to constantize a string worker_class argument, always
38
+ # failing back to the original argument.
39
+ def worker_class_constantize(worker_class)
40
+ return worker_class unless worker_class.is_a?(String)
41
+ worker_class.constantize
42
+ rescue NameError
43
+ worker_class
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end