postburner 1.0.0.rc.7 → 1.0.0.rc.9
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 +53 -0
- data/README.md +103 -58
- data/app/models/postburner/schedule_execution.rb +194 -43
- data/lib/generators/postburner/install/templates/migrations/create_postburner_jobs.rb.erb +1 -1
- data/lib/postburner/active_job/adapter.rb +129 -29
- data/lib/postburner/engine.rb +14 -7
- data/lib/postburner/scheduler.rb +59 -2
- data/lib/postburner/stats/app.rb +259 -0
- data/lib/postburner/stats/beanstalk_reader.rb +151 -0
- data/lib/postburner/stats.rb +202 -0
- data/lib/postburner/strategies/inline_test_queue.rb +1 -1
- data/lib/postburner/strategies/null_queue.rb +4 -1
- data/lib/postburner/tube.rb +2 -2
- data/lib/postburner/version.rb +1 -1
- data/lib/postburner.rb +168 -27
- metadata +4 -25
- data/app/assets/config/postburner_manifest.js +0 -1
- data/app/assets/stylesheets/postburner/application.css +0 -15
- data/app/controllers/postburner/application_controller.rb +0 -5
- data/app/controllers/postburner/jobs_controller.rb +0 -42
- data/app/controllers/postburner/static_controller.rb +0 -9
- data/app/helpers/postburner/application_helper.rb +0 -4
- data/app/views/layouts/postburner/application.html.haml +0 -10
- data/app/views/postburner/jobs/index.html.haml +0 -34
- data/app/views/postburner/jobs/show.html.haml +0 -13
- data/config/routes.rb +0 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d79c173b91d360bc76e72957b849d5cbccaf2208f180e562fd9ab8eb367ce172
|
|
4
|
+
data.tar.gz: d22b5a252c96544fad278757efefa45e7eb5d4a26b505715edc4ad318f2ede77
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c20d6bc35984b9957a4910c4cc0e066d96c830f67c52cfcc5478a22cb9631a1667bdc22925da315a3b3d0f7f3e47560ff9b3d7ea84a897f4a322376db3b6d897
|
|
7
|
+
data.tar.gz: f77570465b1faf7e5f29d0e201f9af2aa02d97cc38d88675ef847b9084116aa06bb082a2d0a085cb0ee5a412acac36ad950342cdfa62be288012520bd0a3efa0
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,58 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## Unreleased
|
|
4
|
+
|
|
5
|
+
## v1.0.0.rc.9 - 2026-11-28
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
|
|
9
|
+
- **`Postburner::Stats`.** Read-only aggregates returned as plain hashes: `jobs` (counts by state), `throughput` (processed per minute, hour, or day, zero-filled), `timing` (duration and lag percentiles per job class), and `tubes` (live Beanstalkd counts per watched tube). Tracked ActiveJob rows are reported under their ActiveJob class. No result includes job args, logs, or errata. `throughput` raises `ArgumentError` when `to` is before `from`.
|
|
10
|
+
- **`Postburner::Stats::App`.** A Rack app that serves `Postburner::Stats` as JSON (`/jobs`, `/throughput`, `/timing`, `/tubes`). The gem never mounts it. Construction raises without `authorize:` (a callable given the request) or `token:` (a bearer token of at least 16 characters), so a mount without one fails at boot. Authorization works with Devise, the Rails authentication generator, route constraints, or any other check on the request. The app keeps only a SHA-256 digest of the token and redacts it from `inspect`. `from`/`to` require `Z` or a numeric offset. Database queries run under a per-request `statement_timeout` (default 5 seconds), and `/tubes` reads Beanstalkd on its own connection with one 2-second deadline covering name lookup, connect, and reads; either limit answers 503. `Stats.jobs` runs as one statement, so its counts share a snapshot.
|
|
11
|
+
|
|
12
|
+
### Fixed
|
|
13
|
+
|
|
14
|
+
- **A failed Beanstalkd put no longer loses a scheduled occurrence.** `ScheduleExecution#enqueue!` commits `scheduled` and `enqueued_at` before the put. When the put failed, the execution kept no `beanstalk_job_id`, the watchdog's due sweep skipped it because it was not `pending`, and reconcile counted it as the schedule's live future. That occurrence never ran. The watchdog now retries these executions once they are older than `ScheduleExecution::PUT_GRACE` (2 minutes). If the job already has a `bkid`, the id is linked and nothing is put. Otherwise the same row is put again: the `Postburner::Job`, or the `Postburner::TrackedJob` a tracked ActiveJob committed before its put failed, which keeps its ActiveJob tube, priority, and TTR. A plain ActiveJob has no row and is enqueued again. A retry that fails raises, and the next watchdog pass tries again.
|
|
15
|
+
- **`ScheduleExecution#enqueue!` no longer creates a second job on a scheduled execution with no Beanstalkd id.** It used to call `create!` again and repoint `job_id`, leaving the first job queued with no `bkid`. A tracked ActiveJob whose put fails now records its `TrackedJob` on the execution, so a retry does not create a second one.
|
|
16
|
+
|
|
17
|
+
### Removed
|
|
18
|
+
|
|
19
|
+
- **The web UI.** The engine no longer ships controllers, views, routes, or assets. `Postburner::Job`, `Postburner::TrackedJob`, and `Postburner::Tube` remain the way to inspect jobs from a console or from your own code. The partial indexes on `postburner_jobs` are unchanged.
|
|
20
|
+
- **The `haml-rails` dependency.** Nothing in the gem renders HAML any more.
|
|
21
|
+
|
|
22
|
+
### Upgrade notes
|
|
23
|
+
|
|
24
|
+
- Remove `mount Postburner::Engine => ...` from `config/routes.rb`. Mounting still boots, but the mount serves no routes.
|
|
25
|
+
- Remove `//= link postburner_manifest.js` from an app's Sprockets manifest. The file no longer exists, and Sprockets raises when a linked file is missing.
|
|
26
|
+
- An application that renders HAML and got `haml-rails` only through Postburner adds it to its own Gemfile.
|
|
27
|
+
- The first watchdog pass after upgrading heals executions whose put failed before the upgrade. One whose `run_at` has passed follows `catch_up`: superseded with `catch_up: false`, run once with `catch_up: true`. To see how many exist first:
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
Postburner::ScheduleExecution.scheduled.where(beanstalk_job_id: nil)
|
|
31
|
+
.where('run_at <= ?', Time.current).joins(:schedule)
|
|
32
|
+
.group('postburner_schedules.name', 'postburner_schedules.catch_up').count
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
- The heal is skipped under a testing strategy (`NullQueue`, `InlineTestQueue`, `TimeTravelTestQueue`), where executions normally have no Beanstalkd id.
|
|
36
|
+
- One duplicate is still possible: if a put succeeded and the process died before the id was saved anywhere, the retry puts a second Beanstalkd job.
|
|
37
|
+
|
|
38
|
+
## v1.0.0.rc.8 - 2026-10-05
|
|
39
|
+
|
|
40
|
+
### Fixed
|
|
41
|
+
|
|
42
|
+
- **A testing strategy no longer drops ActiveJob enqueues outside the test environment.** `NullQueue`, `InlineTestQueue`, and `TimeTravelTestQueue` still keep `Postburner::Job` off Beanstalkd in every environment. `perform_later`, `deliver_later`, and `broadcast_*_later` skip the put only when `Rails.env.test?` and the strategy is a testing strategy. In production the adapter always puts, including after `Postburner.null_strategy!`.
|
|
43
|
+
- **The test strategy is chosen at boot.** The engine reads `config.active_job.queue_adapter` from `after_initialize`, so the choice is in place before the first test and does not wait for `ActiveJob::Base` to load. A strategy assigned in an initializer, or later in `test_helper.rb`, is kept. `:test` selects `InlineTestQueue`. `:postburner` selects `NullQueue`.
|
|
44
|
+
- **Boot no longer reads `config.active_job` unless the test environment has that railtie.** An application that requires Active Record, Action Controller, and Action View without the ActiveJob railtie has no `config.active_job`. The test-strategy hook returns before touching it outside test, and when `config` does not respond to `active_job`.
|
|
45
|
+
|
|
46
|
+
### Changed
|
|
47
|
+
|
|
48
|
+
- **`perform_later` waits for the surrounding transaction to commit.** Selecting the Postburner adapter sets `ActiveJob::Base.enqueue_after_transaction_commit = true`. A job enqueued inside a joinable transaction is put after commit and dropped on rollback. A Beanstalkd error on that put is raised after the data is committed. The flag lives on `ActiveJob::Base`, so a job class that uses another adapter in the same process inherits the same deferral.
|
|
49
|
+
- **In the test environment, `:postburner` does not write to Beanstalkd.** `Postburner::Job#queue!` keeps the row (`NullQueue`). ActiveJob enqueues still write a tracked row when the job is tracked, still emit the enqueue notification, and do not put. `Postburner.default_strategy!` or `Postburner.strict_strategy!` is the opt-in that puts.
|
|
50
|
+
|
|
51
|
+
### Upgrade notes
|
|
52
|
+
|
|
53
|
+
- A test that asserts a real tube job after `perform_later` or `queue!` calls `Postburner.default_strategy!` or `Postburner.strict_strategy!`.
|
|
54
|
+
- `Postburner.null_strategy!` in production still skips the tube for `Postburner::Job` only. ActiveJob continues to put.
|
|
55
|
+
|
|
3
56
|
## v1.0.0.rc.7 - 2026-08-03
|
|
4
57
|
|
|
5
58
|
### Fixed
|
data/README.md
CHANGED
|
@@ -101,10 +101,10 @@ bundle exec rake postburner:work WORKER=default
|
|
|
101
101
|
- [Callbacks](#callbacks)
|
|
102
102
|
- [Instrumentation](#instrumentation)
|
|
103
103
|
- [Logging](#logging)
|
|
104
|
+
- [Stats](#stats)
|
|
104
105
|
- [Why Beanstalkd?](#why-beanstalkd)
|
|
105
106
|
- [Beanstalkd Integration](#beanstalkd-integration)
|
|
106
107
|
- [Installation](#installation)
|
|
107
|
-
- [Web UI - v2 Coming Soon](#web-ui)
|
|
108
108
|
|
|
109
109
|
## Why Postburner?
|
|
110
110
|
|
|
@@ -500,6 +500,7 @@ The scheduler uses **immediate enqueue** combined with a **watchdog safety net**
|
|
|
500
500
|
4. When a worker reserves the watchdog, it instantiates `Postburner::Scheduler` which:
|
|
501
501
|
- Acquires a PostgreSQL advisory lock for coordination
|
|
502
502
|
- Reconciles every schedule (see [Editing & Pausing Schedules](#editing--pausing-schedules)) — auto-bootstrapping unstarted schedules, ensuring each enabled schedule has exactly one future execution queued, and tearing down lingering futures on disabled schedules
|
|
503
|
+
- Retries any execution whose Beanstalkd put failed. The execution is marked `scheduled` before the put, so a failed put leaves it with no `beanstalk_job_id`. After a 2-minute grace the watchdog links the job's `bkid` if the put did succeed, or puts the same job again. A missed one (`run_at` passed) follows the catch-up policy below.
|
|
503
504
|
- Re-queues a new watchdog with delay for the next interval
|
|
504
505
|
|
|
505
506
|
NOTE: The watchdog is ephemeral data in Beanstalkd, not a database record. `Postburner::Scheduler` is the handler class that does the work. This design requires no dedicated scheduler process - existing workers handle everything.
|
|
@@ -658,6 +659,7 @@ The `catch_up` option controls what happens when the worker is down and misses s
|
|
|
658
659
|
Notes:
|
|
659
660
|
1) Previously created executions and enqueued jobs are not affected by the catch-up policy.
|
|
660
661
|
2) If `catch_up` if off, then there the skipped executions are not created or tracked. It resumes from the next future execution.
|
|
662
|
+
3) The policy also covers an execution whose Beanstalkd put failed and was not retried before its `run_at`. With `catch_up: false` the watchdog supersedes it. With `catch_up: true` it runs once, as soon as the put succeeds.
|
|
661
663
|
|
|
662
664
|
```ruby
|
|
663
665
|
# Skip missed executions (default)
|
|
@@ -919,15 +921,15 @@ Pay attention to the following when writing jobs:
|
|
|
919
921
|
|
|
920
922
|
## Queue Strategies
|
|
921
923
|
|
|
922
|
-
Postburner uses different strategies to control
|
|
924
|
+
Postburner uses different strategies to control `Postburner::Job` execution. In the test environment a testing strategy also tells the ActiveJob adapter not to put to Beanstalkd. Outside test the adapter always puts. `DefaultQueue` and `StrictQueue` put in every environment.
|
|
923
925
|
|
|
924
926
|
| Strategy | When to Use | Behavior | Requires Beanstalkd |
|
|
925
927
|
|----------|-------------|----------|---------------------|
|
|
926
|
-
| **DefaultQueue** | Production | Async, gracefully requeues premature jobs | Yes |
|
|
927
|
-
| **StrictQueue** | Production | Async via Beanstalkd, raises error on premature execution | Yes |
|
|
928
|
-
| **NullQueue** | Test
|
|
929
|
-
| **InlineTestQueue** | Test | Inline execution (error raised for delayed jobs) | No |
|
|
930
|
-
| **TimeTravelTestQueue** | Test | Inline execution, (auto time-travels for delayed jobs) | No |
|
|
928
|
+
| **DefaultQueue** | Production, or a test that needs a real tube | Async, gracefully requeues premature jobs. The ActiveJob adapter puts. | Yes |
|
|
929
|
+
| **StrictQueue** | Production, or a test that needs a real tube | Async via Beanstalkd, raises error on premature execution. The ActiveJob adapter puts. | Yes |
|
|
930
|
+
| **NullQueue** | Test default when the adapter is `:postburner`. Also deferred or manual `Postburner::Job` execution | Jobs created but not inserted into Beanstalkd. In test, the ActiveJob adapter does not put. Outside test, the adapter still puts. | No |
|
|
931
|
+
| **InlineTestQueue** | Test default when the adapter is `:test` | Inline execution (error raised for delayed jobs). In test, the ActiveJob adapter does not put. | No |
|
|
932
|
+
| **TimeTravelTestQueue** | Test | Inline execution, (auto time-travels for delayed jobs). In test, the ActiveJob adapter does not put. | No |
|
|
931
933
|
|
|
932
934
|
```ruby
|
|
933
935
|
# Switch strategies
|
|
@@ -950,45 +952,33 @@ There are 4 hook methods that are used with inheritance:
|
|
|
950
952
|
|
|
951
953
|
### Automatic Test Mode
|
|
952
954
|
|
|
953
|
-
|
|
955
|
+
`config.active_job.queue_adapter` is the only setup. At the end of boot the test environment selects a strategy that does not write to Beanstalkd. A strategy assigned in an initializer is already set by then and is kept. A strategy assigned in `test_helper.rb` runs after boot and is kept:
|
|
956
|
+
|
|
957
|
+
| `queue_adapter` | `Postburner::Job#queue!` | ActiveJob `perform_later` |
|
|
958
|
+
|-----------------|--------------------------|---------------------------|
|
|
959
|
+
| `:test` | `InlineTestQueue` (runs in process) | Rails keeps the job in memory |
|
|
960
|
+
| `:postburner` | `NullQueue` (row kept, run it with `Postburner::Job.perform`) | Tracked jobs still write their row. Nothing is put. |
|
|
954
961
|
|
|
955
962
|
```ruby
|
|
956
|
-
#
|
|
957
|
-
|
|
963
|
+
Postburner.default_strategy! # this example needs a real tube
|
|
964
|
+
SendEmail.perform_later(123) # now the adapter puts
|
|
958
965
|
```
|
|
959
966
|
|
|
960
|
-
Postburner
|
|
961
|
-
|
|
962
|
-
### Testing Default Jobs (ActiveJob)
|
|
967
|
+
`Postburner.strict_strategy!` is the same opt-in with strict premature handling. `assert_enqueued_jobs` still needs the `:test` adapter, via `config/environments/test.rb` or `queue_adapter_for_test`.
|
|
963
968
|
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
```ruby
|
|
967
|
-
class MyTest < ActiveSupport::TestCase
|
|
968
|
-
test "job executes" do
|
|
969
|
-
SendEmail.perform_later(123)
|
|
970
|
-
# Job executes immediately in test mode
|
|
971
|
-
end
|
|
972
|
-
|
|
973
|
-
test "job with delay" do
|
|
974
|
-
travel 1.hour do
|
|
975
|
-
SendEmail.set(wait: 1.hour).perform_later(123)
|
|
976
|
-
end
|
|
977
|
-
end
|
|
978
|
-
end
|
|
979
|
-
```
|
|
969
|
+
An application that does not load the ActiveJob railtie has no `config.active_job`, so test boot leaves `DefaultQueue` in place and `Postburner::Job#queue!` puts. Call `Postburner.null_strategy!` in that app. `:inline` and `:async` leave `DefaultQueue` for the same reason: only `:test` and `:postburner` select a strategy that does not put.
|
|
980
970
|
|
|
981
971
|
### Testing Tracked Jobs
|
|
982
972
|
|
|
983
|
-
|
|
973
|
+
With the `:postburner` adapter in test, `perform_later` writes the `TrackedJob` row and leaves `bkid` nil:
|
|
984
974
|
|
|
985
975
|
```ruby
|
|
986
|
-
test "tracked job
|
|
976
|
+
test "tracked job records the enqueue" do
|
|
987
977
|
ProcessPayment.perform_later(456)
|
|
988
978
|
|
|
989
979
|
job = Postburner::TrackedJob.last
|
|
990
|
-
assert job.
|
|
991
|
-
|
|
980
|
+
assert job.queued_at.present?
|
|
981
|
+
assert_nil job.bkid
|
|
992
982
|
end
|
|
993
983
|
```
|
|
994
984
|
|
|
@@ -1014,16 +1004,16 @@ class EmailTest < ActiveSupport::TestCase
|
|
|
1014
1004
|
end
|
|
1015
1005
|
```
|
|
1016
1006
|
|
|
1017
|
-
**How this works:** `assert_enqueued_emails` and `assert_enqueued_jobs`
|
|
1007
|
+
**How this works:** `assert_enqueued_emails` and `assert_enqueued_jobs` read Rails' `:test` adapter. They do not see jobs enqueued through `:postburner`. Set the test adapter when a test needs those assertions. With `:postburner` left in place, test enqueues stay out of Beanstalkd until the test calls `Postburner.default_strategy!` or `Postburner.strict_strategy!`.
|
|
1018
1008
|
|
|
1019
|
-
|
|
1009
|
+
Rails uses the `:test` adapter in the test environment unless the application sets another one:
|
|
1020
1010
|
|
|
1021
1011
|
```ruby
|
|
1022
1012
|
# config/environments/test.rb
|
|
1023
1013
|
config.active_job.queue_adapter = :test
|
|
1024
1014
|
```
|
|
1025
1015
|
|
|
1026
|
-
|
|
1016
|
+
Without the `:test` adapter, `assert_enqueued_emails` does not see the enqueue. Postburner does not run the job in its place, and it does not put one on a tube either.
|
|
1027
1017
|
|
|
1028
1018
|
**`Postburner::Mailer`** bypasses ActiveJob entirely, so `assert_enqueued_emails` cannot see these jobs regardless of adapter. Assert on side effects instead:
|
|
1029
1019
|
|
|
@@ -1637,7 +1627,7 @@ Postburner emits ActiveSupport::Notifications events following Rails conventions
|
|
|
1637
1627
|
|-------|------|--------------|
|
|
1638
1628
|
| `create.schedule.postburner` | When schedule is created | `:schedule` |
|
|
1639
1629
|
| `update.schedule.postburner` | When schedule is updated | `:schedule`, `:changes` |
|
|
1640
|
-
| `audit.schedule.postburner` | When scheduler audits a schedule | `:schedule`, `:execution_created`, `:orphans_enqueued` |
|
|
1630
|
+
| `audit.schedule.postburner` | When scheduler audits a schedule | `:schedule`, `:execution_created`, `:orphans_enqueued`, `:puts_linked`, `:puts_retried`, `:missed_superseded` |
|
|
1641
1631
|
| `reconcile.schedule.postburner` | When a schedule is reconciled (executions converged to config) | `:schedule`, `:superseded`, `:created` |
|
|
1642
1632
|
|
|
1643
1633
|
**Schedule Payload Structure:**
|
|
@@ -1809,6 +1799,82 @@ end
|
|
|
1809
1799
|
|
|
1810
1800
|
These job-specific logs are stored in the database (for tracked jobs only) and are separate from Rails application logs, providing a complete audit trail for critical operations.
|
|
1811
1801
|
|
|
1802
|
+
## Stats
|
|
1803
|
+
|
|
1804
|
+
`Postburner::Stats` returns read-only aggregates as plain hashes, ready for `JSON.generate`. It does no HTTP and no authentication: call it from a console, your own controller, or an exporter.
|
|
1805
|
+
|
|
1806
|
+
```ruby
|
|
1807
|
+
Postburner::Stats.jobs
|
|
1808
|
+
# => { active: 12, due: 3, scheduled: 9, unqueued: 0, failing: 1 }
|
|
1809
|
+
|
|
1810
|
+
Postburner::Stats.throughput(from: 6.hours.ago, bucket: 'hour') # 'minute', 'hour', or 'day'
|
|
1811
|
+
# => [{ at: "2026-11-14T10:00:00Z", processed: 41 }, { at: "2026-11-14T11:00:00Z", processed: 0 }, ...]
|
|
1812
|
+
|
|
1813
|
+
Postburner::Stats.timing(from: 24.hours.ago)
|
|
1814
|
+
# => [{ job_class: "ProcessPayment", processed: 310, errored: 4,
|
|
1815
|
+
# duration_p50: 120, duration_p95: 840, lag_p95: 35 }, ...]
|
|
1816
|
+
|
|
1817
|
+
Postburner::Stats.tubes # timeout: 2 (seconds); raises Postburner::Stats::Unavailable
|
|
1818
|
+
# => [{ tube: "postburner.production.default", ready: 2, delayed: 14, reserved: 3, buried: 0 }, ...]
|
|
1819
|
+
```
|
|
1820
|
+
|
|
1821
|
+
- `jobs`, `throughput`, and `timing` read `postburner_jobs`, so they cover `Postburner::Job` subclasses and tracked ActiveJob classes. Default ActiveJob jobs write no row; only `tubes` includes them.
|
|
1822
|
+
- `timing` reports a tracked ActiveJob under its own class name, not `Postburner::TrackedJob`.
|
|
1823
|
+
- Times are UTC. Durations and lag are milliseconds. Empty `throughput` buckets are filled with zero. A window of more than 10,000 buckets, or one whose `to` is before `from`, raises `ArgumentError`.
|
|
1824
|
+
- No result includes job args, logs, or errata.
|
|
1825
|
+
|
|
1826
|
+
### Serving stats over HTTP
|
|
1827
|
+
|
|
1828
|
+
Postburner adds no routes or middleware. Nothing is reachable over HTTP until you mount `Postburner::Stats::App`, a small Rack app that serves the methods above as JSON. It has no views and no controllers, and it works with any authentication setup.
|
|
1829
|
+
|
|
1830
|
+
The app denies every request by default. Construction raises unless you pass `authorize:`, `token:`, or both. A request is allowed when the bearer token matches or `authorize` returns truthy. If `authorize` raises, the request is denied with 403, not answered with 500.
|
|
1831
|
+
|
|
1832
|
+
```ruby
|
|
1833
|
+
# config/routes.rb
|
|
1834
|
+
|
|
1835
|
+
# Devise: authenticate at the route
|
|
1836
|
+
authenticate :user, ->(user) { user.admin? } do
|
|
1837
|
+
mount Postburner::Stats::App.new(authorize: ->(_request) { true }), at: "/postburner/stats"
|
|
1838
|
+
end
|
|
1839
|
+
|
|
1840
|
+
# Devise: authenticate inside the app
|
|
1841
|
+
mount Postburner::Stats::App.new(
|
|
1842
|
+
authorize: ->(request) { request.env["warden"]&.user(:user)&.admin? }
|
|
1843
|
+
), at: "/postburner/stats"
|
|
1844
|
+
|
|
1845
|
+
# Rails authentication generator (signed session_id cookie)
|
|
1846
|
+
mount Postburner::Stats::App.new(
|
|
1847
|
+
authorize: ->(request) { Session.find_by(id: request.cookie_jar.signed[:session_id])&.user&.admin? }
|
|
1848
|
+
), at: "/postburner/stats"
|
|
1849
|
+
|
|
1850
|
+
# Any route constraint
|
|
1851
|
+
constraints AdminConstraint.new do
|
|
1852
|
+
mount Postburner::Stats::App.new(authorize: ->(_request) { true }), at: "/postburner/stats"
|
|
1853
|
+
end
|
|
1854
|
+
|
|
1855
|
+
# Machine clients (dashboards, monitoring): Authorization: Bearer <token>
|
|
1856
|
+
mount Postburner::Stats::App.new(token: ENV.fetch("POSTBURNER_STATS_TOKEN")), at: "/postburner/stats"
|
|
1857
|
+
```
|
|
1858
|
+
|
|
1859
|
+
`authorize` receives an `ActionDispatch::Request`. Tokens must be at least 16 characters. The app keeps only a SHA-256 digest of the token, and its `inspect` (which `bin/rails routes` prints) shows `[REDACTED]`.
|
|
1860
|
+
|
|
1861
|
+
| Endpoint | Parameters | Returns |
|
|
1862
|
+
|---|---|---|
|
|
1863
|
+
| `GET /jobs` | | `Stats.jobs` |
|
|
1864
|
+
| `GET /throughput` | `from`, `to`, `bucket` (`minute`, `hour`, `day`) | `Stats.throughput` |
|
|
1865
|
+
| `GET /timing` | `from`, `to` | `Stats.timing` |
|
|
1866
|
+
| `GET /tubes` | | `Stats.tubes` |
|
|
1867
|
+
|
|
1868
|
+
`from` and `to` are ISO 8601 with `Z` or a numeric offset (`2026-11-20T00:00:00Z`, `2026-11-20T09:00:00+09:00`). A time without an offset is rejected, because Ruby would read it in the server's local zone. They default to the last 24 hours, and the window may not exceed 31 days.
|
|
1869
|
+
|
|
1870
|
+
Database queries run with `SET LOCAL statement_timeout` inside a savepoint that is always rolled back, so the setting never outlives the request. The default is 5 seconds; pass `statement_timeout:` (seconds, or `nil` to keep the connection's own setting) to change it. `/jobs` is a single statement, so the timeout covers the whole endpoint. `/tubes` reads Beanstalkd on its own short-lived connection with one 2-second deadline covering the name lookup, the connect, and every read, so a stalled Beanstalkd cannot hold a web thread. The lookup deadline is enforced on Ruby 3.4+, and on earlier Rubies on Linux (glibc); elsewhere, use an IP address in `beanstalk_url` to skip the lookup.
|
|
1871
|
+
|
|
1872
|
+
Statuses: 200, 400 for bad parameters, 403 when not authorized (checked first, for every path and method), 404 for an unknown path, 405 for methods other than GET and HEAD, and 503 when Beanstalkd is unreachable or slow, or a query exceeds the statement timeout. Responses send `cache-control: no-store`.
|
|
1873
|
+
|
|
1874
|
+
The app sets no CORS headers. For browser requests from another origin, add them in the host, for example with `rack-cors`.
|
|
1875
|
+
|
|
1876
|
+
`Postburner::Stats` itself has no authentication. If you call it from your own controller, that controller's access rules apply.
|
|
1877
|
+
|
|
1812
1878
|
## Why Beanstalkd?
|
|
1813
1879
|
|
|
1814
1880
|
Beanstalkd is a simple, fast, and reliable queue system. It is a good choice for production environments where you want fast background processing for most jobs, but comprehensive auditing for critical operations.
|
|
@@ -2334,27 +2400,6 @@ Postburner.connected do |conn|
|
|
|
2334
2400
|
end
|
|
2335
2401
|
```
|
|
2336
2402
|
|
|
2337
|
-
## Web UI - v2 Coming Soon
|
|
2338
|
-
|
|
2339
|
-
Mount the inspection interface:
|
|
2340
|
-
|
|
2341
|
-
```ruby
|
|
2342
|
-
# config/routes.rb
|
|
2343
|
-
mount Postburner::Engine => "/postburner"
|
|
2344
|
-
|
|
2345
|
-
# Only in development
|
|
2346
|
-
mount Postburner::Engine => "/postburner" if Rails.env.development?
|
|
2347
|
-
```
|
|
2348
|
-
|
|
2349
|
-
Add your own authentication:
|
|
2350
|
-
|
|
2351
|
-
```ruby
|
|
2352
|
-
# config/routes.rb
|
|
2353
|
-
authenticate :user, ->(user) { user.admin? } do
|
|
2354
|
-
mount Postburner::Engine => "/postburner"
|
|
2355
|
-
end
|
|
2356
|
-
```
|
|
2357
|
-
|
|
2358
2403
|
## Installation
|
|
2359
2404
|
|
|
2360
2405
|
### 1. Install Beanstalkd
|
|
@@ -91,8 +91,23 @@ module Postburner
|
|
|
91
91
|
validates :status, presence: true
|
|
92
92
|
validate :validate_next_run_at!
|
|
93
93
|
|
|
94
|
+
# How long a scheduled execution may go without a Beanstalkd id before a
|
|
95
|
+
# retry treats its put as failed.
|
|
96
|
+
#
|
|
97
|
+
# #enqueue! commits `scheduled` and `enqueued_at` before the put, and records
|
|
98
|
+
# `beanstalk_job_id` only after the put returns. Until then the row looks
|
|
99
|
+
# exactly like one whose put failed. The grace keeps a second caller from
|
|
100
|
+
# putting again inside that window. It is well over a Beanstalkd connect and
|
|
101
|
+
# under the watchdog interval, so the next watchdog pass retries a failed put.
|
|
102
|
+
PUT_GRACE = 2.minutes
|
|
103
|
+
|
|
94
104
|
# Scopes
|
|
95
105
|
scope :due, -> { pending.where('run_at <= ?', Time.current).where(enqueued_at: nil) }
|
|
106
|
+
# Scheduled executions with no recorded Beanstalkd id, older than the grace.
|
|
107
|
+
# Either the put failed or its id was never linked; see #enqueue!.
|
|
108
|
+
scope :put_unconfirmed, ->(as_of: PUT_GRACE.ago) {
|
|
109
|
+
scheduled.where(beanstalk_job_id: nil).where('enqueued_at <= ?', as_of)
|
|
110
|
+
}
|
|
96
111
|
scope :future, -> { where('run_at > ?', Time.current).order(run_at: :asc) }
|
|
97
112
|
scope :past, -> { where('run_at <= ?', Time.current).order(run_at: :desc) }
|
|
98
113
|
|
|
@@ -119,6 +134,24 @@ module Postburner
|
|
|
119
134
|
# - ActiveJob classes: Enqueues directly without database tracking
|
|
120
135
|
#
|
|
121
136
|
# This method is idempotent - calling it multiple times will only enqueue once.
|
|
137
|
+
# On a `scheduled` execution with no `beanstalk_job_id` it does not create a
|
|
138
|
+
# second job:
|
|
139
|
+
#
|
|
140
|
+
# - If the linked Postburner::Job already has a `bkid`, the put succeeded and
|
|
141
|
+
# only the link was lost. The id is copied over and nothing is put.
|
|
142
|
+
# - Otherwise, once `enqueued_at` is older than {PUT_GRACE}, the put is
|
|
143
|
+
# retried on the same row: the Postburner::Job, or the TrackedJob a
|
|
144
|
+
# tracked ActiveJob committed before its put failed. A plain ActiveJob has
|
|
145
|
+
# no row, so it is built and enqueued again.
|
|
146
|
+
# - Inside the grace, or when the linked job has already been processed or
|
|
147
|
+
# removed, it does nothing.
|
|
148
|
+
#
|
|
149
|
+
# A retry whose run_at has passed is put with no delay and runs once. Callers
|
|
150
|
+
# that must honor `catch_up: false` decide before calling; see
|
|
151
|
+
# Postburner::Scheduler#process_schedule.
|
|
152
|
+
#
|
|
153
|
+
# One duplicate remains possible: if a put succeeded and the process died
|
|
154
|
+
# before the id was saved anywhere, the retry puts a second Beanstalkd job.
|
|
122
155
|
#
|
|
123
156
|
# Instruments with ActiveSupport::Notifications:
|
|
124
157
|
# - enqueue.schedule_execution.postburner: When execution is enqueued
|
|
@@ -135,48 +168,44 @@ module Postburner
|
|
|
135
168
|
#
|
|
136
169
|
def enqueue!
|
|
137
170
|
return if enqueued?
|
|
171
|
+
return retry_put! if scheduled? && beanstalk_job_id.nil?
|
|
138
172
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
# which the watchdog can detect and re-queue. This is safer than having a
|
|
142
|
-
# pending execution with a job already in Beanstalkd (which causes duplicates).
|
|
143
|
-
#
|
|
144
|
-
# BOTH enqueue paths defer the actual Beanstalkd insertion until this
|
|
145
|
-
# transaction commits:
|
|
146
|
-
# - Postburner::Job#queue! inserts via an after-commit hook;
|
|
147
|
-
# - the ActiveJob adapter declares enqueue_after_transaction_commit?.
|
|
148
|
-
# So the real Beanstalkd id (and, for tracked ActiveJobs, the TrackedJob
|
|
149
|
-
# row) only exists AFTER the transaction below. We capture the job here and
|
|
150
|
-
# link/record the Beanstalkd id afterward.
|
|
151
|
-
tracked_job_instance = nil
|
|
152
|
-
pending_activejob = nil
|
|
153
|
-
transaction do
|
|
154
|
-
update_columns(
|
|
155
|
-
enqueued_at: Time.current,
|
|
156
|
-
status: self.class.statuses[:scheduled]
|
|
157
|
-
)
|
|
173
|
+
put!
|
|
174
|
+
end
|
|
158
175
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
176
|
+
# Copy the linked job's Beanstalkd id onto this execution when the put
|
|
177
|
+
# succeeded but the link was never written.
|
|
178
|
+
#
|
|
179
|
+
# Applies to a Postburner::Job (or tracked ActiveJob) row with a `bkid`. No
|
|
180
|
+
# age check: a saved `bkid` means the job is on Beanstalkd.
|
|
181
|
+
#
|
|
182
|
+
# @return [Boolean] true if the id was linked
|
|
183
|
+
#
|
|
184
|
+
def link_confirmed_put!
|
|
185
|
+
return false if beanstalk_job_id.present? || job_id.nil?
|
|
165
186
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
if tracked_job_instance
|
|
169
|
-
update_columns(beanstalk_job_id: tracked_job_instance.reload.bkid)
|
|
170
|
-
elsif pending_activejob
|
|
171
|
-
link_activejob!(pending_activejob)
|
|
172
|
-
end
|
|
187
|
+
bkid = job&.bkid
|
|
188
|
+
return false if bkid.blank?
|
|
173
189
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
190
|
+
update_columns(beanstalk_job_id: bkid)
|
|
191
|
+
true
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
# Whether the current queue strategy would put this execution's job to
|
|
195
|
+
# Beanstalkd.
|
|
196
|
+
#
|
|
197
|
+
# A testing strategy keeps Postburner::Job off Beanstalkd, and in the test
|
|
198
|
+
# environment also stops ActiveJob puts. Under those, a missing
|
|
199
|
+
# `beanstalk_job_id` is normal and is not a failed put.
|
|
200
|
+
#
|
|
201
|
+
# @return [Boolean]
|
|
202
|
+
#
|
|
203
|
+
def puts_to_beanstalkd?
|
|
204
|
+
if tracked_job?
|
|
205
|
+
!Postburner.testing?
|
|
206
|
+
else
|
|
207
|
+
!Postburner.suppress_active_job_enqueue?
|
|
208
|
+
end
|
|
180
209
|
end
|
|
181
210
|
|
|
182
211
|
# Skip this execution ("skip this one occurrence, keep the schedule running").
|
|
@@ -305,6 +334,110 @@ module Postburner
|
|
|
305
334
|
|
|
306
335
|
private
|
|
307
336
|
|
|
337
|
+
# Retry the put for a scheduled execution with no Beanstalkd id.
|
|
338
|
+
#
|
|
339
|
+
# @return [void]
|
|
340
|
+
#
|
|
341
|
+
# @api private
|
|
342
|
+
#
|
|
343
|
+
def retry_put!
|
|
344
|
+
return if link_confirmed_put!
|
|
345
|
+
return if enqueued_at.present? && enqueued_at > PUT_GRACE.ago
|
|
346
|
+
|
|
347
|
+
if job_id.present? && (existing = job)
|
|
348
|
+
return if existing.processed_at.present? || existing.removed_at.present?
|
|
349
|
+
|
|
350
|
+
# Restart the grace so no other caller puts again while this one is in
|
|
351
|
+
# flight. A past run_at gets no delay.
|
|
352
|
+
update_columns(enqueued_at: Time.current)
|
|
353
|
+
if existing.is_a?(Postburner::TrackedJob)
|
|
354
|
+
# The row does not carry the ActiveJob's tube, priority, or TTR, so
|
|
355
|
+
# put it through the adapter, which rebuilds them from its args.
|
|
356
|
+
::ActiveJob::QueueAdapters::PostburnerAdapter.new.requeue_tracked(existing, run_at.to_f)
|
|
357
|
+
else
|
|
358
|
+
# queue! returns early only when queued_at AND bkid are set, so it
|
|
359
|
+
# puts this same row again.
|
|
360
|
+
existing.queue!(at: run_at)
|
|
361
|
+
end
|
|
362
|
+
bkid = existing.reload.bkid
|
|
363
|
+
update_columns(beanstalk_job_id: bkid) if bkid.present?
|
|
364
|
+
|
|
365
|
+
instrument_enqueue!
|
|
366
|
+
else
|
|
367
|
+
put!
|
|
368
|
+
end
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
# Mark scheduled, create or build the job, and put it after commit.
|
|
372
|
+
#
|
|
373
|
+
# @return [void]
|
|
374
|
+
#
|
|
375
|
+
# @api private
|
|
376
|
+
#
|
|
377
|
+
def put!
|
|
378
|
+
# Mark as scheduled BEFORE Beanstalkd operations to prevent duplicates.
|
|
379
|
+
# If the put fails, the execution is scheduled with no beanstalk_job_id.
|
|
380
|
+
# #enqueue! retries that row once PUT_GRACE has passed, and the watchdog
|
|
381
|
+
# sweep (Scheduler#process_schedule) calls it on every pass. This is safer
|
|
382
|
+
# than leaving a pending execution with a job already in Beanstalkd, which
|
|
383
|
+
# the due sweep would put a second time.
|
|
384
|
+
#
|
|
385
|
+
# BOTH enqueue paths defer the actual Beanstalkd insertion until this
|
|
386
|
+
# transaction commits:
|
|
387
|
+
# - Postburner::Job#queue! inserts via an after-commit hook;
|
|
388
|
+
# - selecting the Postburner adapter sets
|
|
389
|
+
# ActiveJob::Base.enqueue_after_transaction_commit, so Rails holds
|
|
390
|
+
# the adapter enqueue until this transaction commits.
|
|
391
|
+
# So the real Beanstalkd id (and, for tracked ActiveJobs, the TrackedJob
|
|
392
|
+
# row) only exists AFTER the transaction below. We capture the job here and
|
|
393
|
+
# link/record the Beanstalkd id afterward.
|
|
394
|
+
tracked_job_instance = nil
|
|
395
|
+
pending_activejob = nil
|
|
396
|
+
begin
|
|
397
|
+
transaction do
|
|
398
|
+
update_columns(
|
|
399
|
+
enqueued_at: Time.current,
|
|
400
|
+
status: self.class.statuses[:scheduled]
|
|
401
|
+
)
|
|
402
|
+
|
|
403
|
+
if tracked_job?
|
|
404
|
+
tracked_job_instance = enqueue_tracked_job!
|
|
405
|
+
else
|
|
406
|
+
pending_activejob = build_and_enqueue_activejob!
|
|
407
|
+
end
|
|
408
|
+
end
|
|
409
|
+
rescue StandardError
|
|
410
|
+
# A tracked ActiveJob commits its TrackedJob row before the put. Record
|
|
411
|
+
# it here so a retry puts that row again instead of creating another.
|
|
412
|
+
link_failed_activejob!(pending_activejob) if pending_activejob
|
|
413
|
+
raise
|
|
414
|
+
end
|
|
415
|
+
|
|
416
|
+
# After commit: the deferred insertions have run, so the Beanstalkd id (and
|
|
417
|
+
# any TrackedJob record) now exist. Record the handles on this execution.
|
|
418
|
+
if tracked_job_instance
|
|
419
|
+
update_columns(beanstalk_job_id: tracked_job_instance.reload.bkid)
|
|
420
|
+
elsif pending_activejob
|
|
421
|
+
link_activejob!(pending_activejob)
|
|
422
|
+
end
|
|
423
|
+
|
|
424
|
+
instrument_enqueue!
|
|
425
|
+
end
|
|
426
|
+
|
|
427
|
+
# Instrument the enqueue event, after the transaction has committed.
|
|
428
|
+
#
|
|
429
|
+
# @return [void]
|
|
430
|
+
#
|
|
431
|
+
# @api private
|
|
432
|
+
#
|
|
433
|
+
def instrument_enqueue!
|
|
434
|
+
ActiveSupport::Notifications.instrument('enqueue.schedule_execution.postburner', {
|
|
435
|
+
schedule: Postburner::Instrumentation.schedule_payload(schedule),
|
|
436
|
+
execution: Postburner::Instrumentation.execution_payload(self.reload),
|
|
437
|
+
beanstalk_job_id: beanstalk_job_id
|
|
438
|
+
})
|
|
439
|
+
end
|
|
440
|
+
|
|
308
441
|
# Normalize a snapshot/config hash through one JSON round-trip so two hashes
|
|
309
442
|
# that differ only representationally (symbol vs string keys, Time vs its
|
|
310
443
|
# ISO-8601 string) compare equal. Used by {#drifted?} on both operands.
|
|
@@ -378,11 +511,12 @@ module Postburner
|
|
|
378
511
|
# Creates and enqueues an ActiveJob instance with the scheduled run_at time.
|
|
379
512
|
# Handles both Hash args (passed as keyword arguments) and Array args (splatted).
|
|
380
513
|
#
|
|
381
|
-
# IMPORTANT: the Postburner
|
|
382
|
-
# enqueue_after_transaction_commit
|
|
383
|
-
# until the surrounding transaction commits. This
|
|
384
|
-
# *registers* the enqueue; the resulting
|
|
385
|
-
# ActiveJobs, the TrackedJob record)
|
|
514
|
+
# IMPORTANT: selecting the Postburner adapter sets
|
|
515
|
+
# ActiveJob::Base.enqueue_after_transaction_commit, so Rails defers the
|
|
516
|
+
# actual Beanstalkd put until the surrounding transaction commits. This
|
|
517
|
+
# method therefore only *registers* the enqueue; the resulting
|
|
518
|
+
# beanstalk_job_id (and, for tracked ActiveJobs, the TrackedJob record)
|
|
519
|
+
# are not available until after commit.
|
|
386
520
|
# Linking is the caller's responsibility via {#link_activejob!}.
|
|
387
521
|
#
|
|
388
522
|
# @return [ActiveJob::Base] the enqueued job, to be linked after commit
|
|
@@ -454,6 +588,23 @@ module Postburner
|
|
|
454
588
|
end
|
|
455
589
|
end
|
|
456
590
|
|
|
591
|
+
# Link a tracked ActiveJob's row after its put raised.
|
|
592
|
+
#
|
|
593
|
+
# Errors here are logged, not raised, so the caller re-raises the put error.
|
|
594
|
+
#
|
|
595
|
+
# @param job [ActiveJob::Base]
|
|
596
|
+
# @return [void]
|
|
597
|
+
#
|
|
598
|
+
# @api private
|
|
599
|
+
#
|
|
600
|
+
def link_failed_activejob!(job)
|
|
601
|
+
return unless tracked_activejob?
|
|
602
|
+
|
|
603
|
+
link_activejob!(job)
|
|
604
|
+
rescue StandardError => e
|
|
605
|
+
Rails.logger.error "[Postburner::ScheduleExecution] Could not link tracked job for execution #{id} after a failed put: #{e.class} - #{e.message}"
|
|
606
|
+
end
|
|
607
|
+
|
|
457
608
|
# Remove the work this execution scheduled: its Beanstalkd job AND, for the
|
|
458
609
|
# Postburner::Job / tracked ActiveJob shapes, the Postburner::Job AR row.
|
|
459
610
|
#
|
|
@@ -37,7 +37,7 @@ class CreatePostburnerJobs < ActiveRecord::Migration<%= migration_version %>
|
|
|
37
37
|
t.index [ :error_count ]
|
|
38
38
|
t.index [ :log_count ]
|
|
39
39
|
|
|
40
|
-
# Partial indexes tuned to the
|
|
40
|
+
# Partial indexes tuned to the common job-state queries. Kept
|
|
41
41
|
# alongside the single-column indexes above: the single-column
|
|
42
42
|
# indexes still serve ad-hoc queries, while these partials keep
|
|
43
43
|
# the hot-path listing queries fast on large tables.
|