unique_job 0.4.2 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 01dffe52b964b3fc02be0786f8b7f8ebd8345a02640344a1125a8fc16317ce0c
4
- data.tar.gz: 2acd9dcd223fbaac2933f2ddc6c9572d0c259dfb5395b73df83660de12a3e593
3
+ metadata.gz: 13b17d33adcafb81a0e16d41f3563b9b251b73411d36daa6210c8cc734c97ab5
4
+ data.tar.gz: cf525feb70cafc0c5a2e07b4b47dcb2280d666bec15e81f0604f34a49f93b5fd
5
5
  SHA512:
6
- metadata.gz: f03e34e0838f166986a4890519f38dbc6c80f4df8c0034e8a61179775a7b0013f4ab51bb5f54e1651f06e0997017a21da913fcbf9d3a06a90f8570ebcaf6fcb7
7
- data.tar.gz: 492759ef2b1b96def0b35e0b2a315ec46b2e5004ed9882728998f741d6f4a10a3dda047413d570caf7e3e5a5acad75f15da7e5b02b48c33099a793888521bfe9
6
+ metadata.gz: edb0b4aab14acbe9f07f3db3bf178fae140f05cc18b09f22f0d3639d74c45a7462d10e837a4862bee93737404d948e01d6cc8ac4a4c4aa25a579ab2cb30d4a86
7
+ data.tar.gz: 4e58bc946c6421616ff7397dea2f18a28dd9dcc0855842388a73027fd95168384942366d89acad8385013e431b4e95a026723213efe1e26aca58ef3936027c0d
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.7.4
1
+ 3.0.2
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- unique_job (0.4.2)
4
+ unique_job (0.5.0)
5
5
  redis
6
6
  sidekiq (> 6.0, < 7.0)
7
7
 
@@ -40,4 +40,4 @@ DEPENDENCIES
40
40
  unique_job!
41
41
 
42
42
  BUNDLED WITH
43
- 2.1.4
43
+ 2.2.22
@@ -5,7 +5,8 @@ module UniqueJob
5
5
  include Util
6
6
 
7
7
  def initialize(redis_options)
8
- @redis_options = redis_options
8
+ @history = JobHistory.new(self.class.name, Redis.new(redis_options))
9
+ @context = 'Client'
9
10
  end
10
11
 
11
12
  def call(worker_str, job, queue, redis_pool, &block)
@@ -18,7 +19,7 @@ module UniqueJob
18
19
  else
19
20
  worker = worker_str.new
20
21
  end
21
- perform_if_unique(worker, job['args'], &block)
22
+ perform(worker, job, &block)
22
23
  end
23
24
  end
24
25
  end
@@ -2,70 +2,31 @@ require 'unique_job/logging'
2
2
 
3
3
  module UniqueJob
4
4
  class JobHistory
5
- def initialize(worker_class, queueing_class, ttl)
6
- @key = "#{self.class}:#{queueing_class.name.split('::')[-1]}:#{worker_class}"
7
- @ttl = ttl
8
- end
9
-
10
- def ttl(val = nil)
11
- if val
12
- redis.ttl(key(val))
13
- else
14
- @ttl
15
- end
16
- end
5
+ include Logging
17
6
 
18
- def delete_all
19
- redis.keys("#{@key}:*").each do |key|
20
- redis.del(key)
21
- end
7
+ def initialize(middleware_name, redis)
8
+ @key_prefix = "#{self.class}:#{middleware_name.split('::')[-1]}"
9
+ @redis = redis
22
10
  end
23
11
 
24
- def exists?(val)
25
- redis.exists?(key(val))
12
+ def exists?(v1, v2)
13
+ @redis.exists?(key(v1, v2))
14
+ rescue => e
15
+ logger.warn { "[UniqueJob] Redis#exists? failed v1=#{v1} v2=#{v2} exception=#{e.inspect}" }
16
+ nil
26
17
  end
27
18
 
28
- def add(val)
29
- redis.setex(key(val), @ttl, true)
19
+ def add(v1, v2, ttl)
20
+ @redis.setex(key(v1, v2), ttl, true)
21
+ rescue => e
22
+ logger.warn { "[UniqueJob] Redis#setex failed v1=#{v1} v2=#{v2} ttl=#{ttl} exception=#{e.inspect}" }
23
+ nil
30
24
  end
31
25
 
32
26
  private
33
27
 
34
- def key(val)
35
- "#{@key}:#{val}"
36
- end
37
-
38
- def redis
39
- self.class.redis
40
- end
41
-
42
- class << self
43
- attr_accessor :redis_options
44
-
45
- def redis
46
- @redis ||= Redis.new(redis_options)
47
- end
48
- end
49
-
50
- module RescueAllRedisErrors
51
- include Logging
52
-
53
- %i(
54
- ttl
55
- exists?
56
- add
57
- ).each do |method_name|
58
- define_method(method_name) do |*args, &blk|
59
- start = Time.now
60
- super(*args, &blk)
61
- rescue => e
62
- elapsed = Time.now - start
63
- logger.warn "[UniqueJob] Rescue all errors in #{self.class}##{method_name} #{e.inspect} elapsed=#{sprintf("%.3f sec", elapsed)}"
64
- logger.debug { e.backtrace.join("\n") }
65
- nil
66
- end
67
- end
28
+ def key(v1, v2)
29
+ "#{@key_prefix}:#{v1}:#{v2}"
68
30
  end
69
- prepend RescueAllRedisErrors
70
31
  end
71
32
  end
@@ -5,11 +5,12 @@ module UniqueJob
5
5
  include Util
6
6
 
7
7
  def initialize(redis_options)
8
- @redis_options = redis_options
8
+ @history = JobHistory.new(self.class.name, Redis.new(redis_options))
9
+ @context = 'Server'
9
10
  end
10
11
 
11
12
  def call(worker, msg, queue, &block)
12
- perform_if_unique(worker, msg['args'], &block)
13
+ perform(worker, msg, &block)
13
14
  end
14
15
  end
15
16
  end
@@ -5,53 +5,31 @@ module UniqueJob
5
5
  module Util
6
6
  include Logging
7
7
 
8
- def perform_if_unique(worker, args, &block)
8
+ def perform(worker, job, &block)
9
9
  if worker.respond_to?(:unique_key)
10
- unique_key = worker.unique_key(*args)
11
- logger.debug { "[UniqueJob] Calculate unique key worker=#{worker.class} key=#{unique_key}" }
10
+ key = worker.unique_key(*job['args'])
11
+ logger.debug { "[UniqueJob] Unique key calculated context=#{@context} worker=#{job['class']} key=#{key}" }
12
12
 
13
- if check_uniqueness(worker, unique_key)
13
+ if key.nil? || key.to_s.empty?
14
+ logger.warn { "[UniqueJob] Skip history check context=#{@context} worker=#{job['class']} key=#{key}" }
14
15
  yield
15
16
  else
16
- logger.debug { "[UniqueJob] Duplicate job skipped worker=#{worker.class} key=#{unique_key}" }
17
- perform_callback(worker, :after_skip, args)
18
- nil
17
+ if @history.exists?(job['class'], key)
18
+ logger.info { "[UniqueJob] Duplicate job skipped context=#{@context} worker=#{job['class']} key=#{key}" }
19
+ perform_callback(worker, :after_skip, job['args'])
20
+ nil
21
+ else
22
+ logger.debug { "[UniqueJob] Start job context=#{@context} worker=#{job['class']} key=#{key}" }
23
+ ttl = worker.respond_to?(:unique_in) ? worker.unique_in : 3600
24
+ @history.add(job['class'], key, ttl)
25
+ yield
26
+ end
19
27
  end
20
28
  else
21
29
  yield
22
30
  end
23
31
  end
24
32
 
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
-
31
- history = job_history(worker)
32
-
33
- if history.exists?(key)
34
- false
35
- else
36
- history.add(key)
37
- true
38
- end
39
- end
40
-
41
- def job_history(worker)
42
- ttl = worker.respond_to?(:unique_in) ? worker.unique_in : 3600
43
- JobHistory.redis_options = @redis_options
44
- JobHistory.new(worker.class, self.class, ttl)
45
- end
46
-
47
- def truncate(text, length: 100)
48
- if text.length > length
49
- text.slice(0, length)
50
- else
51
- text
52
- end
53
- end
54
-
55
33
  def perform_callback(worker, callback_name, args)
56
34
  if worker.respond_to?(callback_name)
57
35
  parameters = worker.method(callback_name).parameters
@@ -1,3 +1,3 @@
1
1
  module UniqueJob
2
- VERSION = "0.4.2"
2
+ VERSION = "0.5.0"
3
3
  end
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.4.2
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ts-3156
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-11-26 00:00:00.000000000 Z
11
+ date: 2022-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sidekiq
@@ -78,7 +78,7 @@ metadata:
78
78
  homepage_uri: https://github.com/egotter/unique_job
79
79
  source_code_uri: https://github.com/egotter/unique_job
80
80
  changelog_uri: https://github.com/egotter/unique_job
81
- post_install_message:
81
+ post_install_message:
82
82
  rdoc_options: []
83
83
  require_paths:
84
84
  - lib
@@ -93,8 +93,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  - !ruby/object:Gem::Version
94
94
  version: '0'
95
95
  requirements: []
96
- rubygems_version: 3.1.6
97
- signing_key:
96
+ rubygems_version: 3.2.22
97
+ signing_key:
98
98
  specification_version: 4
99
99
  summary: Set a uniqueness to Sidekiq jobs
100
100
  test_files: []