postburner 1.0.0.rc.8 → 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 +33 -0
- data/README.md +80 -23
- data/app/models/postburner/schedule_execution.rb +188 -40
- data/lib/generators/postburner/install/templates/migrations/create_postburner_jobs.rb.erb +1 -1
- data/lib/postburner/active_job/adapter.rb +48 -0
- data/lib/postburner/engine.rb +0 -9
- 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/tube.rb +2 -2
- data/lib/postburner/version.rb +1 -1
- data/lib/postburner.rb +1 -0
- 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
|
@@ -2,6 +2,39 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
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
|
+
|
|
5
38
|
## v1.0.0.rc.8 - 2026-10-05
|
|
6
39
|
|
|
7
40
|
### 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)
|
|
@@ -1625,7 +1627,7 @@ Postburner emits ActiveSupport::Notifications events following Rails conventions
|
|
|
1625
1627
|
|-------|------|--------------|
|
|
1626
1628
|
| `create.schedule.postburner` | When schedule is created | `:schedule` |
|
|
1627
1629
|
| `update.schedule.postburner` | When schedule is updated | `:schedule`, `:changes` |
|
|
1628
|
-
| `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` |
|
|
1629
1631
|
| `reconcile.schedule.postburner` | When a schedule is reconciled (executions converged to config) | `:schedule`, `:superseded`, `:created` |
|
|
1630
1632
|
|
|
1631
1633
|
**Schedule Payload Structure:**
|
|
@@ -1797,6 +1799,82 @@ end
|
|
|
1797
1799
|
|
|
1798
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.
|
|
1799
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
|
+
|
|
1800
1878
|
## Why Beanstalkd?
|
|
1801
1879
|
|
|
1802
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.
|
|
@@ -2322,27 +2400,6 @@ Postburner.connected do |conn|
|
|
|
2322
2400
|
end
|
|
2323
2401
|
```
|
|
2324
2402
|
|
|
2325
|
-
## Web UI - v2 Coming Soon
|
|
2326
|
-
|
|
2327
|
-
Mount the inspection interface:
|
|
2328
|
-
|
|
2329
|
-
```ruby
|
|
2330
|
-
# config/routes.rb
|
|
2331
|
-
mount Postburner::Engine => "/postburner"
|
|
2332
|
-
|
|
2333
|
-
# Only in development
|
|
2334
|
-
mount Postburner::Engine => "/postburner" if Rails.env.development?
|
|
2335
|
-
```
|
|
2336
|
-
|
|
2337
|
-
Add your own authentication:
|
|
2338
|
-
|
|
2339
|
-
```ruby
|
|
2340
|
-
# config/routes.rb
|
|
2341
|
-
authenticate :user, ->(user) { user.admin? } do
|
|
2342
|
-
mount Postburner::Engine => "/postburner"
|
|
2343
|
-
end
|
|
2344
|
-
```
|
|
2345
|
-
|
|
2346
2403
|
## Installation
|
|
2347
2404
|
|
|
2348
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,50 +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
|
-
# - selecting the Postburner adapter sets
|
|
148
|
-
# ActiveJob::Base.enqueue_after_transaction_commit, so Rails holds
|
|
149
|
-
# the adapter enqueue until this transaction commits.
|
|
150
|
-
# So the real Beanstalkd id (and, for tracked ActiveJobs, the TrackedJob
|
|
151
|
-
# row) only exists AFTER the transaction below. We capture the job here and
|
|
152
|
-
# link/record the Beanstalkd id afterward.
|
|
153
|
-
tracked_job_instance = nil
|
|
154
|
-
pending_activejob = nil
|
|
155
|
-
transaction do
|
|
156
|
-
update_columns(
|
|
157
|
-
enqueued_at: Time.current,
|
|
158
|
-
status: self.class.statuses[:scheduled]
|
|
159
|
-
)
|
|
173
|
+
put!
|
|
174
|
+
end
|
|
160
175
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
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?
|
|
167
186
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
if tracked_job_instance
|
|
171
|
-
update_columns(beanstalk_job_id: tracked_job_instance.reload.bkid)
|
|
172
|
-
elsif pending_activejob
|
|
173
|
-
link_activejob!(pending_activejob)
|
|
174
|
-
end
|
|
187
|
+
bkid = job&.bkid
|
|
188
|
+
return false if bkid.blank?
|
|
175
189
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
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
|
|
182
209
|
end
|
|
183
210
|
|
|
184
211
|
# Skip this execution ("skip this one occurrence, keep the schedule running").
|
|
@@ -307,6 +334,110 @@ module Postburner
|
|
|
307
334
|
|
|
308
335
|
private
|
|
309
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
|
+
|
|
310
441
|
# Normalize a snapshot/config hash through one JSON round-trip so two hashes
|
|
311
442
|
# that differ only representationally (symbol vs string keys, Time vs its
|
|
312
443
|
# ISO-8601 string) compare equal. Used by {#drifted?} on both operands.
|
|
@@ -457,6 +588,23 @@ module Postburner
|
|
|
457
588
|
end
|
|
458
589
|
end
|
|
459
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
|
+
|
|
460
608
|
# Remove the work this execution scheduled: its Beanstalkd job AND, for the
|
|
461
609
|
# Postburner::Job / tracked ActiveJob shapes, the Postburner::Job AR row.
|
|
462
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.
|
|
@@ -100,8 +100,44 @@ module ActiveJob
|
|
|
100
100
|
true
|
|
101
101
|
end
|
|
102
102
|
|
|
103
|
+
# Puts an existing tracked row to Beanstalkd again.
|
|
104
|
+
#
|
|
105
|
+
# Used to retry a put that failed after the row was committed, without
|
|
106
|
+
# creating a second {Postburner::TrackedJob}. The ActiveJob is rebuilt from
|
|
107
|
+
# the row's args, so the tube, priority, and TTR match the first put.
|
|
108
|
+
#
|
|
109
|
+
# @param tracked_job [Postburner::TrackedJob] row whose put failed
|
|
110
|
+
# @param timestamp [Numeric, nil] when to run, as a unix timestamp; a past
|
|
111
|
+
# time is put with no delay
|
|
112
|
+
#
|
|
113
|
+
# @return [void]
|
|
114
|
+
#
|
|
115
|
+
def requeue_tracked(tracked_job, timestamp)
|
|
116
|
+
put_tracked(rebuild_tracked(tracked_job), tracked_job, timestamp)
|
|
117
|
+
end
|
|
118
|
+
|
|
103
119
|
private
|
|
104
120
|
|
|
121
|
+
# Rebuilds the ActiveJob a {Postburner::TrackedJob} row was created from.
|
|
122
|
+
#
|
|
123
|
+
# @param tracked_job [Postburner::TrackedJob]
|
|
124
|
+
# @return [ActiveJob::Base]
|
|
125
|
+
#
|
|
126
|
+
def rebuild_tracked(tracked_job)
|
|
127
|
+
data = tracked_job.args
|
|
128
|
+
job = data['job_class'].constantize.new(
|
|
129
|
+
*::ActiveJob::Arguments.deserialize(data['arguments'] || [])
|
|
130
|
+
)
|
|
131
|
+
job.job_id = data['job_id']
|
|
132
|
+
job.queue_name = data['queue_name']
|
|
133
|
+
job.priority = data['priority']
|
|
134
|
+
job.executions = data['executions'] || 0
|
|
135
|
+
job.exception_executions = data['exception_executions'] || {}
|
|
136
|
+
job.locale = data['locale'] if data['locale']
|
|
137
|
+
job.timezone = data['timezone'] if data['timezone']
|
|
138
|
+
job
|
|
139
|
+
end
|
|
140
|
+
|
|
105
141
|
# Checks if a job should be tracked in PostgreSQL.
|
|
106
142
|
#
|
|
107
143
|
# Detects tracking by checking if the job class includes the
|
|
@@ -198,7 +234,19 @@ module ActiveJob
|
|
|
198
234
|
#
|
|
199
235
|
def enqueue_tracked(job, timestamp)
|
|
200
236
|
tracked_job = create_tracked_record(job, timestamp)
|
|
237
|
+
put_tracked(job, tracked_job, timestamp)
|
|
238
|
+
end
|
|
201
239
|
|
|
240
|
+
# Puts a committed tracked row's minimal payload to Beanstalkd and
|
|
241
|
+
# records the Beanstalkd id on the row.
|
|
242
|
+
#
|
|
243
|
+
# @param job [ActiveJob::Base] The job instance
|
|
244
|
+
# @param tracked_job [Postburner::TrackedJob] its row
|
|
245
|
+
# @param timestamp [Numeric, nil] When to execute the job
|
|
246
|
+
#
|
|
247
|
+
# @return [void]
|
|
248
|
+
#
|
|
249
|
+
def put_tracked(job, tracked_job, timestamp)
|
|
202
250
|
# Calculate delay for Beanstalkd
|
|
203
251
|
# ceil so the job is never released before its timestamp; see
|
|
204
252
|
# Postburner::Insertion#queue!
|
data/lib/postburner/engine.rb
CHANGED
|
@@ -1,18 +1,9 @@
|
|
|
1
1
|
require 'beaneater'
|
|
2
|
-
require 'haml-rails'
|
|
3
2
|
|
|
4
3
|
module Postburner
|
|
5
4
|
class Engine < ::Rails::Engine
|
|
6
5
|
isolate_namespace Postburner
|
|
7
6
|
|
|
8
|
-
config.generators do |g|
|
|
9
|
-
g.template_engine :haml
|
|
10
|
-
g.helper false
|
|
11
|
-
g.fixture false
|
|
12
|
-
g.assets false
|
|
13
|
-
g.skip_routes true
|
|
14
|
-
end
|
|
15
|
-
|
|
16
7
|
# Choose the test strategy once boot has finished applying configuration.
|
|
17
8
|
# after_initialize runs after config/initializers and before a test helper
|
|
18
9
|
# continues. The configured adapter is already a name here, so this does
|
data/lib/postburner/scheduler.rb
CHANGED
|
@@ -439,7 +439,8 @@ module Postburner
|
|
|
439
439
|
#
|
|
440
440
|
# 1. Delegates to reconcile! (with lock: false — see below).
|
|
441
441
|
# 2. Enqueues any orphaned pending executions that somehow weren't queued.
|
|
442
|
-
# 3.
|
|
442
|
+
# 3. Retries scheduled executions whose put failed (#heal_unconfirmed_puts).
|
|
443
|
+
# 4. Updates last_audit_at for observability.
|
|
443
444
|
#
|
|
444
445
|
# LOCKING: #perform already holds SCHEDULER_LOCK_KEY (blocking: false) on this
|
|
445
446
|
# AR connection for the duration of process_all_schedules. PostgreSQL advisory
|
|
@@ -481,6 +482,8 @@ module Postburner
|
|
|
481
482
|
end
|
|
482
483
|
end
|
|
483
484
|
|
|
485
|
+
put_counts = heal_unconfirmed_puts(schedule)
|
|
486
|
+
|
|
484
487
|
# Update last_audit_at
|
|
485
488
|
schedule.update_column(:last_audit_at, Time.current)
|
|
486
489
|
|
|
@@ -488,12 +491,66 @@ module Postburner
|
|
|
488
491
|
ActiveSupport::Notifications.instrument('audit.schedule.postburner', {
|
|
489
492
|
schedule: Postburner::Instrumentation.schedule_payload(schedule),
|
|
490
493
|
execution_created: created.present?,
|
|
491
|
-
orphans_enqueued: orphans_count
|
|
494
|
+
orphans_enqueued: orphans_count,
|
|
495
|
+
puts_linked: put_counts[:linked],
|
|
496
|
+
puts_retried: put_counts[:retried],
|
|
497
|
+
missed_superseded: put_counts[:superseded]
|
|
492
498
|
})
|
|
493
499
|
|
|
494
500
|
logger.debug "[Postburner::Scheduler] Schedule '#{schedule.name}': enqueued #{orphans_count} orphaned executions" if orphans_count > 0
|
|
495
501
|
end
|
|
496
502
|
|
|
503
|
+
# Retry scheduled executions whose put failed or was never linked.
|
|
504
|
+
#
|
|
505
|
+
# ScheduleExecution#enqueue! commits `scheduled` before the put, so a failed
|
|
506
|
+
# put leaves a scheduled row with no beanstalk_job_id. The due sweep only sees
|
|
507
|
+
# pending rows, and reconcile! counts a future one as the stable live future,
|
|
508
|
+
# so without this sweep the occurrence never runs.
|
|
509
|
+
#
|
|
510
|
+
# Runs after reconcile!, so a row reconcile superseded is not touched. For
|
|
511
|
+
# each row older than ScheduleExecution::PUT_GRACE:
|
|
512
|
+
#
|
|
513
|
+
# 1. If its job already has a Beanstalkd id, link it. The put succeeded.
|
|
514
|
+
# 2. If run_at has passed and the schedule has `catch_up: false`, supersede
|
|
515
|
+
# it. It is a missed occurrence, and reconcile! has already created the
|
|
516
|
+
# next one.
|
|
517
|
+
# 3. Otherwise retry the put via #enqueue!. A past run_at (catch_up: true)
|
|
518
|
+
# runs once with no delay; oldest first.
|
|
519
|
+
#
|
|
520
|
+
# Skipped when the queue strategy would not put the row (a testing strategy),
|
|
521
|
+
# where a missing Beanstalkd id is normal. A failed retry raises, so
|
|
522
|
+
# process_all_schedules counts the schedule as failed and the next watchdog
|
|
523
|
+
# pass tries again.
|
|
524
|
+
#
|
|
525
|
+
# @param schedule [Postburner::Schedule]
|
|
526
|
+
# @return [Hash] counts for :linked, :retried, :superseded
|
|
527
|
+
#
|
|
528
|
+
# @api private
|
|
529
|
+
#
|
|
530
|
+
def heal_unconfirmed_puts(schedule)
|
|
531
|
+
counts = { linked: 0, retried: 0, superseded: 0 }
|
|
532
|
+
|
|
533
|
+
schedule.executions.put_unconfirmed.order(:run_at).each do |execution|
|
|
534
|
+
next unless execution.puts_to_beanstalkd?
|
|
535
|
+
|
|
536
|
+
if execution.link_confirmed_put!
|
|
537
|
+
counts[:linked] += 1
|
|
538
|
+
elsif execution.run_at <= Time.current && !schedule.catch_up?
|
|
539
|
+
logger.warn "[Postburner::Scheduler] Superseding missed execution #{execution.id} for schedule '#{schedule.name}' (put never confirmed, run_at #{execution.run_at}, catch_up: false)"
|
|
540
|
+
counts[:superseded] += 1 if execution.supersede!
|
|
541
|
+
else
|
|
542
|
+
logger.warn "[Postburner::Scheduler] Retrying put for execution #{execution.id} for schedule '#{schedule.name}' (no Beanstalkd id since #{execution.enqueued_at})"
|
|
543
|
+
execution.enqueue!
|
|
544
|
+
counts[:retried] += 1
|
|
545
|
+
end
|
|
546
|
+
rescue => e
|
|
547
|
+
logger.error "[Postburner::Scheduler] Failed to heal execution #{execution.id}: #{e.class} - #{e.message}"
|
|
548
|
+
raise
|
|
549
|
+
end
|
|
550
|
+
|
|
551
|
+
counts
|
|
552
|
+
end
|
|
553
|
+
|
|
497
554
|
# Re-queue the watchdog for the next run (if not already queued).
|
|
498
555
|
#
|
|
499
556
|
# Called in the ensure block of perform to guarantee the watchdog
|