unique_job 0.4.4 → 0.5.0

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: 3a0a010e61296a6a1fdf584470177a01db0325a899ee6a6cae9e683b6a498b5c
4
- data.tar.gz: 077e870580c431a04c1c73797b8cf4fe8364289f3a11c4d15788045f65f069be
3
+ metadata.gz: 13b17d33adcafb81a0e16d41f3563b9b251b73411d36daa6210c8cc734c97ab5
4
+ data.tar.gz: cf525feb70cafc0c5a2e07b4b47dcb2280d666bec15e81f0604f34a49f93b5fd
5
5
  SHA512:
6
- metadata.gz: 9cca947e010dec20bf88146f014caa1021091034ce1709e4e1f60db189da51aefc198a3fb53bde6d1be4206271576fe87ef012f7d6533f9f272ee3755b0585fc
7
- data.tar.gz: ce2b3b6191f93fee6dcaaf9ddeac62750d4d3cbb1218a6540fd47367ce74dea0370a05793d61840c645f9e4377f978f091f2a558f8f2fb4434985f449ecb3b74
6
+ metadata.gz: edb0b4aab14acbe9f07f3db3bf178fae140f05cc18b09f22f0d3639d74c45a7462d10e837a4862bee93737404d948e01d6cc8ac4a4c4aa25a579ab2cb30d4a86
7
+ data.tar.gz: 4e58bc946c6421616ff7397dea2f18a28dd9dcc0855842388a73027fd95168384942366d89acad8385013e431b4e95a026723213efe1e26aca58ef3936027c0d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- unique_job (0.4.4)
4
+ unique_job (0.5.0)
5
5
  redis
6
6
  sidekiq (> 6.0, < 7.0)
7
7
 
@@ -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)
@@ -2,77 +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
- MX = Mutex.new
46
-
47
- def redis
48
- MX.synchronize do
49
- unless @redis
50
- @redis = Redis.new(redis_options)
51
- end
52
- end
53
- @redis
54
- end
55
- end
56
-
57
- module RescueAllRedisErrors
58
- include Logging
59
-
60
- %i(
61
- ttl
62
- exists?
63
- add
64
- ).each do |method_name|
65
- define_method(method_name) do |*args, &blk|
66
- start = Time.now
67
- super(*args, &blk)
68
- rescue => e
69
- elapsed = Time.now - start
70
- logger.warn "[UniqueJob] Rescue all errors in #{self.class}##{method_name} #{e.inspect} elapsed=#{sprintf("%.3f sec", elapsed)}"
71
- logger.debug { e.backtrace.join("\n") }
72
- nil
73
- end
74
- end
28
+ def key(v1, v2)
29
+ "#{@key_prefix}:#{v1}:#{v2}"
75
30
  end
76
- prepend RescueAllRedisErrors
77
31
  end
78
32
  end
@@ -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 = 'Server'
9
10
  end
10
11
 
11
12
  def call(worker, msg, queue, &block)
@@ -7,44 +7,29 @@ module UniqueJob
7
7
 
8
8
  def perform(worker, job, &block)
9
9
  if worker.respond_to?(:unique_key)
10
- unique_key = worker.unique_key(*job['args'])
11
- logger.debug { "[UniqueJob] Unique key calculated worker=#{job['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 unique?(worker, unique_key)
14
- elsif unique?(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}" }
15
15
  yield
16
16
  else
17
- logger.debug { "[UniqueJob] Duplicate job skipped worker=#{job['class']} key=#{unique_key}" }
18
- perform_callback(worker, :after_skip, job['args'])
19
- 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
20
27
  end
21
28
  else
22
29
  yield
23
30
  end
24
31
  end
25
32
 
26
- def unique?(worker, key)
27
- if key.nil? || key.to_s.empty?
28
- logger.warn { "[UniqueJob] Don't check a job with a blank key worker=#{worker.class} key=#{key}" }
29
- return false
30
- end
31
-
32
- history = job_history(worker)
33
-
34
- if history.exists?(key)
35
- false
36
- else
37
- history.add(key)
38
- true
39
- end
40
- end
41
-
42
- def job_history(worker)
43
- ttl = worker.respond_to?(:unique_in) ? worker.unique_in : 3600
44
- JobHistory.redis_options = @redis_options
45
- JobHistory.new(worker.class, self.class, ttl)
46
- end
47
-
48
33
  def perform_callback(worker, callback_name, args)
49
34
  if worker.respond_to?(callback_name)
50
35
  parameters = worker.method(callback_name).parameters
@@ -1,3 +1,3 @@
1
1
  module UniqueJob
2
- VERSION = "0.4.4"
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.4
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ts-3156
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-07-14 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