job-iteration 1.18.0 → 1.19.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 +12 -0
- data/README.md +26 -0
- data/lib/job-iteration/active_record_batch_enumerator.rb +13 -4
- data/lib/job-iteration/active_record_cursor.rb +14 -3
- data/lib/job-iteration/active_record_enumerator.rb +26 -3
- data/lib/job-iteration/enumerator_builder.rb +5 -0
- data/lib/job-iteration/log_subscriber.rb +2 -2
- data/lib/job-iteration/version.rb +1 -1
- metadata +61 -57
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 88280bd40e0e7a284628f882df86e59dfb77bb90ac1e4d43324b61691f750a93
|
|
4
|
+
data.tar.gz: 3824453c2a0f1062448caf220b677754036b067c4c3639ffe4fedef708e5b123
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 87937768b5d9e7a37d590f5c5c8b1742f6b4d09f97390216981fa1764b900ca80f0a693fd47c924d51c15cc909528c0dc73629bfc9154c6951f4e62a4b6082e6
|
|
7
|
+
data.tar.gz: 70abdd8d211a9e5a9c319afa1e8afe9f9bdd6f57c3b02a8526cab0ce38750cb4e358acefd9fe793896ab8df1854c004f14e0d414d8e854932d6d65b2cc16e1b7
|
data/CHANGELOG.md
CHANGED
|
@@ -16,6 +16,18 @@ nil
|
|
|
16
16
|
|
|
17
17
|
nil
|
|
18
18
|
|
|
19
|
+
## v1.19.0 (Nov 27, 2026)
|
|
20
|
+
|
|
21
|
+
### Features
|
|
22
|
+
|
|
23
|
+
- [740](https://github.com/Shopify/job-iteration/pull/740) - Add an `around_query:` option to Active Record enumerators for wrapping the queries they execute.
|
|
24
|
+
|
|
25
|
+
## v1.18.1 (Nov 20, 2026)
|
|
26
|
+
|
|
27
|
+
### Bug fixes
|
|
28
|
+
|
|
29
|
+
- [763](https://github.com/Shopify/job-iteration/pull/763) - Omit cursor values from the default no-record and interruption log messages to avoid exposing sensitive cursor data.
|
|
30
|
+
|
|
19
31
|
## v1.18.0 (Nov 6, 2026)
|
|
20
32
|
|
|
21
33
|
### Features
|
data/README.md
CHANGED
|
@@ -168,6 +168,32 @@ end
|
|
|
168
168
|
|
|
169
169
|
Iteration hooks into most popular queue adapters out of the box to support graceful interruption. No extra configuration is required.
|
|
170
170
|
|
|
171
|
+
## Wrapping Active Record queries
|
|
172
|
+
|
|
173
|
+
Pass `around_query:` to an Active Record enumerator when its queries need to execute in a specific database context. The proc receives each query as a block and must call it:
|
|
174
|
+
|
|
175
|
+
```ruby
|
|
176
|
+
class ReadFromReplicaJob < ApplicationJob
|
|
177
|
+
include JobIteration::Iteration
|
|
178
|
+
|
|
179
|
+
def build_enumerator(cursor:)
|
|
180
|
+
enumerator_builder.active_record_on_records(
|
|
181
|
+
User.all,
|
|
182
|
+
cursor: cursor,
|
|
183
|
+
around_query: ->(&query) { ActiveRecord::Base.connected_to(role: :reading, prevent_writes: true, &query) },
|
|
184
|
+
)
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def each_iteration(user)
|
|
188
|
+
user.notify_about_something
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
It wraps the queries the enumerator runs itself: the size query, and the query that fetches each record page or discovers the cursor for each batch relation. It does not wrap `each_iteration`, including queries caused by loading or updating a relation yielded from `active_record_on_batch_relations`.
|
|
194
|
+
|
|
195
|
+
`around_query:` is accepted by every Active Record builder, including the parallel ones. With nested enumerators, pass it to each level that needs it.
|
|
196
|
+
|
|
171
197
|
## Supported dependencies
|
|
172
198
|
|
|
173
199
|
Job-iteration currently supports the following queue adapters (in order of implementation):
|
|
@@ -10,9 +10,10 @@ module JobIteration
|
|
|
10
10
|
|
|
11
11
|
SQL_DATETIME_WITH_NSEC = "%Y-%m-%d %H:%M:%S.%N"
|
|
12
12
|
|
|
13
|
-
def initialize(relation, columns: nil, batch_size: 100, timezone: nil, cursor: nil)
|
|
13
|
+
def initialize(relation, columns: nil, batch_size: 100, timezone: nil, cursor: nil, around_query: nil)
|
|
14
14
|
@batch_size = batch_size
|
|
15
15
|
@timezone = timezone
|
|
16
|
+
@around_query = around_query
|
|
16
17
|
@column_mgr = ColumnManager.new(relation: relation, columns: columns)
|
|
17
18
|
@cursor = Array.wrap(cursor)
|
|
18
19
|
@initial_cursor = @cursor
|
|
@@ -33,7 +34,7 @@ module JobIteration
|
|
|
33
34
|
end
|
|
34
35
|
|
|
35
36
|
def size
|
|
36
|
-
(@base_relation.count(:all) + @batch_size - 1) / @batch_size # ceiling division
|
|
37
|
+
(execute_query { @base_relation.count(:all) } + @batch_size - 1) / @batch_size # ceiling division
|
|
37
38
|
end
|
|
38
39
|
|
|
39
40
|
private
|
|
@@ -44,8 +45,10 @@ module JobIteration
|
|
|
44
45
|
relation = relation.where(*conditions)
|
|
45
46
|
end
|
|
46
47
|
|
|
47
|
-
cursor_values, pkey_ids =
|
|
48
|
-
|
|
48
|
+
cursor_values, pkey_ids = execute_query do
|
|
49
|
+
relation.uncached do
|
|
50
|
+
pluck_columns(relation)
|
|
51
|
+
end
|
|
49
52
|
end
|
|
50
53
|
|
|
51
54
|
cursor = cursor_values.last
|
|
@@ -59,6 +62,12 @@ module JobIteration
|
|
|
59
62
|
filter_relation_with_primary_key(pkey_ids)
|
|
60
63
|
end
|
|
61
64
|
|
|
65
|
+
def execute_query(&query)
|
|
66
|
+
return query.call unless @around_query
|
|
67
|
+
|
|
68
|
+
@around_query.call(&query)
|
|
69
|
+
end
|
|
70
|
+
|
|
62
71
|
# Yields relations by selecting the primary keys of records in the batch.
|
|
63
72
|
# Post.where(published: nil) results in an enumerator of relations like:
|
|
64
73
|
# Post.where(published: nil, ids: batch_of_ids)
|
|
@@ -18,7 +18,8 @@ module JobIteration
|
|
|
18
18
|
end
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
def initialize(relation, columns, position, instance, instances)
|
|
21
|
+
def initialize(relation, columns, position, instance, instances, around_query: nil)
|
|
22
|
+
@around_query = around_query
|
|
22
23
|
@columns = if columns
|
|
23
24
|
Array(columns)
|
|
24
25
|
else
|
|
@@ -84,8 +85,10 @@ module JobIteration
|
|
|
84
85
|
relation = relation.where(*conditions)
|
|
85
86
|
end
|
|
86
87
|
|
|
87
|
-
records =
|
|
88
|
-
relation.
|
|
88
|
+
records = execute_query do
|
|
89
|
+
relation.uncached do
|
|
90
|
+
relation.to_a
|
|
91
|
+
end
|
|
89
92
|
end
|
|
90
93
|
|
|
91
94
|
update_from_record(records.last) unless records.empty?
|
|
@@ -94,6 +97,14 @@ module JobIteration
|
|
|
94
97
|
records.empty? ? nil : records
|
|
95
98
|
end
|
|
96
99
|
|
|
100
|
+
private
|
|
101
|
+
|
|
102
|
+
def execute_query(&query)
|
|
103
|
+
return query.call unless @around_query
|
|
104
|
+
|
|
105
|
+
@around_query.call(&query)
|
|
106
|
+
end
|
|
107
|
+
|
|
97
108
|
protected
|
|
98
109
|
|
|
99
110
|
def conditions
|
|
@@ -7,10 +7,20 @@ module JobIteration
|
|
|
7
7
|
class ActiveRecordEnumerator
|
|
8
8
|
SQL_DATETIME_WITH_NSEC = "%Y-%m-%d %H:%M:%S.%N"
|
|
9
9
|
|
|
10
|
-
def initialize(
|
|
10
|
+
def initialize(
|
|
11
|
+
relation,
|
|
12
|
+
columns: nil,
|
|
13
|
+
batch_size: 100,
|
|
14
|
+
timezone: nil,
|
|
15
|
+
cursor: nil,
|
|
16
|
+
instance: nil,
|
|
17
|
+
instances: nil,
|
|
18
|
+
around_query: nil
|
|
19
|
+
)
|
|
11
20
|
@relation = relation
|
|
12
21
|
@batch_size = batch_size
|
|
13
22
|
@timezone = timezone
|
|
23
|
+
@around_query = around_query
|
|
14
24
|
@columns = if columns
|
|
15
25
|
Array(columns)
|
|
16
26
|
else
|
|
@@ -41,12 +51,18 @@ module JobIteration
|
|
|
41
51
|
end
|
|
42
52
|
|
|
43
53
|
def size
|
|
44
|
-
full_size = @relation.count(:all)
|
|
54
|
+
full_size = execute_query { @relation.count(:all) }
|
|
45
55
|
@instances.present? ? full_size / @instances : full_size
|
|
46
56
|
end
|
|
47
57
|
|
|
48
58
|
private
|
|
49
59
|
|
|
60
|
+
def execute_query(&query)
|
|
61
|
+
return query.call unless @around_query
|
|
62
|
+
|
|
63
|
+
@around_query.call(&query)
|
|
64
|
+
end
|
|
65
|
+
|
|
50
66
|
def instrument_next_batch(cursor)
|
|
51
67
|
ActiveSupport::Notifications.instrument("active_record_cursor.iteration") do
|
|
52
68
|
cursor.next_batch(@batch_size)
|
|
@@ -64,7 +80,14 @@ module JobIteration
|
|
|
64
80
|
end
|
|
65
81
|
|
|
66
82
|
def finder_cursor
|
|
67
|
-
JobIteration::ActiveRecordCursor.new(
|
|
83
|
+
JobIteration::ActiveRecordCursor.new(
|
|
84
|
+
@relation,
|
|
85
|
+
@columns,
|
|
86
|
+
@cursor,
|
|
87
|
+
@instance,
|
|
88
|
+
@instances,
|
|
89
|
+
around_query: @around_query,
|
|
90
|
+
)
|
|
68
91
|
end
|
|
69
92
|
|
|
70
93
|
def column_value(record, attribute)
|
|
@@ -112,6 +112,11 @@ module JobIteration
|
|
|
112
112
|
# iteration, they may be skipped or yielded multiple times depending on the nature of the update and the
|
|
113
113
|
# cursor's value. If the value gets updated to a greater value than the cursor's value, it will get yielded
|
|
114
114
|
# again. Similarly, if the value gets updated to a lesser value than the curor's value, it will get skipped.
|
|
115
|
+
#
|
|
116
|
+
# +around_query:+ takes a proc that receives a block for each query the enumerator executes (the size
|
|
117
|
+
# query and each page query) and must call it. Use it to run those queries in a specific database context:
|
|
118
|
+
#
|
|
119
|
+
# around_query: ->(&query) { ActiveRecord::Base.connected_to(role: :reading, &query) }
|
|
115
120
|
def build_active_record_enumerator_on_records(scope, cursor:, **args)
|
|
116
121
|
enum = build_active_record_enumerator(
|
|
117
122
|
scope,
|
|
@@ -15,7 +15,7 @@ module JobIteration
|
|
|
15
15
|
def not_found(event)
|
|
16
16
|
info do
|
|
17
17
|
"[JobIteration::Iteration] Enumerator found nothing to iterate! " \
|
|
18
|
-
"times_interrupted=#{event.payload[:times_interrupted]}
|
|
18
|
+
"times_interrupted=#{event.payload[:times_interrupted]}"
|
|
19
19
|
end
|
|
20
20
|
end
|
|
21
21
|
|
|
@@ -36,7 +36,7 @@ module JobIteration
|
|
|
36
36
|
def interrupted(event)
|
|
37
37
|
info do
|
|
38
38
|
"[JobIteration::Iteration] Interrupting and re-enqueueing the job " \
|
|
39
|
-
"
|
|
39
|
+
"reason=#{event.payload[:reason] || "nil"}"
|
|
40
40
|
end
|
|
41
41
|
end
|
|
42
42
|
|
metadata
CHANGED
|
@@ -1,84 +1,88 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: job-iteration
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.19.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
- Shopify
|
|
7
|
+
- Shopify
|
|
8
8
|
bindir: exe
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
|
-
- !ruby/object:Gem::Dependency
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: activejob
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
-
|
|
17
|
+
- ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: "7.1"
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
-
|
|
25
|
+
- ">="
|
|
26
|
+
- !ruby/object:Gem::Version
|
|
27
|
+
version: "7.1"
|
|
26
28
|
description: Makes your background jobs interruptible and resumable.
|
|
27
29
|
email:
|
|
28
|
-
- ops-accounts+shipit@shopify.com
|
|
30
|
+
- ops-accounts+shipit@shopify.com
|
|
29
31
|
executables: []
|
|
30
32
|
extensions: []
|
|
31
33
|
extra_rdoc_files: []
|
|
32
34
|
files:
|
|
33
|
-
- CHANGELOG.md
|
|
34
|
-
- LICENSE.txt
|
|
35
|
-
- README.md
|
|
36
|
-
- job-iteration.gemspec
|
|
37
|
-
- lib/job-iteration.rb
|
|
38
|
-
- lib/job-iteration/active_record_batch_enumerator.rb
|
|
39
|
-
- lib/job-iteration/active_record_batch_enumerator/column_manager.rb
|
|
40
|
-
- lib/job-iteration/active_record_cursor.rb
|
|
41
|
-
- lib/job-iteration/active_record_enumerator.rb
|
|
42
|
-
- lib/job-iteration/csv_enumerator.rb
|
|
43
|
-
- lib/job-iteration/enumerator_builder.rb
|
|
44
|
-
- lib/job-iteration/interruption_adapters.rb
|
|
45
|
-
- lib/job-iteration/interruption_adapters/delayed_job_adapter.rb
|
|
46
|
-
- lib/job-iteration/interruption_adapters/good_job_adapter.rb
|
|
47
|
-
- lib/job-iteration/interruption_adapters/null_adapter.rb
|
|
48
|
-
- lib/job-iteration/interruption_adapters/resque_adapter.rb
|
|
49
|
-
- lib/job-iteration/interruption_adapters/sidekiq_adapter.rb
|
|
50
|
-
- lib/job-iteration/interruption_adapters/solid_queue_adapter.rb
|
|
51
|
-
- lib/job-iteration/interruption_adapters/sqs_adapter.rb
|
|
52
|
-
- lib/job-iteration/iteration.rb
|
|
53
|
-
- lib/job-iteration/log_subscriber.rb
|
|
54
|
-
- lib/job-iteration/nested_enumerator.rb
|
|
55
|
-
- lib/job-iteration/parallel_enumerator.rb
|
|
56
|
-
- lib/job-iteration/railtie.rb
|
|
57
|
-
- lib/job-iteration/test_helper.rb
|
|
58
|
-
- lib/job-iteration/throttle_enumerator.rb
|
|
59
|
-
- lib/job-iteration/version.rb
|
|
60
|
-
- lib/tapioca/dsl/compilers/job_iteration.rb
|
|
61
|
-
homepage: https://github.com/shopify/job-iteration
|
|
35
|
+
- CHANGELOG.md
|
|
36
|
+
- LICENSE.txt
|
|
37
|
+
- README.md
|
|
38
|
+
- job-iteration.gemspec
|
|
39
|
+
- lib/job-iteration.rb
|
|
40
|
+
- lib/job-iteration/active_record_batch_enumerator.rb
|
|
41
|
+
- lib/job-iteration/active_record_batch_enumerator/column_manager.rb
|
|
42
|
+
- lib/job-iteration/active_record_cursor.rb
|
|
43
|
+
- lib/job-iteration/active_record_enumerator.rb
|
|
44
|
+
- lib/job-iteration/csv_enumerator.rb
|
|
45
|
+
- lib/job-iteration/enumerator_builder.rb
|
|
46
|
+
- lib/job-iteration/interruption_adapters.rb
|
|
47
|
+
- lib/job-iteration/interruption_adapters/delayed_job_adapter.rb
|
|
48
|
+
- lib/job-iteration/interruption_adapters/good_job_adapter.rb
|
|
49
|
+
- lib/job-iteration/interruption_adapters/null_adapter.rb
|
|
50
|
+
- lib/job-iteration/interruption_adapters/resque_adapter.rb
|
|
51
|
+
- lib/job-iteration/interruption_adapters/sidekiq_adapter.rb
|
|
52
|
+
- lib/job-iteration/interruption_adapters/solid_queue_adapter.rb
|
|
53
|
+
- lib/job-iteration/interruption_adapters/sqs_adapter.rb
|
|
54
|
+
- lib/job-iteration/iteration.rb
|
|
55
|
+
- lib/job-iteration/log_subscriber.rb
|
|
56
|
+
- lib/job-iteration/nested_enumerator.rb
|
|
57
|
+
- lib/job-iteration/parallel_enumerator.rb
|
|
58
|
+
- lib/job-iteration/railtie.rb
|
|
59
|
+
- lib/job-iteration/test_helper.rb
|
|
60
|
+
- lib/job-iteration/throttle_enumerator.rb
|
|
61
|
+
- lib/job-iteration/version.rb
|
|
62
|
+
- lib/tapioca/dsl/compilers/job_iteration.rb
|
|
63
|
+
homepage: "https://github.com/shopify/job-iteration"
|
|
62
64
|
licenses:
|
|
63
|
-
- MIT
|
|
65
|
+
- MIT
|
|
64
66
|
metadata:
|
|
65
|
-
changelog_uri: https://github.com/Shopify/job-iteration/blob/main/CHANGELOG.md
|
|
66
|
-
allowed_push_host: https://rubygems.org
|
|
67
|
+
changelog_uri: "https://github.com/Shopify/job-iteration/blob/main/CHANGELOG.md"
|
|
68
|
+
allowed_push_host: "https://rubygems.org"
|
|
67
69
|
rdoc_options: []
|
|
68
70
|
require_paths:
|
|
69
|
-
- lib
|
|
71
|
+
- lib
|
|
70
72
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
71
73
|
requirements:
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
74
|
+
-
|
|
75
|
+
- ">="
|
|
76
|
+
- !ruby/object:Gem::Version
|
|
77
|
+
version: "3.1"
|
|
75
78
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
79
|
requirements:
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
+
-
|
|
81
|
+
- ">="
|
|
82
|
+
- !ruby/object:Gem::Version
|
|
83
|
+
version: "0"
|
|
80
84
|
requirements: []
|
|
81
|
-
rubygems_version: 4.0
|
|
85
|
+
rubygems_version: 4.1.0
|
|
82
86
|
specification_version: 4
|
|
83
87
|
summary: Makes your background jobs interruptible and resumable.
|
|
84
88
|
test_files: []
|