unique_job 0.1.0 → 0.2.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/job_history.rb +10 -6
- data/lib/unique_job/logging.rb +17 -0
- data/lib/unique_job/util.rb +8 -19
- data/lib/unique_job/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c480f4b0fe78931cf109fbca3db98032cd5bc2e1ad065dc218c56e1ac00361b5
|
4
|
+
data.tar.gz: e67b02dd3bde8bfb46d417e46bac20780f7c05eb67dcd44f93d9fda831ecde15
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 44785d034535d444ee219a7b0ad0dd064b02b85aee5289f13ba74a1c2d4fded15898ed2b974c4be4fbc5d6644bf4ba4e704a461c2f3f96b8341e93ea9aeb6dbc
|
7
|
+
data.tar.gz: bd9b4b1ef9d56ae58f724d1dc827f3747895154b0f7fd978dfd1739e02fdb547fb1d3ee1d2ea5d667e92b572ad1bc1448e579fcd181b7da53acd1c811ab16f55
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
+
require 'unique_job/logging'
|
2
|
+
|
1
3
|
module UniqueJob
|
2
4
|
class JobHistory
|
3
5
|
def initialize(worker_class, queueing_class, ttl)
|
4
|
-
@key = "#{self.class}:#{queueing_class}:#{worker_class}"
|
6
|
+
@key = "#{self.class}:#{queueing_class.name.split('::')[-1]}:#{worker_class}"
|
5
7
|
@ttl = ttl
|
6
8
|
end
|
7
9
|
|
@@ -20,7 +22,7 @@ module UniqueJob
|
|
20
22
|
end
|
21
23
|
|
22
24
|
def exists?(val)
|
23
|
-
redis.exists(key(val))
|
25
|
+
redis.exists?(key(val))
|
24
26
|
end
|
25
27
|
|
26
28
|
def add(val)
|
@@ -44,18 +46,20 @@ module UniqueJob
|
|
44
46
|
end
|
45
47
|
|
46
48
|
module RescueAllRedisErrors
|
49
|
+
include Logging
|
50
|
+
|
47
51
|
%i(
|
48
52
|
ttl
|
49
53
|
exists?
|
50
54
|
add
|
51
55
|
).each do |method_name|
|
52
56
|
define_method(method_name) do |*args, &blk|
|
53
|
-
start = Time.
|
57
|
+
start = Time.now
|
54
58
|
super(*args, &blk)
|
55
59
|
rescue => e
|
56
|
-
elapsed = Time.
|
57
|
-
|
58
|
-
|
60
|
+
elapsed = Time.now - start
|
61
|
+
logger.warn "Rescue all errors in #{self.class}##{method_name} #{e.inspect} elapsed=#{sprintf("%.3f sec", elapsed)}"
|
62
|
+
logger.debug { e.backtrace.join("\n") }
|
59
63
|
nil
|
60
64
|
end
|
61
65
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
3
|
+
module UniqueJob
|
4
|
+
module Logging
|
5
|
+
def logger
|
6
|
+
if File.basename($0) == 'rake'
|
7
|
+
Logger.new(STDOUT, level: Logger::WARN)
|
8
|
+
elsif defined?(Sidekiq)
|
9
|
+
Sidekiq.logger
|
10
|
+
elsif defined?(Rails)
|
11
|
+
Rails.logger
|
12
|
+
else
|
13
|
+
Logger.new(STDOUT, level: Logger::WARN)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/unique_job/util.rb
CHANGED
@@ -1,11 +1,16 @@
|
|
1
|
+
require 'unique_job/logging'
|
2
|
+
require 'unique_job/job_history'
|
3
|
+
|
1
4
|
module UniqueJob
|
2
5
|
module Util
|
6
|
+
include Logging
|
7
|
+
|
3
8
|
def perform_if_unique(worker, args, &block)
|
4
9
|
if worker.respond_to?(:unique_key)
|
5
10
|
unique_key = worker.unique_key(*args)
|
6
11
|
|
7
12
|
if unique_key.nil? || unique_key.to_s.empty?
|
8
|
-
logger.warn { "#{
|
13
|
+
logger.warn { "#{self.class}##{__method__} Key is blank worker=#{worker.class} args=#{truncate(args.inspect)}" }
|
9
14
|
yield
|
10
15
|
elsif perform_unique_check(worker, args, unique_key.to_s)
|
11
16
|
yield
|
@@ -19,7 +24,7 @@ module UniqueJob
|
|
19
24
|
history = job_history(worker)
|
20
25
|
|
21
26
|
if history.exists?(unique_key)
|
22
|
-
logger.info { "#{
|
27
|
+
logger.info { "#{self.class}##{__method__} Skip duplicate job for #{history.ttl} seconds, remaining #{history.ttl(unique_key)} seconds worker=#{worker.class} args=#{truncate(args.inspect)}" }
|
23
28
|
|
24
29
|
perform_callback(worker, :after_skip, args)
|
25
30
|
|
@@ -32,7 +37,7 @@ module UniqueJob
|
|
32
37
|
|
33
38
|
def job_history(worker)
|
34
39
|
ttl = worker.respond_to?(:unique_in) ? worker.unique_in : 3600
|
35
|
-
JobHistory.new(worker.class,
|
40
|
+
JobHistory.new(worker.class, self.class, ttl)
|
36
41
|
end
|
37
42
|
|
38
43
|
def truncate(text, length: 100)
|
@@ -59,21 +64,5 @@ module UniqueJob
|
|
59
64
|
end
|
60
65
|
end
|
61
66
|
end
|
62
|
-
|
63
|
-
def class_name
|
64
|
-
self.class.name.demodulize
|
65
|
-
end
|
66
|
-
|
67
|
-
def logger
|
68
|
-
if File.basename($0) == 'rake'
|
69
|
-
Logger.new(STDOUT, level: Logger::WARN)
|
70
|
-
elsif defined?(Sidekiq)
|
71
|
-
Sidekiq.logger
|
72
|
-
elsif defined?(Rails)
|
73
|
-
Rails.logger
|
74
|
-
else
|
75
|
-
Logger.new(STDOUT, level: Logger::WARN)
|
76
|
-
end
|
77
|
-
end
|
78
67
|
end
|
79
68
|
end
|
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.2.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: 2020-
|
11
|
+
date: 2020-08-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sidekiq
|
@@ -64,6 +64,7 @@ files:
|
|
64
64
|
- lib/unique_job.rb
|
65
65
|
- lib/unique_job/client_middleware.rb
|
66
66
|
- lib/unique_job/job_history.rb
|
67
|
+
- lib/unique_job/logging.rb
|
67
68
|
- lib/unique_job/server_middleware.rb
|
68
69
|
- lib/unique_job/util.rb
|
69
70
|
- lib/unique_job/version.rb
|