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 +4 -4
- data/CHANGELOG.md +6 -0
- data/lib/job-iteration/enumerator_builder.rb +12 -1
- data/lib/job-iteration/throttle_enumerator.rb +31 -7
- data/lib/job-iteration/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0cafd687e18593d0a43b3413b22c5c5cca285f88526dc0b7723a713ff0249f6b
|
|
4
|
+
data.tar.gz: 3a88bfef4d14b0d5e122fa31e9b093af62469950ce6d9458f37f6f6b474d9e3c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
-
|
|
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
|
-
|
|
31
|
-
@enum.
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
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
|