sidekiq-throttled 0.10.0 → 0.11.0

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: a204bc340965b64fe16f9b896815c9b3fbf383418b0774871d90c35d4c8fb061
4
- data.tar.gz: bb2b9681a7caf33d3418ab2f475bc0ab2f6141ed9e6609d55d680815da71c4e6
3
+ metadata.gz: '05945dcd1de433dd74010e8fa402c6fb2e6100fb6306aef386536da24cd2e08b'
4
+ data.tar.gz: fe07b3c6409e7749f2af3b85d36e27dcda5d53b92c771e050a951f6175cec407
5
5
  SHA512:
6
- metadata.gz: '0801102f967a02d3a023b9d8bfe04b8d44280c5f2986182ef4a5cc6e971091afcb3fb1b6c845f382829de071e11d6a3111cb04a21b040b022b61ecac76d5a422'
7
- data.tar.gz: 243ceeb9a791417edc07a7f697660035e52a61f3db4aefbe93508be5037d1af1932d2b3ad81886ee1b9ef8408e97f1bef8cac03b9a78d8e4c29a2197f9f927f6
6
+ metadata.gz: 2965642d0564eaf45c033b6c6d6ddfe5b80804eebfbfef7908c4f81bbc450baec4e71d1b70fe21bf6515336d173b3a95e74c4b97aa26d35a3947b7b24ba02c1e
7
+ data.tar.gz: 149ba3f6d29c9963c52f96d7ea1b6be23424726a7791afe7c3fad4ab95bf82b2db0df274ee3a26842e7726e1f403afff2d99c997f1033f2ddb63a95f9790d698
data/CHANGES.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## 0.11.0 (2019-08-24)
2
+
3
+ * [#59](https://github.com/sensortower/sidekiq-throttled/pull/59)
4
+ Add throttling observer.
5
+ ([@ogins57])
6
+
7
+
1
8
  ## 0.10.0 (2019-06-22)
2
9
 
3
10
  * [#60](https://github.com/sensortower/sidekiq-throttled/pull/60)
@@ -193,3 +200,4 @@
193
200
  [@iporsut]: https://github.com/iporsut
194
201
  [@mstruve]: https://github.com/mstruve
195
202
  [@ziaulrehman40]: https://github.com/ziaulrehman40
203
+ [@ogins57]: https://github.com/ogins57
data/README.md CHANGED
@@ -59,6 +59,38 @@ class MyWorker
59
59
  end
60
60
  ```
61
61
 
62
+ ### Observer
63
+
64
+ You can specify an observer that will be called on throttling. To do so pass an
65
+ `:observer` option with callable object:
66
+
67
+ ``` ruby
68
+ class MyWorker
69
+ include Sidekiq::Worker
70
+ include Sidekiq::Throttled::Worker
71
+
72
+ MY_OBSERVER = lambda do |strategy, *args|
73
+ # do something
74
+ end
75
+
76
+ sidekiq_options :queue => :my_queue
77
+
78
+ sidekiq_throttle({
79
+ :concurrency => { :limit => 10 },
80
+ :threshold => { :limit => 100, :period => 1.hour }
81
+ :observer => MY_OBSERVER
82
+ })
83
+
84
+ def perform(*args)
85
+ # ...
86
+ end
87
+ end
88
+ ```
89
+
90
+ Observer will receive `strategy, *args` arguments, where `strategy` is a Symbol
91
+ `:concurrency` or `:threshold`, and `*args` are the arguements that were passed
92
+ to the job.
93
+
62
94
 
63
95
  ### Dynamic throttling
64
96
 
@@ -19,26 +19,24 @@ module Sidekiq
19
19
  # @return [Strategy::Threshold, nil]
20
20
  attr_reader :threshold
21
21
 
22
+ # @!attribute [r] observer
23
+ # @return [Proc, nil]
24
+ attr_reader :observer
25
+
22
26
  # @param [#to_s] name
23
27
  # @param [Hash] concurrency Concurrency options.
24
28
  # See keyword args of {Strategy::Concurrency#initialize} for details.
25
29
  # @param [Hash] threshold Threshold options.
26
30
  # See keyword args of {Strategy::Threshold#initialize} for details.
27
31
  # @param [#call] key_suffix Dynamic key suffix generator.
28
- def initialize(name, concurrency: nil, threshold: nil, key_suffix: nil)
29
- key = "throttled:#{name}"
30
-
31
- @concurrency =
32
- if concurrency
33
- concurrency[:key_suffix] ||= key_suffix
34
- Concurrency.new(key, **concurrency)
35
- end
36
-
37
- @threshold =
38
- if threshold
39
- threshold[:key_suffix] ||= key_suffix
40
- Threshold.new(key, **threshold)
41
- end
32
+ # @param [#call] observer Process called after throttled.
33
+ def initialize(
34
+ name,
35
+ concurrency: nil, threshold: nil, key_suffix: nil, observer: nil
36
+ )
37
+ @observer = observer
38
+ @concurrency = make_strategy(Concurrency, name, key_suffix, concurrency)
39
+ @threshold = make_strategy(Threshold, name, key_suffix, threshold)
42
40
 
43
41
  return if @concurrency || @threshold
44
42
 
@@ -55,9 +53,13 @@ module Sidekiq
55
53
 
56
54
  # @return [Boolean] whenever job is throttled or not.
57
55
  def throttled?(jid, *job_args)
58
- return true if @concurrency&.throttled?(jid, *job_args)
56
+ if @concurrency&.throttled?(jid, *job_args)
57
+ @observer&.call(:concurrency, *job_args)
58
+ return true
59
+ end
59
60
 
60
61
  if @threshold&.throttled?(*job_args)
62
+ @observer&.call(:threshold, *job_args)
61
63
  finalize!(jid, *job_args)
62
64
  return true
63
65
  end
@@ -77,6 +79,18 @@ module Sidekiq
77
79
  @concurrency&.reset!
78
80
  @threshold&.reset!
79
81
  end
82
+
83
+ private
84
+
85
+ # @return [Base, nil]
86
+ def make_strategy(strategy, name, key_suffix, options)
87
+ return unless options
88
+
89
+ strategy.new("throttled:#{name}", {
90
+ :key_suffix => key_suffix,
91
+ **options
92
+ })
93
+ end
80
94
  end
81
95
  end
82
96
  end
@@ -3,6 +3,6 @@
3
3
  module Sidekiq
4
4
  module Throttled
5
5
  # Gem version
6
- VERSION = "0.10.0"
6
+ VERSION = "0.11.0"
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq-throttled
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexey V Zapparov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-24 00:00:00.000000000 Z
11
+ date: 2019-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby