sidekiq-max-jobs 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ae8b389a6961487e91003baca5d13e15237f3cff0732cd789a57ead24781deba
4
- data.tar.gz: 3aaedb46b7abb7f78cb0794bb759561ea632b208787ae8218a9a75d8ac511856
3
+ metadata.gz: 88185f0bd0aa5075fae604f768a9b011edf6c4d349bb8d5fe32a177488bca55a
4
+ data.tar.gz: a0fde7488aa0115cd4402c07508922cfe91faa7cf57e5589297432522f3fd97f
5
5
  SHA512:
6
- metadata.gz: 5e40e4105fededa4a8b4b3513673bd3fcbbdb83e6ae9c0cf064cfa30bc32d66ccca61f9dbcedbbce91ccd4c40312af6a5dc90bcc057f74e663d72c2cc47b848c
7
- data.tar.gz: 632290222af419e9ddb70e86bb58adb20e1a4bd5123079f816d81fd03ad676e5c27939852b6c11378a09a733c451550cb78735ceb4ad49efebbe8197f06f3f01
6
+ metadata.gz: 675e4bb41052bfd805c2abb42686cad52bcb6a3fd8f0e70dac7974571b495dc8a3acb7fd3a4f249badc845e8c83ba2423034345d683ad0f0da19ee021b983753
7
+ data.tar.gz: 36742d445b462d6539a11c4d99e35503d32754d70c0096b9825593d1dac0ddee5815b6adadd7d6e98a6d07b1afc167f9f74d7ba424fc7e4ebafe16eb87c98fb7
@@ -1,6 +1,6 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2020-06-09 22:11:35 -0500 using RuboCop version 0.85.1.
3
+ # on 2020-06-17 19:26:25 -0500 using RuboCop version 0.85.1.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
@@ -22,6 +22,11 @@ Metrics/AbcSize:
22
22
  Metrics/BlockLength:
23
23
  Max: 26
24
24
 
25
+ # Offense count: 1
26
+ # Configuration parameters: CountComments.
27
+ Metrics/ClassLength:
28
+ Max: 102
29
+
25
30
  # Offense count: 1
26
31
  # Configuration parameters: CountComments, ExcludedMethods.
27
32
  Metrics/MethodLength:
data/README.md CHANGED
@@ -66,7 +66,14 @@ behavior (currently only configurable via the environment):
66
66
  * `MAX_JOBS_JITTER`: Used as the upper-bound for calculating a random number
67
67
  between 1 and the value specified. This value is added to the `MAX_JOBS` value,
68
68
  mentioned above, to decrease the likelihood that all of your `Worker(s)`
69
- restart at / around the same time (default: `1`)
69
+ restart at / around the same time (default: `rand(1)`)
70
+ * `MAX_JOBS_<QUEUE>`: The number of jobs to process for a specific queue before
71
+ terminating (default: `(ENV['MAX_JOBS'] || 100).to_i`)
72
+ * `MAX_JOBS_JITTER_<QUEUE>`: Used as the upper-bound for calculating a random
73
+ number between 1 and the value specified. This value is added to the
74
+ `MAX_JOBS_<QUEUE>` value, mentioned above, to decreased the likelihood that all
75
+ of your `Worker(s)` restart at / around the same time (default:
76
+ `rand((ENV['MAX_JOBS_JITTER'] || 1).to_i)`)
70
77
 
71
78
  Contributing
72
79
  ------------
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.4
@@ -16,12 +16,26 @@ module Sidekiq
16
16
  ).strip
17
17
 
18
18
  class << self
19
- def counter
20
- @counter ||= 0
19
+ def cache
20
+ @cache ||= {}
21
21
  end
22
22
 
23
- def increment_counter!
24
- @counter = counter.next
23
+ def counter(queue)
24
+ key = counter_key(queue)
25
+ return cache[key] if cache.include?(key)
26
+
27
+ cache[key] = 0
28
+ end
29
+
30
+ def counter_key(queue)
31
+ "COUNTER_#{queue.upcase}"
32
+ end
33
+
34
+ def increment_counter!(queue)
35
+ key = counter_key(queue)
36
+ counter = cache[key] || 0
37
+
38
+ cache[key] = counter.next
25
39
  end
26
40
 
27
41
  def log_info(message)
@@ -29,23 +43,61 @@ module Sidekiq
29
43
  end
30
44
 
31
45
  def log_initialization!
32
- log_info("Max-Jobs middleware enabled, shutting down pid: #{pid} after: #{max_jobs_with_jitter} job(s)")
46
+ log_info("Max-Jobs middleware enabled, shutting down pid: #{pid} after max-jobs threshold reached")
47
+ end
48
+
49
+ def max_jobs(queue)
50
+ key = max_jobs_key(queue)
51
+ return cache[key] if cache.include?(key)
52
+
53
+ cache[key] = (
54
+ ENV[key] ||
55
+ ENV['MAX_JOBS'] ||
56
+ 100
57
+ ).to_i
33
58
  end
34
59
 
35
- def max_jobs
36
- @max_jobs ||= (ENV['MAX_JOBS'] || 100).to_i
60
+ def max_jobs_key(queue)
61
+ "MAX_JOBS_#{queue.upcase}"
37
62
  end
38
63
 
39
- def max_jobs_jitter
40
- @max_jobs_jitter ||= rand((ENV['MAX_JOBS_JITTER'] || 1).to_i)
64
+ def max_jobs_jitter(queue)
65
+ key = max_jobs_jitter_key(queue)
66
+ return cache[key] if cache.include?(key)
67
+
68
+ cache[key] = rand(
69
+ (
70
+ ENV[key] ||
71
+ ENV['MAX_JOBS_JITTER'] ||
72
+ 1
73
+ ).to_i
74
+ )
75
+ end
76
+
77
+ def max_jobs_jitter_key(queue)
78
+ "MAX_JOBS_JITTER_#{queue.upcase}"
41
79
  end
42
80
 
43
- def max_jobs_with_jitter
44
- @max_jobs_with_jitter ||= max_jobs + max_jobs_jitter
81
+ def max_jobs_with_jitter(queue)
82
+ key = max_jobs_with_jitter_key(queue)
83
+ return cache[key] if cache.include?(key)
84
+
85
+ cache[key] = max_jobs(queue) + max_jobs_jitter(queue)
86
+ end
87
+
88
+ def max_jobs_with_jitter_key(queue)
89
+ "MAX_JOBS_WITH_JITTER_#{queue.upcase}"
90
+ end
91
+
92
+ def mutex(queue)
93
+ key = mutex_key(queue)
94
+ return cache[key] if cache.include?(key)
95
+
96
+ cache[key] = ::Mutex.new
45
97
  end
46
98
 
47
- def mutex
48
- @mutex ||= ::Mutex.new
99
+ def mutex_key(queue)
100
+ "MUTEX_#{queue.upcase}"
49
101
  end
50
102
 
51
103
  def pid
@@ -54,9 +106,9 @@ module Sidekiq
54
106
  end
55
107
 
56
108
  def call(
57
- _, # worker-instance
58
- _, # item
59
- _ # queue
109
+ _, # worker-instance
110
+ _, # item
111
+ queue
60
112
  )
61
113
  exception_raised = false
62
114
  begin
@@ -69,10 +121,10 @@ module Sidekiq
69
121
  raise
70
122
  ensure
71
123
  if !exception_raised
72
- self.class.mutex.synchronize do
73
- self.class.increment_counter!
124
+ self.class.mutex(queue).synchronize do
125
+ self.class.increment_counter!(queue)
74
126
 
75
- if self.class.counter == self.class.max_jobs_with_jitter
127
+ if self.class.counter(queue) == self.class.max_jobs_with_jitter(queue)
76
128
  self.class.log_info("Max-Jobs quota met, shutting down pid: #{self.class.pid}")
77
129
  ::Process.kill('TERM', self.class.pid)
78
130
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq-max-jobs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan W. Zaleski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-09 00:00:00.000000000 Z
11
+ date: 2020-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sidekiq