maintenance_tasks 2.13.0 → 2.15.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: 8402f1f4b8919e892a6334373ad397d49b5c6c78893f2f107b990b90653435d1
4
- data.tar.gz: 1459d5b92233a1196aeb83c80bec9da525f04e975a50f6a415cdcd8dc647bd59
3
+ metadata.gz: f74ab248a9b57875d5e7b47234598a46fbc37033fb7d96c95e26b03c55d83793
4
+ data.tar.gz: cf2322966430eee4215131e9885aba07591d2e6f5081f6ab1683188ae5be8e80
5
5
  SHA512:
6
- metadata.gz: 3dff16b85a650f4d73cf79df20267d1278b1181f2ba6717742581909ebfd39145dfdd9b9cf1453edaa785474fb6f7dda87af2bad534e6398bf010945b02f2da3
7
- data.tar.gz: c7b75c1edc684fad66c5080ee3642fe469038e1f4ab2329ead0fd3c65e5f0ca2a940250dc988658dce672e2de0d056741b9a317310afa23165ba27794722ce1d
6
+ metadata.gz: 22bde697fa7b38899ff5dc4c7fc353776a29766f479a3fac4f988b921aaf62bd58534f55a2c9caa1b9bf227ddbbef249c1d521b863afc5ab59527ad32528a4e9
7
+ data.tar.gz: 1f51f3e1ead4af15d02886b950562e0a103baf055839e1e5e6f661b2dfd29d47626859fbd350e8d311293c88d4b4f59189ae70e32a8dbca54559d75ce5fafaa5
data/README.md CHANGED
@@ -242,8 +242,8 @@ seconds to process. Consider skipping the count (defining a `count` that returns
242
242
  `nil`) or use an approximation, eg: count the number of new lines:
243
243
 
244
244
  ```ruby
245
- def count(task)
246
- task.csv_content.count("\n") - 1
245
+ def count
246
+ csv_content.count("\n") - 1
247
247
  end
248
248
  ```
249
249
 
@@ -391,7 +391,10 @@ This method should return an `Enumerator`, yielding pairs of `[item, cursor]`.
391
391
  Maintenance Tasks takes care of persisting the current cursor position and will
392
392
  provide it as the `cursor` argument if your task is interrupted or resumed. The
393
393
  `cursor` is stored as a `String`, so your custom enumerator should handle
394
- serializing/deserializing the value if required.
394
+ serializing/deserializing the value if required. Note that if you've enabled
395
+ [JSON cursors](#json-cursors), the cursor will be automatically serialized and
396
+ deserialized. However for this to work, the cursor values must be
397
+ JSON-serializable.
395
398
 
396
399
  ```ruby
397
400
  # app/tasks/maintenance/custom_enumerator_task.rb
@@ -561,6 +564,9 @@ control the cursor columns, through the `cursor_columns` method in the
561
564
  the query is ordered by the primary key. If cursor columns values change during
562
565
  an iteration, records may be skipped or yielded multiple times.
563
566
 
567
+ Note that in order to use this feature, you must first enable
568
+ [JSON cursors](#json-cursors).
569
+
564
570
  ```ruby
565
571
  module Maintenance
566
572
  class UpdatePostsTask < MaintenanceTasks::Task
@@ -1317,6 +1323,46 @@ MaintenanceTasks.metadata = ->() do
1317
1323
  end
1318
1324
  ```
1319
1325
 
1326
+ #### JSON Cursors
1327
+
1328
+ By default, cursor values are persisted in the database as a string. If you
1329
+ want to iterate over collections that require multiple values to keep track of
1330
+ the position, you can enable JSON cursors. This will cause the cursor to be
1331
+ serialized as JSON when persisted, and deserialized when read back.
1332
+
1333
+ This is especially useful when you need to iterate over a model that uses a
1334
+ composite primary key.
1335
+
1336
+ Be advised that this feature comes with a few caveats:
1337
+
1338
+ 1. Cursor values must be capable of being serialized to JSON and parsed from
1339
+ JSON. If they are not, errors will occur during task execution.
1340
+ 2. If a cursor contains a value that loses precision when serialized, it may
1341
+ lead to unexpected behaviour.
1342
+ 3. This feature utilizes a string column to store the JSON data. If your
1343
+ database has a hard limit on how big a string value can be, be mindful that
1344
+ certain cursor structures could result in a value that could exceed that
1345
+ limit and cause issues.
1346
+
1347
+ A new column was added to discern JSON cursors from plain string cursors. Make
1348
+ sure you have run the latest database migrations provided by the gem before
1349
+ enabling this feature. See [upgrading](#upgrading) for more information.
1350
+
1351
+ ```ruby
1352
+ # config/initializers/maintenance_tasks.rb
1353
+ MaintenanceTasks.serialize_cursors_as_json = true
1354
+ ```
1355
+
1356
+ #### Configure staleness threshold
1357
+
1358
+ To specify a staleness threshold date interval which will mark task runs as stale, you can
1359
+ configure `MaintenanceTasks.task_staleness_threshold`. Tasks that have last run
1360
+ successfully after this threshold will be marked stale.
1361
+
1362
+ The value for `MaintenanceTasks.task_staleness_threshold` must be an
1363
+ `ActiveSupport::Duration`. If no value is specified, it will default to 30 days. This can be disabled
1364
+ by setting `MaintenanceTasks.task_staleness_threshold` to `false`.
1365
+
1320
1366
  ## Upgrading
1321
1367
 
1322
1368
  Use bundler to check for and upgrade to newer versions. After installing a new
@@ -11,7 +11,7 @@ module MaintenanceTasks
11
11
  policy.style_src_elem(
12
12
  BULMA_CDN,
13
13
  # <style> tag in app/views/layouts/maintenance_tasks/application.html.erb
14
- "'sha256-WHHDQLdkleXnAN5zs0GDXC5ls41CHUaVsJtVpaNx+EM='",
14
+ "'sha256-b9tTK1UaF0U8792/A1vIUkeZwjPgECIOeKJdhYED06A='",
15
15
  )
16
16
  capybara_lockstep_scripts = [
17
17
  "'sha256-1AoN3ZtJC5OvqkMgrYvhZjp4kI8QjJjO7TAyKYiDw+U='",
@@ -62,7 +62,7 @@ module MaintenanceTasks
62
62
  def status_tag(status)
63
63
  tag.span(
64
64
  status.capitalize,
65
- class: ["tag", "has-text-weight-medium", "pr-2", "mr-4"] + STATUS_COLOURS.fetch(status),
65
+ class: ["tag", "has-text-weight-medium", "px-2", "mx-4"] + STATUS_COLOURS.fetch(status),
66
66
  )
67
67
  end
68
68
 
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "json"
4
+
3
5
  module MaintenanceTasks
4
6
  # Concern that holds the behaviour of the job that runs the tasks. It is
5
7
  # included in {TaskJob} and if MaintenanceTasks.job is overridden, it must be
@@ -30,8 +32,18 @@ module MaintenanceTasks
30
32
 
31
33
  private
32
34
 
35
+ def serialized_cursor_position
36
+ cursor_position && @run.cursor_is_json? ? cursor_position.to_json : cursor_position
37
+ end
38
+
39
+ def deserialized_run_cursor
40
+ return JSON.parse(@run.cursor) if @run.cursor && @run.cursor_is_json?
41
+
42
+ @run.cursor
43
+ end
44
+
33
45
  def build_enumerator(_run, cursor:)
34
- cursor ||= @run.cursor
46
+ cursor ||= deserialized_run_cursor
35
47
  self.cursor_position = cursor
36
48
  enumerator_builder = self.enumerator_builder
37
49
  @collection_enum = @task.enumerator_builder(cursor: cursor)
@@ -140,7 +152,7 @@ module MaintenanceTasks
140
152
 
141
153
  def on_shutdown
142
154
  @run.job_shutdown
143
- @run.cursor = cursor_position
155
+ @run.cursor = serialized_cursor_position
144
156
  @ticker.persist
145
157
  end
146
158
 
@@ -177,7 +189,7 @@ module MaintenanceTasks
177
189
  @ticker.persist if defined?(@ticker)
178
190
 
179
191
  if defined?(@run)
180
- @run.cursor = cursor_position
192
+ @run.cursor = serialized_cursor_position
181
193
  @run.persist_error(error)
182
194
 
183
195
  task_context = {