sidekiq-batch-jobs 0.1.0 → 0.2.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.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +133 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +447 -71
  5. data/Rakefile +13 -0
  6. data/app/models/sidekiq_batch/abandoned_enrollment_error.rb +9 -0
  7. data/app/models/sidekiq_batch/abandoned_enrollment_reaper.rb +76 -0
  8. data/app/models/sidekiq_batch/announcement.rb +106 -0
  9. data/app/models/sidekiq_batch/batch_enrollment_context.rb +165 -40
  10. data/app/models/sidekiq_batch/client_middleware.rb +5 -7
  11. data/app/models/sidekiq_batch/completion_query.rb +84 -0
  12. data/app/models/sidekiq_batch/groomer_worker.rb +15 -0
  13. data/app/models/sidekiq_batch/jid_index.rb +49 -0
  14. data/app/models/sidekiq_batch/middleware.rb +58 -38
  15. data/app/models/sidekiq_batch/orphaned_callback_reaper.rb +37 -0
  16. data/app/models/sidekiq_batch/orphaned_job_error.rb +9 -0
  17. data/app/models/sidekiq_batch/reaper_worker.rb +58 -0
  18. data/app/models/sidekiq_batch/record_groomer.rb +31 -0
  19. data/app/models/sidekiq_batch/stuck_job_reaper.rb +67 -0
  20. data/app/models/sidekiq_batch.rb +124 -52
  21. data/app/models/sidekiq_batch_job.rb +38 -18
  22. data/lib/generators/sidekiq/batch/jobs/install/install_generator.rb +11 -1
  23. data/lib/generators/sidekiq/batch/jobs/install/templates/create_sidekiq_batch_tables.rb.tt +12 -2
  24. data/lib/sidekiq/batch/jobs/configuration.rb +100 -0
  25. data/lib/sidekiq/batch/jobs/engine.rb +6 -2
  26. data/lib/sidekiq/batch/jobs/enum_compat.rb +29 -0
  27. data/lib/sidekiq/batch/jobs/failure_policy.rb +129 -0
  28. data/lib/sidekiq/batch/jobs/version.rb +1 -1
  29. data/lib/sidekiq/batch/jobs.rb +64 -21
  30. metadata +38 -124
  31. data/docker-compose.yml +0 -26
  32. data/sig/sidekiq/batch/jobs.rbs +0 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e99dbc78b86da74277380f2ba6fe62c5433ab3815d23b0f756ff6173267fb168
4
- data.tar.gz: 96f45bcea22e9cd2359190946e62b54d92c8c192ddfbc87dd4148ccb9abe53e1
3
+ metadata.gz: 9e70220429bbc1911259964dccf5cc7a67ac5ecdbb0d2ecafe98fb783d70c043
4
+ data.tar.gz: fb4d826947c4acf259e3641022031b656a6b4bd879ba748b1e0cc1f9342e691b
5
5
  SHA512:
6
- metadata.gz: 7d665aba6579df375e91325f433cecf574abb0310ad9efd142358232890b5ddabccdac3f6d3a3a9773080f7facd7617fc4d4c0ac4116285e938e6ec805a4c15e
7
- data.tar.gz: dd13456bd144b5d0a104a78b7ddf8eee1469c06a28ac497add4b78a6f4c666fa2c2151fcb2824513292b65f13af16e237bcc9e7713dec07d1d14e79ab54a509e
6
+ metadata.gz: 9b1bbb5b23cae02b4d51ab59c8a471f75cb46371236a6b9dd1494979cc9523c4a46c00e10e4402a713d6c9d426087e6f8ab7fd57bad48d0998b6457d90eb966a
7
+ data.tar.gz: d514d78ae12916da738b6d421a46eb4401d3e8ee3664dc8af67c84c21d26accb7ac2a8609450fe255ebb6ea38957d12a505c602402782fc69bb70c5976b59844
data/CHANGELOG.md ADDED
@@ -0,0 +1,133 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented here.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.2.0] - 2026-08-31
9
+
10
+ 0.1.0 could enrol a batch, detect completion and fire a callback. This release keeps that core
11
+ and builds the rest of the gem around it: a maintenance layer that repairs batches nothing else
12
+ can reach, a failure policy you control, and a third callback event. It also renames the
13
+ callback vocabulary to match Sidekiq Pro's, which is the change most likely to affect you.
14
+
15
+ ### Breaking
16
+
17
+ - **`:complete` now fires whatever the outcome.** In 0.1.0 it meant "all finished *and* all
18
+ succeeded"; that meaning is now `:success`. If you registered `on(:complete, MyWorker)` as a
19
+ success hook, it will start running on failed batches too, with nothing to warn you. Change
20
+ those registrations to `on(:success, MyWorker)` to keep 0.1.0's behaviour.
21
+ - Batch status `complete` is now `succeeded`, so each terminal status names the event that
22
+ fires with it. The stored integer is unchanged, so no data migration is needed, but code
23
+ comparing `batch.status == "complete"` or calling `complete_status?` must be updated.
24
+ `SidekiqBatchJob` statuses are untouched: a *job* that finished is still `complete`.
25
+ - `SidekiqBatch#fire_callback(event)` is gone, replaced by `#fire_callbacks`. A batch now
26
+ announces two events at once, so firing one by hand would spend an announcement the batch has
27
+ not finished making.
28
+ - `Sidekiq::Batch::Jobs.auto_install = false` and `.disable_auto_install!` are gone, along with
29
+ `.reset_installed!`. Configure the gem through `Sidekiq::Batch::Jobs.configure` instead.
30
+ - A batch can only be enrolled once. A second `jobs { … }` block on the same batch raises
31
+ `AlreadyStartedError` rather than enrolling into a batch whose `total_jobs` is already
32
+ stamped. There is no reopening a batch the way Sidekiq Pro allows.
33
+ - **A migration is required.** See *Upgrading from 0.1.0* below.
34
+
35
+ ### Added
36
+
37
+ - A configurable failure policy: `:any_failure` (the default, and what 0.1.0 always did),
38
+ `:all_failed`, `{ tolerate: 10 }` or `{ tolerate: "5%" }`, per batch or globally through
39
+ `config.failure_policy`. All four are the same rule with a different threshold, evaluated
40
+ inside the same atomic statement that transitions the batch, so choosing one never costs a
41
+ race. Nothing forgives a `jobs {}` block that died partway through enrolling: that batch
42
+ fails whatever the policy, with the cause recorded in `enrollment_error`.
43
+ - A third callback event. `:complete` fires however the batch went, `:success` and `:failure`
44
+ name the outcome and are mutually exclusive, so "always do X, and separately tell me when it
45
+ went badly" is two registrations rather than one worker doing both. Each event's enqueue is
46
+ claimed atomically and independently, so a callback that cannot be delivered never causes a
47
+ redelivery of one that already went out. Delivery is at-least-once: the push shares a
48
+ transaction with the claim, so a crash between the two rolls that claim back and the reaper
49
+ re-enqueues. Write callbacks to be idempotent.
50
+ - `SidekiqBatch::ReaperWorker`, three recoveries in one pass: batches whose `jobs {}` block died
51
+ mid-enrollment and left them `pending` forever, batches stalled by a job that vanished from
52
+ Redis (SIGKILL, OOM, pod eviction, or **Kill All** on Sidekiq's Retries page, which moves jobs
53
+ to the dead set without running death handlers), and callbacks orphaned by a crash between the
54
+ completion `UPDATE` and the enqueue.
55
+ - `SidekiqBatch::GroomerWorker`, which deletes terminal batches past the retention window and
56
+ long-abandoned `pending` ones as a backstop for hosts that do not run the reaper.
57
+ - `Sidekiq::Batch::Jobs.configure` for `base_class_name`, `stuck_after`, `retention`,
58
+ `failure_policy`, `on_alert`, `maintenance_queue`, `error_message_max` and `auto_install`.
59
+ `on_alert` is the gem's only unprompted signal; point it somewhere you read.
60
+ - `#percentage_progress`, `#eta` and `#terminal?` alongside the existing `#progress`.
61
+ - Rails 6.1 and Sidekiq 7 support. 0.1.0 required Ruby >= 3.2 and only worked on Rails 7+;
62
+ 0.2.0 runs on Ruby >= 3.0 and is tested against Rails 6.1, 7.1 and 8.0 with Sidekiq 7 and 8.
63
+
64
+ ### Fixed
65
+
66
+ Found by a full audit of 0.1.0. Each fix has a spec that was confirmed to fail against the
67
+ old code.
68
+
69
+ - **A job's row could be marked failed on its first attempt.** `final_attempt?` compared
70
+ `retry_count` against the wrong bound and read the worker class instead of the payload, so a
71
+ `retry: false` worker pushed with `set(retry: 5)` failed its batch while Sidekiq went on to
72
+ retry and succeed. Unrecoverable once it happened. The same bug made the middleware's
73
+ failure path dead code for every retrying worker.
74
+ - **An exception or a crash inside `jobs { … }` stranded the batch in `pending` forever.**
75
+ Its queued jobs ran, nothing ever announced, and neither the batch nor its rows were ever
76
+ cleaned up. Ordinary exceptions are now recorded and the batch finished; a killed process is
77
+ recovered by the reaper.
78
+ - **A batch's own callback could be enrolled into the batch it announces**, freezing
79
+ `total_jobs` below the row count and leaving a `pending` row nothing would look at.
80
+ - **Reopening a batch deleted it.** A second `jobs { … }` block that enrolled nothing, or
81
+ raised before its first push, destroyed the batch and every row tracking a job that was
82
+ already running.
83
+ - **Every Sidekiq job in the host application ran a Postgres query**, batch-tracked or not.
84
+ Untracked jobs now cost zero queries and have no Postgres coupling at all.
85
+ - **Enrollment cost five round trips per job**, two of which bought nothing. Now three, which
86
+ is the floor.
87
+ - Enrolling inside an open transaction was only detected at the start of the block, and only
88
+ on the default connection.
89
+ - `fire_callback` was public and consumed the batch's one announcement, so calling it early
90
+ permanently suppressed the real callback.
91
+ - A bookkeeping failure could reach the caller as if their enqueue had failed, inviting a retry
92
+ that enqueued the whole batch a second time; another could replace a job's own exception on
93
+ its way to Sidekiq, misreporting why the job died.
94
+ - `batch.destroy` loaded and destroyed children one row at a time despite the FK already
95
+ cascading.
96
+
97
+ ### Upgrading from 0.1.0
98
+
99
+ Four columns and two indexes:
100
+
101
+ ```ruby
102
+ class UpgradeSidekiqBatchTables < ActiveRecord::Migration[7.1]
103
+ def change
104
+ add_column :sidekiq_batches, :callbacks_fired, :jsonb, null: false, default: {}
105
+ add_column :sidekiq_batches, :failure_policy, :string
106
+ add_column :sidekiq_batches, :failure_tolerance, :integer
107
+ add_column :sidekiq_batches, :enrollment_error, :jsonb
108
+
109
+ add_index :sidekiq_batches, [:status, :created_at]
110
+ add_index :sidekiq_batch_jobs, :updated_at
111
+ remove_index :sidekiq_batches, :status
112
+ end
113
+ end
114
+ ```
115
+
116
+ The bare `:status` index goes because both composites lead with `status`, and Postgres serves a
117
+ status-only lookup from either. `sidekiq_batch_jobs.updated_at` is what stops the reaper
118
+ sequentially scanning the whole jobs table.
119
+
120
+ No data backfill. The status integers are unchanged, and existing rows keep a NULL
121
+ `failure_policy`, which the completion statement reads as `:any_failure`: exactly what 0.1.0
122
+ did. Batches that already announced under 0.1.0 have `callback_fired_at` set, so the new
123
+ orphaned-callback reaper leaves them alone.
124
+
125
+ Then re-read the `:complete` note under **Breaking** above. It is the one change that alters
126
+ behaviour without raising anything.
127
+
128
+ ## [0.1.0] - 2026-05-13
129
+
130
+ First release. `batch.jobs { … }` enrollment with rows committed before their jobs reach Redis,
131
+ atomic completion detection, two callback events (`:complete` and `:failure`), `#progress`, a
132
+ `context` column, and client middleware, server middleware and a death handler wired up by the
133
+ Rails engine.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Douglas Greyling
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.