job-iteration 1.18.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ee75f098d2b1a3eed6fb0690cb9f7f28d08c35d2dc214585c1ca915f74677df7
4
- data.tar.gz: 36ea172bc286dd7133c04094918dd16e2f8b0401ea4c16dbd85b14f0528224b1
3
+ metadata.gz: 88280bd40e0e7a284628f882df86e59dfb77bb90ac1e4d43324b61691f750a93
4
+ data.tar.gz: 3824453c2a0f1062448caf220b677754036b067c4c3639ffe4fedef708e5b123
5
5
  SHA512:
6
- metadata.gz: 4f570809a416c4c5f99dba36d8b59e99e904ae2c7dfad62216b0f43e0ae9189a4f8fbadba42c996a5d244258a2612bcf5fe5971080dd721ced61a702cce661c8
7
- data.tar.gz: 0b6699c8c436e527e371a6f416c44ea435c3e3120fa6cabd6f2dc586190578df451b34daf3786026549bc7aebf5ed0e4d70596b2d49f00a71533661779041aeb
6
+ metadata.gz: 87937768b5d9e7a37d590f5c5c8b1742f6b4d09f97390216981fa1764b900ca80f0a693fd47c924d51c15cc909528c0dc73629bfc9154c6951f4e62a4b6082e6
7
+ data.tar.gz: 70abdd8d211a9e5a9c319afa1e8afe9f9bdd6f57c3b02a8526cab0ce38750cb4e358acefd9fe793896ab8df1854c004f14e0d414d8e854932d6d65b2cc16e1b7
data/CHANGELOG.md CHANGED
@@ -16,6 +16,12 @@ 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
+
19
25
  ## v1.18.1 (Nov 20, 2026)
20
26
 
21
27
  ### Bug fixes
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 = relation.uncached do
48
- pluck_columns(relation)
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 = relation.uncached do
88
- relation.to_a
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(relation, columns: nil, batch_size: 100, timezone: nil, cursor: nil, instance: nil, instances: nil)
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(@relation, @columns, @cursor, @instance, @instances)
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,
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JobIteration
4
- VERSION = "1.18.1"
4
+ VERSION = "1.19.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.18.1
4
+ version: 1.19.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shopify