unique_job 0.3.1.pre → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 10d542ca656951df33aad35ccc2bc6a7084e817440b5350589f29d318200be78
4
- data.tar.gz: 1c029a320d84f7dc509a0315c1420e07fac331119ea643b58b391516c0f14804
3
+ metadata.gz: 01dffe52b964b3fc02be0786f8b7f8ebd8345a02640344a1125a8fc16317ce0c
4
+ data.tar.gz: 2acd9dcd223fbaac2933f2ddc6c9572d0c259dfb5395b73df83660de12a3e593
5
5
  SHA512:
6
- metadata.gz: 99507fb879e9708a1488081d7cc5b6115c77f2b38915a3a23e162b2b862ea090d5ffd368ac5fb777629523218529acf5635bc9fa47156b9c5de6e1d4e746393f
7
- data.tar.gz: 34d3309e4c134e5e8b03bf931473b84722d9393fc42d581acde96bdf7e268b410a38073ba25b2b8d797e8394df6ca344bee3ed2eb5ed93905cea84a1f9721b4a
6
+ metadata.gz: f03e34e0838f166986a4890519f38dbc6c80f4df8c0034e8a61179775a7b0013f4ab51bb5f54e1651f06e0997017a21da913fcbf9d3a06a90f8570ebcaf6fcb7
7
+ data.tar.gz: 492759ef2b1b96def0b35e0b2a315ec46b2e5004ed9882728998f741d6f4a10a3dda047413d570caf7e3e5a5acad75f15da7e5b02b48c33099a793888521bfe9
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ -fd
2
+ --require spec_helper
data/Gemfile CHANGED
@@ -4,4 +4,4 @@ source "https://rubygems.org"
4
4
  gemspec
5
5
 
6
6
  gem "rake", "~> 12.0"
7
- gem "minitest", "~> 5.0"
7
+ gem "rspec"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- unique_job (0.3.1.pre)
4
+ unique_job (0.4.2)
5
5
  redis
6
6
  sidekiq (> 6.0, < 7.0)
7
7
 
@@ -9,10 +9,23 @@ GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
11
  connection_pool (2.2.3)
12
- minitest (5.14.2)
12
+ diff-lcs (1.4.4)
13
13
  rack (2.2.3)
14
14
  rake (12.3.3)
15
15
  redis (4.2.2)
16
+ rspec (3.10.0)
17
+ rspec-core (~> 3.10.0)
18
+ rspec-expectations (~> 3.10.0)
19
+ rspec-mocks (~> 3.10.0)
20
+ rspec-core (3.10.1)
21
+ rspec-support (~> 3.10.0)
22
+ rspec-expectations (3.10.1)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.10.0)
25
+ rspec-mocks (3.10.2)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.10.0)
28
+ rspec-support (3.10.3)
16
29
  sidekiq (6.1.2)
17
30
  connection_pool (>= 2.2.2)
18
31
  rack (~> 2.0)
@@ -22,8 +35,8 @@ PLATFORMS
22
35
  ruby
23
36
 
24
37
  DEPENDENCIES
25
- minitest (~> 5.0)
26
38
  rake (~> 12.0)
39
+ rspec
27
40
  unique_job!
28
41
 
29
42
  BUNDLED WITH
@@ -4,6 +4,10 @@ module UniqueJob
4
4
  class ClientMiddleware
5
5
  include Util
6
6
 
7
+ def initialize(redis_options)
8
+ @redis_options = redis_options
9
+ end
10
+
7
11
  def call(worker_str, job, queue, redis_pool, &block)
8
12
  if job.has_key?('at')
9
13
  # perform_in or perform_at
@@ -40,8 +40,10 @@ module UniqueJob
40
40
  end
41
41
 
42
42
  class << self
43
+ attr_accessor :redis_options
44
+
43
45
  def redis
44
- @redis ||= Redis.client(ENV['REDIS_HOST'])
46
+ @redis ||= Redis.new(redis_options)
45
47
  end
46
48
  end
47
49
 
@@ -58,7 +60,7 @@ module UniqueJob
58
60
  super(*args, &blk)
59
61
  rescue => e
60
62
  elapsed = Time.now - start
61
- logger.warn "Rescue all errors in #{self.class}##{method_name} #{e.inspect} elapsed=#{sprintf("%.3f sec", elapsed)}"
63
+ logger.warn "[UniqueJob] Rescue all errors in #{self.class}##{method_name} #{e.inspect} elapsed=#{sprintf("%.3f sec", elapsed)}"
62
64
  logger.debug { e.backtrace.join("\n") }
63
65
  nil
64
66
  end
@@ -4,6 +4,10 @@ module UniqueJob
4
4
  class ServerMiddleware
5
5
  include Util
6
6
 
7
+ def initialize(redis_options)
8
+ @redis_options = redis_options
9
+ end
10
+
7
11
  def call(worker, msg, queue, &block)
8
12
  perform_if_unique(worker, msg['args'], &block)
9
13
  end
@@ -10,10 +10,7 @@ module UniqueJob
10
10
  unique_key = worker.unique_key(*args)
11
11
  logger.debug { "[UniqueJob] Calculate unique key worker=#{worker.class} key=#{unique_key}" }
12
12
 
13
- if unique_key.nil? || unique_key.to_s.empty?
14
- logger.warn { "[UniqueJob] Don't check a job with a blank key worker=#{worker.class} key=#{unique_key}" }
15
- yield
16
- elsif check_uniqueness(worker, unique_key.to_s)
13
+ if check_uniqueness(worker, unique_key)
17
14
  yield
18
15
  else
19
16
  logger.debug { "[UniqueJob] Duplicate job skipped worker=#{worker.class} key=#{unique_key}" }
@@ -25,19 +22,25 @@ module UniqueJob
25
22
  end
26
23
  end
27
24
 
28
- def check_uniqueness(worker, unique_key)
25
+ def check_uniqueness(worker, key)
26
+ if key.nil? || key.to_s.empty?
27
+ logger.warn { "[UniqueJob] Don't check a job with a blank key worker=#{worker.class} key=#{key}" }
28
+ return false
29
+ end
30
+
29
31
  history = job_history(worker)
30
32
 
31
- if history.exists?(unique_key)
33
+ if history.exists?(key)
32
34
  false
33
35
  else
34
- history.add(unique_key)
36
+ history.add(key)
35
37
  true
36
38
  end
37
39
  end
38
40
 
39
41
  def job_history(worker)
40
42
  ttl = worker.respond_to?(:unique_in) ? worker.unique_in : 3600
43
+ JobHistory.redis_options = @redis_options
41
44
  JobHistory.new(worker.class, self.class, ttl)
42
45
  end
43
46
 
@@ -1,3 +1,3 @@
1
1
  module UniqueJob
2
- VERSION = "0.3.1.pre"
2
+ VERSION = "0.4.2"
3
3
  end
data/lib/unique_job.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'redis'
2
+
1
3
  require "unique_job/client_middleware"
2
4
  require "unique_job/job_history"
3
5
  require "unique_job/server_middleware"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unique_job
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1.pre
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - ts-3156
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-11-25 00:00:00.000000000 Z
11
+ date: 2021-11-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sidekiq
@@ -52,6 +52,7 @@ extensions: []
52
52
  extra_rdoc_files: []
53
53
  files:
54
54
  - ".gitignore"
55
+ - ".rspec"
55
56
  - ".ruby-version"
56
57
  - ".travis.yml"
57
58
  - CODE_OF_CONDUCT.md
@@ -88,9 +89,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
88
89
  version: 2.3.0
89
90
  required_rubygems_version: !ruby/object:Gem::Requirement
90
91
  requirements:
91
- - - ">"
92
+ - - ">="
92
93
  - !ruby/object:Gem::Version
93
- version: 1.3.1
94
+ version: '0'
94
95
  requirements: []
95
96
  rubygems_version: 3.1.6
96
97
  signing_key: