job-iteration 1.19.0 → 1.20.0

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: 88280bd40e0e7a284628f882df86e59dfb77bb90ac1e4d43324b61691f750a93
4
- data.tar.gz: 3824453c2a0f1062448caf220b677754036b067c4c3639ffe4fedef708e5b123
3
+ metadata.gz: 0cafd687e18593d0a43b3413b22c5c5cca285f88526dc0b7723a713ff0249f6b
4
+ data.tar.gz: 3a88bfef4d14b0d5e122fa31e9b093af62469950ce6d9458f37f6f6b474d9e3c
5
5
  SHA512:
6
- metadata.gz: 87937768b5d9e7a37d590f5c5c8b1742f6b4d09f97390216981fa1764b900ca80f0a693fd47c924d51c15cc909528c0dc73629bfc9154c6951f4e62a4b6082e6
7
- data.tar.gz: 70abdd8d211a9e5a9c319afa1e8afe9f9bdd6f57c3b02a8526cab0ce38750cb4e358acefd9fe793896ab8df1854c004f14e0d414d8e854932d6d65b2cc16e1b7
6
+ metadata.gz: 62c5598a83db2d7c8fbf51e363592a3bab4b52e9a7cb3c0dbfc28812e5d3d17ba6e3514c7972b361767aa867646d53f385499092492ce02349898271ac98406b
7
+ data.tar.gz: c08f5c8bba7c3e1c3d4f55eee773c2bed4f77d11c556288dca1b24496dc48704017d2654e11ee6772dc42a8ea568368f6bfeac0980c414c8ef7c64fa6b9b5a79
data/CHANGELOG.md CHANGED
@@ -16,6 +16,12 @@ nil
16
16
 
17
17
  nil
18
18
 
19
+ ## v1.20.0 (Dec 2, 2026)
20
+
21
+ ### Features
22
+
23
+ - [772](https://github.com/Shopify/job-iteration/pull/772) - Add a `check_before_fetch:` option to `build_throttle_enumerator` that checks the throttle before the wrapped enumerator fetches its next value, so a throttled job no longer runs a query and discards the result. The default behaviour is unchanged.
24
+
19
25
  ## v1.19.0 (Nov 27, 2026)
20
26
 
21
27
  ### Features
@@ -183,12 +183,23 @@ module JobIteration
183
183
  enum
184
184
  end
185
185
 
186
- def build_throttle_enumerator(enumerable, throttle_on:, backoff:)
186
+ # Builds Enumerator that wraps another enumerator and re-enqueues the job with +backoff+ when +throttle_on+
187
+ # returns true. See {JobIteration::ThrottleEnumerator} and the throttling guide.
188
+ #
189
+ # @param enumerable [#each] the enumerator to throttle.
190
+ # @param throttle_on [#call] returns true when the job should stop and be re-enqueued.
191
+ # @param backoff [ActiveSupport::Duration, Numeric] how long to wait before the job runs again.
192
+ # @param check_before_fetch [Boolean] when true, +throttle_on+ is called once before iteration starts and then
193
+ # after each value has been processed, before +enumerable+ is asked for the next value, so a throttled job does
194
+ # not fetch a value only to discard it. When false (the default), +throttle_on+ is called after +enumerable+
195
+ # has produced each value and before it is processed.
196
+ def build_throttle_enumerator(enumerable, throttle_on:, backoff:, check_before_fetch: false)
187
197
  enum = JobIteration::ThrottleEnumerator.new(
188
198
  enumerable,
189
199
  @job,
190
200
  throttle_on: throttle_on,
191
201
  backoff: backoff,
202
+ check_before_fetch: check_before_fetch,
192
203
  ).to_enum
193
204
  wrap(self, enum)
194
205
  end
@@ -18,23 +18,38 @@ module JobIteration
18
18
  # The enumerator from above will mimic +active_record_on_batches+,
19
19
  # except when +DatabaseStatus.unhealthy?+ starts to return true.
20
20
  # In that case, it will re-enqueue the job with a specified backoff.
21
+ #
22
+ # By default, +throttle_on+ is called after the wrapped enumerator has produced the next value and before that value
23
+ # is processed, so a throttled job has already fetched (and then discards) the next value. Pass
24
+ # +check_before_fetch: true+ to call +throttle_on+ once before iteration starts and then after each value has been
25
+ # processed, before the wrapped enumerator is asked for the next one.
21
26
  class ThrottleEnumerator
22
- def initialize(enum, job, throttle_on:, backoff:)
27
+ def initialize(enum, job, throttle_on:, backoff:, check_before_fetch: false)
23
28
  @enum = enum
24
29
  @job = job
25
30
  @throttle_on = throttle_on
26
31
  @backoff = backoff
32
+ @check_before_fetch = check_before_fetch
27
33
  end
28
34
 
29
35
  def to_enum
30
- Enumerator.new(-> { @enum.size }) do |yielder|
31
- @enum.each do |*val|
32
- if should_throttle?
33
- ActiveSupport::Notifications.instrument("throttled.iteration", job_class: @job.class.name)
34
- throw(:abort, [:retry, @backoff])
36
+ if @check_before_fetch
37
+ Enumerator.new(-> { @enum.size }) do |yielder|
38
+ throttle_if_needed
39
+
40
+ @enum.each do |*val|
41
+ yielder.yield(*val)
42
+
43
+ throttle_if_needed
35
44
  end
45
+ end
46
+ else
47
+ Enumerator.new(-> { @enum.size }) do |yielder|
48
+ @enum.each do |*val|
49
+ throttle_if_needed
36
50
 
37
- yielder.yield(*val)
51
+ yielder.yield(*val)
52
+ end
38
53
  end
39
54
  end
40
55
  end
@@ -42,5 +57,14 @@ module JobIteration
42
57
  def should_throttle?
43
58
  @throttle_on.call
44
59
  end
60
+
61
+ private
62
+
63
+ def throttle_if_needed
64
+ return unless should_throttle?
65
+
66
+ ActiveSupport::Notifications.instrument("throttled.iteration", job_class: @job.class.name)
67
+ throw(:abort, [:retry, @backoff])
68
+ end
45
69
  end
46
70
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JobIteration
4
- VERSION = "1.19.0"
4
+ VERSION = "1.20.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: job-iteration
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.19.0
4
+ version: 1.20.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shopify