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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/unique_job/client_middleware.rb +2 -1
- data/lib/unique_job/job_history.rb +16 -62
- data/lib/unique_job/server_middleware.rb +2 -1
- data/lib/unique_job/util.rb +14 -29
- data/lib/unique_job/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13b17d33adcafb81a0e16d41f3563b9b251b73411d36daa6210c8cc734c97ab5
|
4
|
+
data.tar.gz: cf525feb70cafc0c5a2e07b4b47dcb2280d666bec15e81f0604f34a49f93b5fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: edb0b4aab14acbe9f07f3db3bf178fae140f05cc18b09f22f0d3639d74c45a7462d10e837a4862bee93737404d948e01d6cc8ac4a4c4aa25a579ab2cb30d4a86
|
7
|
+
data.tar.gz: 4e58bc946c6421616ff7397dea2f18a28dd9dcc0855842388a73027fd95168384942366d89acad8385013e431b4e95a026723213efe1e26aca58ef3936027c0d
|
data/Gemfile.lock
CHANGED
@@ -2,77 +2,31 @@ require 'unique_job/logging'
|
|
2
2
|
|
3
3
|
module UniqueJob
|
4
4
|
class JobHistory
|
5
|
-
|
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
|
19
|
-
|
20
|
-
|
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?(
|
25
|
-
redis.exists?(key(
|
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(
|
29
|
-
redis.setex(key(
|
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(
|
35
|
-
"#{@
|
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
|
data/lib/unique_job/util.rb
CHANGED
@@ -7,44 +7,29 @@ module UniqueJob
|
|
7
7
|
|
8
8
|
def perform(worker, job, &block)
|
9
9
|
if worker.respond_to?(:unique_key)
|
10
|
-
|
11
|
-
logger.debug { "[UniqueJob] Unique key calculated worker=#{job['class']} 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
|
14
|
-
|
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
|
-
|
18
|
-
|
19
|
-
|
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
|
data/lib/unique_job/version.rb
CHANGED
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
|
+
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-
|
11
|
+
date: 2022-07-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sidekiq
|