unique_job 0.4.2 → 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/.ruby-version +1 -1
- data/Gemfile.lock +2 -2
- data/lib/unique_job/client_middleware.rb +3 -2
- data/lib/unique_job/job_history.rb +16 -55
- data/lib/unique_job/server_middleware.rb +3 -2
- data/lib/unique_job/util.rb +15 -37
- data/lib/unique_job/version.rb +1 -1
- metadata +6 -6
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/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
3.0.2
|
data/Gemfile.lock
CHANGED
@@ -5,7 +5,8 @@ module UniqueJob
|
|
5
5
|
include Util
|
6
6
|
|
7
7
|
def initialize(redis_options)
|
8
|
-
@
|
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
|
-
|
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
|
-
|
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
|
-
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
|
-
@
|
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
|
-
|
13
|
+
perform(worker, msg, &block)
|
13
14
|
end
|
14
15
|
end
|
15
16
|
end
|
data/lib/unique_job/util.rb
CHANGED
@@ -5,53 +5,31 @@ module UniqueJob
|
|
5
5
|
module Util
|
6
6
|
include Logging
|
7
7
|
|
8
|
-
def
|
8
|
+
def perform(worker, job, &block)
|
9
9
|
if worker.respond_to?(:unique_key)
|
10
|
-
|
11
|
-
logger.debug { "[UniqueJob]
|
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
|
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
|
-
|
17
|
-
|
18
|
-
|
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
|
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
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
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.
|
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: []
|