unique_job 0.1.0 → 0.3.1.pre

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: af33cb02d39f50c4d16316374ee2079b863505321370817e1df1a293d5b6674f
4
- data.tar.gz: '02906948d9857c66215b6984ff2966e766bde452eb375f08c71f01739b3dbf25'
3
+ metadata.gz: 10d542ca656951df33aad35ccc2bc6a7084e817440b5350589f29d318200be78
4
+ data.tar.gz: 1c029a320d84f7dc509a0315c1420e07fac331119ea643b58b391516c0f14804
5
5
  SHA512:
6
- metadata.gz: 6e53fde574f9343471ccdbbf72a9a3562e4a8c95950968ea55f1cbb5f774989b8ab5f781dd129f299817d4e45dbfaa9fe4ae42b3aa9b5b86dd4475c4d6d6ab50
7
- data.tar.gz: cd1f0aff1da071006a5c9c003882650bfb2d534b0c1bc455469fddfdc3de60b889e404ddb072426f155ea944315bcd88c9e0ecb24009b0e011fdd4c878b30347
6
+ metadata.gz: 99507fb879e9708a1488081d7cc5b6115c77f2b38915a3a23e162b2b862ea090d5ffd368ac5fb777629523218529acf5635bc9fa47156b9c5de6e1d4e746393f
7
+ data.tar.gz: 34d3309e4c134e5e8b03bf931473b84722d9393fc42d581acde96bdf7e268b410a38073ba25b2b8d797e8394df6ca344bee3ed2eb5ed93905cea84a1f9721b4a
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.4
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- unique_job (0.1.0)
4
+ unique_job (0.3.1.pre)
5
5
  redis
6
6
  sidekiq (> 6.0, < 7.0)
7
7
 
@@ -9,17 +9,14 @@ GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
11
  connection_pool (2.2.3)
12
- minitest (5.11.3)
13
- rack (2.2.2)
14
- rack-protection (2.0.8.1)
15
- rack
16
- rake (12.3.2)
17
- redis (4.2.1)
18
- sidekiq (6.0.7)
12
+ minitest (5.14.2)
13
+ rack (2.2.3)
14
+ rake (12.3.3)
15
+ redis (4.2.2)
16
+ sidekiq (6.1.2)
19
17
  connection_pool (>= 2.2.2)
20
18
  rack (~> 2.0)
21
- rack-protection (>= 2.0.0)
22
- redis (>= 4.1.0)
19
+ redis (>= 4.2.0)
23
20
 
24
21
  PLATFORMS
25
22
  ruby
@@ -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.zone.now
57
+ start = Time.now
54
58
  super(*args, &blk)
55
59
  rescue => e
56
- elapsed = Time.zone.now - start
57
- Rails.logger.warn "Rescue all errors in #{self.class}##{method_name} #{e.inspect} elapsed=#{sprintf("%.3f sec", elapsed)}"
58
- Rails.logger.debug { e.backtrace.join("\n") }
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
@@ -1,28 +1,34 @@
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)
11
+ logger.debug { "[UniqueJob] Calculate unique key worker=#{worker.class} key=#{unique_key}" }
6
12
 
7
13
  if unique_key.nil? || unique_key.to_s.empty?
8
- logger.warn { "#{class_name}##{__method__} Key is blank worker=#{worker.class} args=#{truncate(args.inspect)}" }
14
+ logger.warn { "[UniqueJob] Don't check a job with a blank key worker=#{worker.class} key=#{unique_key}" }
9
15
  yield
10
- elsif perform_unique_check(worker, args, unique_key.to_s)
16
+ elsif check_uniqueness(worker, unique_key.to_s)
11
17
  yield
18
+ else
19
+ logger.debug { "[UniqueJob] Duplicate job skipped worker=#{worker.class} key=#{unique_key}" }
20
+ perform_callback(worker, :after_skip, args)
21
+ nil
12
22
  end
13
23
  else
14
24
  yield
15
25
  end
16
26
  end
17
27
 
18
- def perform_unique_check(worker, args, unique_key)
28
+ def check_uniqueness(worker, unique_key)
19
29
  history = job_history(worker)
20
30
 
21
31
  if history.exists?(unique_key)
22
- logger.info { "#{class_name}##{__method__} Skip duplicate job for #{history.ttl} seconds, remaining #{history.ttl(unique_key)} seconds worker=#{worker.class} args=#{truncate(args.inspect)}" }
23
-
24
- perform_callback(worker, :after_skip, args)
25
-
26
32
  false
27
33
  else
28
34
  history.add(unique_key)
@@ -32,7 +38,7 @@ module UniqueJob
32
38
 
33
39
  def job_history(worker)
34
40
  ttl = worker.respond_to?(:unique_in) ? worker.unique_in : 3600
35
- JobHistory.new(worker.class, class_name, ttl)
41
+ JobHistory.new(worker.class, self.class, ttl)
36
42
  end
37
43
 
38
44
  def truncate(text, length: 100)
@@ -54,26 +60,9 @@ module UniqueJob
54
60
  worker.send(callback_name, *args)
55
61
  end
56
62
  rescue ArgumentError => e
57
- message = "The number of parameters of the callback method (#{parameters.size}) is not the same as the number of arguments (#{args.size})"
58
- raise ArgumentError.new("#{self.class}:#{worker.class} #{message} callback_name=#{callback_name} args=#{args.inspect} parameters=#{parameters.inspect}")
63
+ raise ArgumentError.new("[UniqueJob] Invalid parameters callback=#{callback_name}")
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
@@ -1,3 +1,3 @@
1
1
  module UniqueJob
2
- VERSION = "0.1.0"
2
+ VERSION = "0.3.1.pre"
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.1.0
4
+ version: 0.3.1.pre
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-06-14 00:00:00.000000000 Z
11
+ date: 2021-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sidekiq
@@ -52,6 +52,7 @@ extensions: []
52
52
  extra_rdoc_files: []
53
53
  files:
54
54
  - ".gitignore"
55
+ - ".ruby-version"
55
56
  - ".travis.yml"
56
57
  - CODE_OF_CONDUCT.md
57
58
  - Gemfile
@@ -64,6 +65,7 @@ files:
64
65
  - lib/unique_job.rb
65
66
  - lib/unique_job/client_middleware.rb
66
67
  - lib/unique_job/job_history.rb
68
+ - lib/unique_job/logging.rb
67
69
  - lib/unique_job/server_middleware.rb
68
70
  - lib/unique_job/util.rb
69
71
  - lib/unique_job/version.rb
@@ -86,12 +88,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
86
88
  version: 2.3.0
87
89
  required_rubygems_version: !ruby/object:Gem::Requirement
88
90
  requirements:
89
- - - ">="
91
+ - - ">"
90
92
  - !ruby/object:Gem::Version
91
- version: '0'
93
+ version: 1.3.1
92
94
  requirements: []
93
- rubyforge_project:
94
- rubygems_version: 2.7.6
95
+ rubygems_version: 3.1.6
95
96
  signing_key:
96
97
  specification_version: 4
97
98
  summary: Set a uniqueness to Sidekiq jobs