pgbus 0.20.0 → 0.21.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 (63) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +59 -6
  3. data/README.md +3 -2
  4. data/app/frontend/pgbus/modules/charts.js +18 -0
  5. data/app/frontend/pgbus/modules/charts.min.js +1 -1
  6. data/app/frontend/pgbus/modules/charts.min.js.map +1 -1
  7. data/app/helpers/pgbus/workflow_insights_helper.rb +8 -0
  8. data/app/helpers/pgbus/workflows_helper.rb +2 -2
  9. data/app/models/pgbus/blocked_execution.rb +12 -0
  10. data/app/models/pgbus/uniqueness_key.rb +88 -3
  11. data/app/models/pgbus/workflow_entry.rb +3 -2
  12. data/app/models/pgbus/workflow_step.rb +7 -0
  13. data/app/views/pgbus/workflows/_timeline.html.erb +11 -1
  14. data/app/views/pgbus/workflows/definitions/_step_card.html.erb +3 -0
  15. data/config/locales/da.yml +7 -1
  16. data/config/locales/de.yml +7 -1
  17. data/config/locales/en.yml +7 -1
  18. data/config/locales/es.yml +7 -1
  19. data/config/locales/fi.yml +7 -1
  20. data/config/locales/fr.yml +7 -1
  21. data/config/locales/it.yml +7 -1
  22. data/config/locales/ja.yml +7 -1
  23. data/config/locales/nb.yml +7 -1
  24. data/config/locales/nl.yml +7 -1
  25. data/config/locales/pt.yml +7 -1
  26. data/config/locales/sv.yml +7 -1
  27. data/lib/generators/pgbus/templates/add_workflows.rb.erb +3 -1
  28. data/lib/generators/pgbus/templates/migration.rb.erb +3 -1
  29. data/lib/pgbus/active_job/adapter.rb +81 -3
  30. data/lib/pgbus/active_job/delivery.rb +20 -6
  31. data/lib/pgbus/active_job/executor.rb +18 -8
  32. data/lib/pgbus/concurrency/blocked_execution.rb +14 -6
  33. data/lib/pgbus/configuration/workflow_settings.rb +1 -1
  34. data/lib/pgbus/configuration.rb +43 -1
  35. data/lib/pgbus/dead_letter_header.rb +9 -1
  36. data/lib/pgbus/instrumentation.rb +3 -2
  37. data/lib/pgbus/integrations/appsignal/dashboard.json +30 -1
  38. data/lib/pgbus/mcp/tools/workflow_stats_tool.rb +1 -1
  39. data/lib/pgbus/mcp/tools/workflows_attention_tool.rb +1 -1
  40. data/lib/pgbus/metrics/workflow_metrics.rb +11 -1
  41. data/lib/pgbus/process/consumer.rb +2 -1
  42. data/lib/pgbus/process/dispatcher.rb +13 -3
  43. data/lib/pgbus/process/worker.rb +10 -1
  44. data/lib/pgbus/recurring/schedule.rb +1 -1
  45. data/lib/pgbus/streams/presence.rb +65 -36
  46. data/lib/pgbus/uniqueness.rb +23 -6
  47. data/lib/pgbus/version.rb +1 -1
  48. data/lib/pgbus/web/data_source/workflow_list.rb +52 -7
  49. data/lib/pgbus/web/data_source.rb +123 -15
  50. data/lib/pgbus/web/workflow_insights_report.rb +11 -3
  51. data/lib/pgbus/web/workflow_state.rb +19 -3
  52. data/lib/pgbus/web/workflow_timeline.rb +23 -6
  53. data/lib/pgbus/workflow/instance.rb +36 -3
  54. data/lib/pgbus/workflow/runner.rb +2 -2
  55. data/lib/pgbus/workflow/signal_peek.rb +47 -0
  56. data/lib/pgbus/workflow/signals.rb +31 -10
  57. data/lib/pgbus/workflow/step_definition.rb +49 -5
  58. data/lib/pgbus/workflow/step_execution.rb +27 -10
  59. data/lib/pgbus/workflow/wait_timeout.rb +56 -4
  60. data/lib/pgbus/workflow.rb +11 -5
  61. data/sig/pgbus/configuration.rbs +1 -0
  62. data/sig/pgbus.rbs +43 -0
  63. metadata +2 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dd8fbf7676d82bc5e1648aba550d325d6d5e1ee30b2f87b483a123ac493506ec
4
- data.tar.gz: 47e9a9949d84ea0489c3d5aba6b47bb343c3895d8d8ce15255ec699ad203f752
3
+ metadata.gz: 1c3c6921f17cfc002f7177240a3d1b2c202a78c7127e8f0da75a75ebcaf365d1
4
+ data.tar.gz: b73396771876a230b1b62b6b30dceb87329be7259810c5346fd4a3fc78f59991
5
5
  SHA512:
6
- metadata.gz: 0dd9781845545f3203aac84f297c865604cc2b0560f6fdb2f630d99642e0b50f8a3a2a673d2d9dc91b5353b09b9b83ca073f7464a78e44cc3762b81a257ddd00
7
- data.tar.gz: 791c9590b9544a5cbbf89386da81ce4b5daaceccb0df08079d95960e7b434646c63335217fcdb9960bc59124320549b6f78a54ae57e30ed692bd0eaccd50cf62
6
+ metadata.gz: 5667ec2a6ebd1ef9e09f5f89818e69784267b3aa0c13a0b5cef2782b346af2361c106454fbb58d67a688b48f451483327fa4f3edc0a1d9a82664aa599af52454
7
+ data.tar.gz: 83d7a44001fe09db593caacc788307198361279c023b7ed2ed8397fdec17fb353fa44087d9a973857951835d6cb1206314ade872a99d85586988561be2477a85
data/CHANGELOG.md CHANGED
@@ -1,5 +1,35 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.21.0] - 2026-10-19
4
+
5
+ ### Added
6
+
7
+ - **feat(workflow): `wait_timeout:` takes a per-run deadline — a method name or a lambda, returning a duration or a Time (issue #574).** `step :await_reply, wait_for: :replied, wait_timeout: -> { subject.expires_at }, on_timeout: :run` (or `wait_timeout: :reply_deadline`, an instance method) is evaluated once per attempt, when the step parks, on the workflow instance (`instance_exec`, as `skip_if:` is, with `subject` and `arguments`). It returns a `Time` (or anything with `#to_time` that is not a String): the deadline itself, so `timeout_at` no longer drifts by the time the run took to reach the wait (the steps before it, queue latency, a reattempt); a deadline already past is `timeout_at = now`, and the next sweep applies `on_timeout`. Or a Duration or a number of seconds, counted from park and required to be positive. Or `nil`: no timeout for this attempt. Anything else raises `ArgumentError` at the gate, which, like an error the callable raises, goes through the step's exception policies (the default pauses the run), so the run is never left parked silently. The callable runs under the run's row lock in a savepoint, so a query of its own that fails in the database (a dropped column, a `statement_timeout`) is handled the same way instead of aborting the run's transaction. `on_timeout:` still needs `wait_timeout:`, and a callable counts as given (its default is `:pause!`). Under `on_timeout: :notify` the callable is evaluated again at each notice: a `Time` after now is the next deadline, a duration moves the deadline one period on as a static `wait_timeout:` does, and `nil` or a `Time` not after now (the same `expires_at`) clears `timeout_at`, so the wait is reported once and keeps waiting with no deadline instead of re-firing on a past time (the Workflows list and the run's page still say since when it is late, and that no further late notice is due); one that raises or returns anything else at a notice pauses the run with that error (the sweep holds the run's row lock, under which no `on_exception` block may run). A pause still moves the deadline by its length, as for a static one. `StepDefinition#wait_timeout` stays the static seconds (nil for a callable); `#wait_timeout_callable` and `#dynamic_wait_timeout?` read the per-run one. The definitions page (`/pgbus/workflows/definitions/:workflow_class`) shows each signal step's wait timeout, `dynamic (per run)` for a callable, and `pgbus_workflow_stats` sends it per step as `wait_timeout` (seconds or `"dynamic"`). A static `wait_timeout:` (seconds or a Duration) is unchanged, validated when the class loads and counted from park. Refs #574.
8
+ - **feat(workflow): `woken?` and `signal_ready?` — a `wait_for:` step's guards can tell a signalled step from one that has not waited yet (issue #572).** Guards run before the signal gate, on every claim of the step, so a `skip_if:` on the state the signal reports (skip a wait the record already passed) recorded a step `signal!` woke as `skipped` and never ran its body, and a signal sent before the step's first claim stayed `pending` for good because the gate never ran. Two step readers, usable in `skip_if:`, `cancel_if` and the body, answer it without claiming anything (the gate still claims, after the guards): `signal_ready?` is true when the gate runs this attempt with a signal, the one the step holds or a pending one its `wait_for:` matcher takes; `woken?` is true when this attempt holds a signal before its gate (`signal!` woke it, or an earlier attempt took one and reattempted or failed into a pause), false on a first claim even with a buffered signal. Both keep one answer for the whole attempt, are false after an `on_timeout: :run` wake and in a step without `wait_for:`, and raise `Pgbus::Workflow::NotInStep` outside a step. The recipe: `step :await_shipped, wait_for: :shipped, skip_if: -> { subject.shipped? && !signal_ready? } do … end` skips a run that started after the order shipped and completes a woken step or one whose signal came first, consuming the signal. `skip_if` still runs on every claim, and no step outcome changes for an existing class. A guard that skips takes no signal: a pending one stays pending for a later `wait_for:` step, as with an outside `skip!`. Refs #572.
9
+ - **feat(workflow): `start!` takes `actor:` and `reason:` for the audit trail (issue #571).** `Pgbus::Workflow.start!(subject = nil, idempotency_key: nil, actor: nil, reason: nil, **arguments)`: the run's `started` event records the actor and the reason, and its first `step_scheduled` event the actor, as `rerun!` and every outside verb already did, so a run started by a person says who started it and why (`ReportWorkflow.start!(actor: "alice", reason: "monthly close")`). Without them the trail says `"app"` with no reason, as before; the default is `actor || "app"`, the outside verbs' convention, so `actor: ""` is kept as given. Neither is stored in the run's `arguments` or changes the run row. A duplicate start (same `idempotency_key`, `duplicate_start?` true) still writes no events, so the first caller's actor stays on the trail. Refs #571.
10
+
11
+ ### Changed
12
+
13
+ - **`actor:` and `reason:` are reserved keywords of `Pgbus::Workflow.start!` (issue #571).** Like `idempotency_key:`, they are taken for the audit trail and no longer reach the run's `arguments`. A workflow that receives an argument literally named `actor` or `reason` through `start!` renames it, e.g. `start!(subject, requested_by: "alice")` read as `arguments[:requested_by]`. Runs already started keep their stored arguments, and `rerun!` copies them as stored. Refs #571.
14
+
15
+ ### Fixed
16
+
17
+ - **The workflows one-ongoing index predicate dumps the same `schema.rb` line from a migrated and from a schema-loaded database (issue #573).** `pgbus:add_workflows` and the fresh-install migration declared `idx_pgbus_workflows_one_ongoing` with `where: "status IN ('ready','performing','waiting','paused')"`. `status` is a `varchar`, so Postgres stored the list as `varchar` literals cast to `text[]`; `db:schema:load` parses that dumped string into a different expression, and each later dump flipped the line depending on which kind of database it came from. Both templates now write `status::text IN (…)`, whose stored form replays to itself. The index means the same either way, so existing installs need nothing. An app that wants a stable dump can recreate the index in a migration of its own: `remove_index :pgbus_workflows, name: "idx_pgbus_workflows_one_ongoing"`, then `add_index :pgbus_workflows, [:workflow_class, :subject_type, :subject_id], unique: true, where: "status::text IN ('ready','performing','waiting','paused')", name: "idx_pgbus_workflows_one_ongoing"` (both run in the migration's transaction, so the constraint never lapses; `remove_index` takes an ACCESS EXCLUSIVE lock, so `pgbus_workflows` is locked against reads and writes (workers, workflow starts and the dashboard wait) until the migration commits), and dump the schema again. Refs #573.
18
+ - **A guard that ends a `wait_for:` step after a reattempt consumes the signal the step holds (issue #572).** A woken attempt that called `reattempt!` (or failed into a pause and was resumed) kept its signal `claimed` for the next attempt, whose row carries no `signal_id` until its gate runs. When `cancel_if` or `skip_if` ended that next attempt (or a guard raised and its `on_exception` policy skipped or cancelled, or a deploy had declared the step a `removed_step` meanwhile), the signal stayed `claimed` forever. The attempt now records the held signal before its outcome, so it is consumed when the step ends and kept when it reattempts or pauses, as for any attempt. Refs #572.
19
+
20
+ ## [0.20.1] - 2026-10-18
21
+
22
+ ### Added
23
+
24
+ - **feat(workflow): `on_timeout: :notify` — a soft wait deadline that reports a late signal and keeps waiting.** A fifth `on_timeout:` value for a `wait_for:` step with `wait_timeout:`. When the sweep (or `expire_wait`) finds the wait past `timeout_at`, it records the `wait_timed_out` event and instruments `pgbus.workflow.wait_timed_out` with `action: "notify"`, as the other values do, and then leaves the run and the step as they are: same attempt, still `waiting`, no step outcome, no StepJob. Only `timeout_at` moves, to the next deadline on the `wait_timeout:` grid (one `wait_timeout:` on; a sweep that missed several deadlines reports them once and moves to the first one ahead of now), so a late signal still wakes that attempt and its body runs with `received_signal` set and `timed_out?` false. A paused run's wait still does not fire while paused. Replaces the `on_timeout: :run` + `reattempt!` workaround, whose `reattempted` outcomes counted as failures in `pgbus_workflow_steps_total`. New counter `pgbus_workflow_wait_overdue_total{workflow_class, step}` (Prometheus, StatsD, AppSignal), one per late notice (a sweep that missed several deadlines sends one). The dashboard says "Waiting for signal `approved` since 3h ago (late since 2h ago). The next late notice goes out in 1h" once a deadline passed (and "A late notice goes out …" before), and the timeline marks every `wait_timed_out` with a dashed line, listed as text below the chart; the workflow rows (and so the `pgbus_workflows` / `pgbus_workflow` MCP tools) gain `on_timeout` and `late_since` (the first missed deadline). The bundled AppSignal dashboard gets a "Late workflow waits" graph. Refs #565.
25
+
26
+ ### Fixed
27
+
28
+ - **An `:until_executed` job keeps its uniqueness key while its retry waits (issue #564).** A job that rescheduled itself with `retry_job(wait:)` (or `retry_on … wait:`) enqueued the retry through the key it held (#333), but the key row kept pointing at the running attempt, and the executor deleted the row by key when that attempt archived. For the whole wait nothing held the key: a fresh `perform_later` with the same key ran beside the retry, and the retry's own success then deleted the newcomer's row. Now the row is handed to the retry the way a first enqueue takes one: before the retry is sent or parked it is unbound onto the retry's queue and re-stamped (`UniquenessKey.unbind!`), and once sent it is bound to the retry (`UniquenessKey.bind!(…, acquired_at:)`). A retry parked behind its `limits_concurrency` slot (with `to: 1, on_conflict: :block` the attempt holds that slot, so its retry always parks) keeps the unbound row until it runs; a retry dropped by its limit, or whose send failed before any message existed, hands the row back to the attempt. A finishing message (success, dead-letter, or a failed `:while_executing` attempt) releases only the row it holds (`UniquenessKey.release_held_by!`): the row bound to it, or an unbound row acquired no later than the message was sent, so a retry that finishes before its bind still releases it; a row handed to a retry, or that a successor took after an operator released it, stays. The dashboard's discards (a job, a failed job, a dead-lettered copy, an event, the bulk discards and queue purges) release the same way, through `UniquenessKey.release_all_held_by!` for the bulk ones, judged by when the job's message was sent: a failed job's queued message gives its own `enqueued_at`, and the dead-letter header gains `source_msg_id` and `source_enqueued_at` so a DLQ copy names its original and when it was sent. Without a send time only a lock bound to the message is freed (the reaper takes the rest). The Locks page's Release still deletes by key. A retry enqueued outside the executor, or by a job running inline inside another job, moves nothing; one enqueued by an attempt that no longer holds the key moves nothing and logs a warning. The dispatcher's reaper now counts a key carried by a job parked in `pgbus_blocked_executions` (legacy double-encoded rows included) as live: a parked `:until_executed` job's unbound row was reaped once it was older than twice the visibility timeout. A parked job's `created_at` now comes from the database clock, like its lock's, so discarding it from the dashboard frees its lock even when the app's clock runs behind the database's. A job's lock now records its queue the way the queue's table spells it (`Uniqueness.lock_queue_name`: `orders-handler` → `orders_handler`), at every place a job lock is written or released (adapter, executor, recurring scheduler), so a release that knows only the table (the dashboard) or a worker that spells the queue the table's way still finds it; a multi-queue worker also hands the executor the configured queue name instead of one cut from the table name. For such a queue, a job in flight during the upgrade holds its lock under the old spelling: its first retry after the upgrade does not take the lock (logged), so it and that job's later retries run without one until the job finishes, and the stale lock is reclaimed by the reaper once its message is gone. `config.queue_prefix` is now stored the way the queue tables spell it (`"my-app"` reads back as `"my_app"`): table names are unchanged, and every place that strips the prefix off a table name (the dashboard, the worker, the lock queue above) strips it whole. A prefix with a leading, trailing or doubled underscore (`"myapp_"`) is kept as written and now warned about at boot: `queue_name` collapses the separator after it only for a queue name that needs normalizing (`myapp__orders`, but `myapp_orders_handler` for `orders-handler`), so the reaper cannot resolve such a queue's lock back to its table: a lock orphaned there (a worker crash before its release) is never reclaimed and has to be released by key. A job's own release and the dashboard still match. `Pgbus::ActiveJob::Delivery.current` gains `enqueued_at` and `job_id`. Refs #564.
29
+ - **Presence statements run under the ActiveRecord adapter's lock (issue #566).** `Pgbus::Streams::Presence` ran every statement on `connection.raw_connection`, and `raw_connection` holds the adapter's lock only long enough to hand the `PG::Connection` out. On a pinned pool, as in transactional tests, every thread shares one adapter and that lock is all that serialises them, so the streamer's seat sweep (`expire_seats!` on the dispatcher thread) could be in flight on the same libpq connection a request thread was about to use. Every presence statement (`join`, `leave`, `touch`, `set_state`, `members`, `count`, `sweep!`, `expire_seats!` and the seat-schema probe) now goes through `Presence.exec_params`, which holds `adapter.lock` for the whole statement. The adapter lock is reentrant, so a request already inside a transaction re-enters it; unpinned, it is a `NullLock` and nothing changes. The seat-schema probe also no longer runs while holding the probe's own mutex: a sweep that probed under it while a pinned request inside `ActiveRecord::Base.transaction` (which holds the adapter's lock for its whole block) asked `seats?` deadlocked both threads. Two threads may now probe at once: a true answer one of them stored is never taken back, and a probe that a `reset_schema_cache!` overtook stores nothing. `Presence.connection` still returns the raw `PG::Connection`.
30
+
31
+ ## [0.20.0] - 2026-10-18
32
+
3
33
  ### Added
4
34
 
5
35
  - **feat(mcp): workflow write tools, opt-in — resume, cancel, skip step, resume at, run now, signal (issue #550).** `config.mcp_write_tools = true` (default `false`, validated as a boolean, read when `Pgbus::MCP::Server.build` runs) adds `pgbus_workflow_resume`, `pgbus_workflow_cancel`, `pgbus_workflow_skip_step`, `pgbus_workflow_resume_at` (`step`), `pgbus_workflow_run_now` and `pgbus_workflow_signal` (`name`, a JSON object `payload` capped at `workflow_max_payload_size`, an optional `idempotency_key`; name and key at most 255 characters) to the MCP server; off, they are never constructed and `tools/list` does not show them, and the read tools are the same either way. Each takes `workflow_id`, a required `reason` (1–500 characters after trimming) and an optional `expected_status` (a run that has moved on by the time the call starts answers `ok: false` and nothing is done; the check runs just before the action, whose transition is guarded again under the run's row lock); a verb the run's `available_actions` does not allow answers `ok: false` with the allowed list; otherwise the call goes through the dashboard's `Web::DataSource` action, is recorded on the run's audit trail with its reason as actor `mcp` (`mcp:<client name>` when the transport knows the client; the name is stripped of control characters and capped at 100), and answers `{ ok:, reason:, workflow: }` with the `pgbus_workflow` detail as the run is after the call (refusals included), payloads always redacted, plus `available_actions` for a disallowed verb and `code`/`status` when the guarded transition refused it; an unexpected failure answers a fixed reason rather than the exception's text (the DataSource logged it). The tools live on their own base class, `Pgbus::MCP::WriteTool` (not `BaseTool`), annotated `readOnlyHint: false, destructiveHint: true` (`idempotentHint: true` on cancel only: a retried skip or resume at can act again once the run parked or paused again). No bulk tools: `pgbus_workflows_attention` groups the stuck runs and `pgbus_workflows` lists a group's runs, one call per run. The server's instructions name them when they are on, `pgbus mcp` logs it at start, and an unauthenticated `Pgbus::MCP.rack_app` with them on warns that anyone who reaches it can change workflow runs. `Workflow#signal!`, `Workflow.signal!` (by subject), `Signals.deliver` and `DataSource#signal_workflow` take an optional `reason:`, recorded on the `signal_received` event. Refs #550.
@@ -7,8 +37,32 @@
7
37
  - **feat(dashboard): the Workflows pages — list and filters, needs attention, step timeline, audit trail, operator actions, MCP read tools (issue #542).** `/pgbus/workflows` lists every run newest first (ongoing by default, each state, or all) with its class, subject, status badge, current step, start and last activity, and says in words what it is doing and what happens next ("Waiting for batch `OrderWorkflow#notify_vendors` (7 of 12 jobs done)", "Paused on `capture`: Timeout::Error — resume or cancel", "Overdue: `welcome` was due 2h ago; the sweep sends it again"); filters on class, current step, error class, subject type and id, workflow id and a created-at range combine and survive paging. `/pgbus/workflows/:workflow_id` shows the arguments, an ApexCharts range-bar timeline (one row per attempt split into waited, queued and ran, coloured by outcome, with waits and pauses as their own segments, zoomable), every attempt with the same durations as text, its worker, app revision, error, backtrace and cause chain, the signals, the audit trail ("Resumed by dashboard:alice: SMTP fixed"), whether it started on an older definition, rerun links both ways, and the actions `available_actions` allows: Resume (labelled "retry `capture`" or "continue with `receipt`"), Resume at a chosen step, Run now, Skip step, Send signal (a JSON object, sent once per form render), Rerun and Cancel, each with a confirm and an optional reason, each reporting the real outcome ("Not done: the run is cancelled now."). `/pgbus/workflows/attention` groups every stuck run by class, step, reason and error class with a count, first/last seen and the newest message, with bulk Resume/Cancel (500 runs per press, each through its own guarded transition: "Resumed 212; 2 had already moved on"). The home page gains a Workflows card ("N ongoing (M paused) · K need attention") and the nav a Workflows link. New settings: `config.web_actor` (`->(request) { name }`, recorded as `"dashboard:<name>"`, or `"dashboard"`) and `config.workflow_subject_url` (`->(subject_type, subject_id) { url }`, linked only when it is an http(s) URL or a same-site path). `Web::DataSource` gains `workflows`, `workflows_count`, `workflow_detail`, `workflow_attention_groups`, the home counts and the actions; `Pgbus::WorkflowEntry.attention_groups` / `attention_ids` share the stuck-reason `CASE` with the gauge. The MCP server gains the read-only `pgbus_workflows`, `pgbus_workflow` and `pgbus_workflows_attention`, with arguments, outputs, signal payloads and error text behind `include_payloads`. Refs #542.
8
38
  - **feat(workflows): waiting steps — `fan_out` over a batch, signals, wait timeouts (issue #541).** `fan_out :notify_vendors do |b| b.add(*jobs) end` runs its body with a recording builder, then opens one batch (`Pgbus.batch`, described `Class#step workflow_id`) whose `on_complete` is `Pgbus::Workflow::BatchFinishedJob`; the step and the run park as `waiting` and the callback resumes the run once every member finished: the step completes and the next is scheduled, or, with a dead-lettered member, `on_batch_failure:` (`:pause!` default, `:cancel!`, `:skip!`) applies with `Pgbus::Workflow::FanOutFailed` ("1 of 3 jobs dead-lettered") recorded. A verb or an exception in the body ends the step before any batch exists, the body cannot set the batch's callbacks, an empty batch resolves at once, and a reattempt or `resume!` re-runs the whole body on a fresh batch (members must be idempotent). `step :capture, wait_for: :payment_confirmed` parks the step at its gate until `wf.signal!(:payment_confirmed, payload: {…}, idempotency_key: event.id, actor:)` arrives, or `OrderWorkflow.signal!(order, :payment_confirmed, …, missing: :raise | :ignore)` by subject; signals live in `pgbus_workflow_signals` (`Pgbus::WorkflowSignal`, `pending → claimed → consumed`), early ones are buffered, a redelivery with the same key stores nothing and answers `duplicate_delivery?`, `wait_for:` takes a name, a `Pgbus::Workflow::SignalMatcher.new(:shipped) { |payload| … }` or any `matches?(signal)` object, the body reads `received_signal.payload` with indifferent access, and a claimed signal survives a `reattempt!` or a failure that pauses. The wake is a step-row write, so its StepJob goes out only after the signal commits; a paused run takes the signal but stays paused until `resume!`, and `resume!` of a run paused while waiting puts it back to `waiting`. `wait_timeout: 2.days, on_timeout: :pause! | :cancel! | :skip! | :run` bounds a wait (`timeout:` still bounds the run): the dispatcher's sweep applies it under the row lock (`Pgbus::Workflow::WaitTimedOut` for `:pause!`; `:run` runs the body with `timed_out?` true), a late signal stays pending, and the time a run spends paused does not count against the wait. The sweep also resolves a fan_out step whose batch finished but whose callback was lost, and pauses one whose batch row is gone (outcome unknown, whatever `on_batch_failure:` says). `skip!` outside a step skips the step a waiting run waits on. New runner verbs `park`, `wake`, `resolve`, `rewait` and outside verbs `skip`, `signal` in `States`; events `step_waiting`, `signal_received`, `signal_claimed`, `wait_timed_out`, `fan_out_opened`, `fan_out_finished`; instrumentation `pgbus.workflow.signal` and `pgbus.workflow.wait_timed_out`; stuck reason `waiting_overdue` past the new `workflow_waiting_visibility_threshold` (7 days). `TestHelpers` gains `deliver_signal`, `complete_fan_out` and `expire_wait`, and `speedrun_workflow` raises `Pgbus::Workflow::StepWaiting` naming what the step waits for. Schema: `pgbus:add_workflows` and `pgbus:install` carry the five step columns and the signals table; installs that ran the core migration add them with `rails generate pgbus:add_workflow_signals [--database=pgbus]` (`pgbus:update` detects it), and `start!` raises `WaitingStepsUnmigrated` (or `BatchCallbacksUnmigrated` for a `fan_out` before `pgbus:add_batch_callback_jobs`) until then. Refs #541.
9
39
  - **feat(workflows): `Pgbus::Workflow`, durable multi-step workflows.** A workflow class declares steps (`step :name, wait:, skip_if:, timeout:, job_options:, on_exception:`, anonymous `step { … }`, `step :method`, `step def …`, `before_step:`/`after_step:`), `cancel_if`, class-level `on_exception` policies and `set_step_job_options`; `SignupWorkflow.start!(user, plan: "pro")` writes the run and its first step row in one transaction, and each step attempt is a `pgbus_workflow_steps` row and one `Pgbus::Workflow::StepJob`, sent only by the step row's after-commit callback (so `start!` inside an app transaction on the same database sends nothing until it commits). Inside a step, `subject` (GlobalID, nil once the record is gone), `arguments` (ActiveJob::Arguments, indifferent access, capped at `workflow_max_payload_size`) and the verbs `pause!`, `cancel!`, `reattempt!(wait:)`, `skip!`, `finished!`; outside, `resume!`, `resume_at!(:step)`, `pause!`, `cancel!`, `run_now!`, `reschedule!(at:)` and `rerun!`, each with `actor:`/`reason:`, all checked against one transition table, `Pgbus::Workflow::States` (`available_actions`). An unhandled exception never re-raises into ActiveJob: the step row records the class, message, cleaned and capped backtrace, cause chain, `hostname:pid` and app revision, `Pgbus::ErrorReporter` gets it with the workflow, step and attempt as context, and the run pauses unless a policy (`action:`, `matching:`, `wait:`, `max_reattempts:`, `terminal_action:`, `report:`, or an imperative block) says otherwise. Idempotent by contract: `output(**values)` is written in the UPDATE that completes the step and `output_of(:step)` reads the latest completed attempt only; `step_idempotency_key` is the same on every attempt (`attempt_idempotency_key` differs); `start!(…, idempotency_key:)` returns the existing run with `duplicate_start?`. At most one ongoing run per `(class, subject)` (`Pgbus::Workflow::AlreadyRunning`); subject-less runs are not limited. `removed_step :name` gravestones skip a step a live run still names; an unknown step pauses with `StepNotDefined`; `definition_digest` marks runs that started on an older definition. Every transition writes a `pgbus_workflow_events` row (actor, reason, details) in its own transaction (`wf.events`). Housekeeping from the dispatcher: a redelivered StepJob whose row is still in progress means its worker died, and `workflow_stall_action` (`:pause`, `:reattempt`, `:cancel`) applies at once; the sweep (`workflow_sweep_interval`) stalls a step past its `timeout:` or `workflow_stall_threshold`, re-sends lost scheduled steps and emits `pgbus.workflow.gauge`; finished/cancelled runs are deleted after `workflow_retention` (30 days). Instrumented as `pgbus.workflow.step`, `pgbus.workflow_finished`, `pgbus.workflow_paused`, `pgbus.workflow_cancelled`, `pgbus.workflow.stall_detected`; Prometheus/StatsD and AppSignal get `pgbus_workflow_step_duration_seconds`, `pgbus_workflow_steps_total`, run counters and the `pgbus_workflows`/`pgbus_workflows_stuck` gauges (the AppSignal dashboard gains a workflows row; the StatsD backend sends any `*_seconds` histogram as a `*_ms` timer, × 1000). Instrumentation and error reports of a transition made inside an app's transaction go out when that transaction commits, never on its rollback. `Pgbus::Workflow::TestHelpers` (`perform_next_step`, `run_step_inline`, `speedrun_workflow`) runs steps inline; `Pgbus::Workflow::StepContext` and Pgbus's log context tag a running step; `Pgbus::ActiveJob::Delivery.current` exposes the running job's `msg_id`/`read_ct`. Set up with `rails generate pgbus:add_workflows` (`pgbus:update` detects the missing tables; fresh `pgbus:install` includes them). New configuration: `workflow_queue`, `workflow_retention`, `workflow_sweep_interval`, `workflow_stall_threshold`, `workflow_stall_action`, `workflow_max_payload_size`, `workflow_error_backtrace_lines`. Refs #540.
40
+
41
+ ### Fixed
42
+
43
+ - **A blank signal `idempotency_key` is no key (issue #550).** `wf.signal!(…, idempotency_key: "")` (or whitespace) stored `""` as a real key, so every later signal sent with a blank key dedup'd against the first: it answered `duplicate_delivery?` and was never stored, leaving a step waiting for it. `Signals.deliver` now treats a blank key as none, for app code, the dashboard and the MCP tool alike. Refs #550.
44
+
45
+ ## [0.19.4] - 2026-10-17
46
+
47
+ ### Fixed
48
+
49
+ - **`Pgbus.batch` inside a database transaction counts the jobs that enqueue after commit, and waits for them.** A job with Active Job's `enqueue_after_transaction_commit` added inside an open transaction is enqueued only when the transaction commits, after the block has returned and its thread tag is gone. The batch counted nothing, finished empty when the block returned and sent its `on_complete` (and `on_success`) callback before any member job existed; the jobs then ran outside the batch. Even a rolled-back transaction sent the callback. Now `b.add`, `batch.add` and `perform_later` record the batch on the job instance (`enqueue_batch_id`, transient: never serialized, cleared once the enqueue is done), the adapter tags and counts each job from it at the real enqueue, and the batch starts processing (or an open batch re-checks for finish) through `ActiveRecord.after_all_transactions_commit`, after every deferred enqueue has counted itself in. On a rollback nothing is sent, no callback fires and the batch row is rolled back with the transaction. A deferred first attempt discarded at enqueue (uniqueness or concurrency `on_conflict: :discard`) is uncounted as before, and a `retry_on` re-enqueue still re-joins without a second count. Outside a transaction nothing changes, including the queries a batch runs. Known gaps: a bare `ActiveJob.perform_all_later` inside the block does not record the batch (use `b.add`); a `batch.add` inside a transaction raises `AlreadyFinished` when the transaction commits, not at the `add` line, if the batch finished in the meantime; a batch row on a separate `connects_to` database is not part of the caller's transaction. Refs #553.
50
+ - **AppSignal: the job's transaction is current while the job runs, and carries its error (issue #552).** The AppSignal subscriber opened the `background_job` transaction from a finish-only notification callback, after the job had run, and completed it in the same call. `pgbus.job_failed` fired after that, so its `Appsignal.set_error` found no current transaction and reported nothing: a job whose class no longer exists (`ActiveJob::UnknownJobClassError`) or whose arguments cannot be deserialized retried and dead-lettered with nothing in AppSignal but the counters, an error raised inside `perform` arrived only as an action-less sample from the Rails error reporter, and job code calling `Appsignal.add_tags`, `set_error` or `instrument` attached to nothing. Event handlers had the same shape. The subscriber now listens to `pgbus.executor.execute` and `pgbus.event_processed` on both sides (`start` and `finish`): the `<JobClass>#perform` / `<Handler>#handle` transaction opens before the job or handler runs, is current for the whole execution (deserialize, perform, archive), records the exception the execution raised and completes at the end, so an error raised inside `perform` lands on it once, with its duration and queue start. A failure raised before that span opens (a message body that is not JSON, an event that cannot be built, a dead-letter move that failed) is reported from `pgbus.job_failed` / `pgbus.event_failed` on a transaction of its own with the same action and tags; an error the span already recorded is not reported twice. A transaction that is already current is reused and left to its owner to complete, as AppSignal's own ActiveJob hook does, and a span inside another of its kind restores the outer one when it finishes. Like that hook, the span records any exception that ends the execution, so a job interrupted between `perform` and the archive (an `Async::Stop` on shutdown under `execution_mode: :async`, an `Interrupt`) now shows up as an error sample too. The counters and the `pgbus_job_duration_ms` / `pgbus_event_duration_ms` distributions are recorded as before, once per execution. Refs #552.
51
+
52
+ ## [0.19.3] - 2026-10-17
53
+
54
+ ### Added
55
+
10
56
  - **`Pgbus.stream_key` works inside a non-main Ractor.** `Pgbus.stream_key`, `Pgbus.stream_key!` and `Pgbus.stream_name_budget` (and `Streams::Key`) check the queue-name budget against the `queue_prefix`, which they read from `Pgbus.configuration`; a non-main Ractor cannot read that (it holds the logger and the app's callables), so they raised `Ractor::IsolationError` there. Each `Pgbus.finalize!` and finalizing `Pgbus.configure` (so also the engine's `after_initialize` and `pgbus start`) now publishes `Pgbus.configuration_snapshot`, a Ractor-shareable `Pgbus::Configuration::Snapshot` of the settings a non-main Ractor reads (the `queue_prefix` for now), and the stream keys read it there. On the main Ractor nothing changes: they read the live configuration, so a `queue_prefix` set through a setter still applies at once; a non-main Ractor sees it from the next finalize. Refs #527.
11
- - **Deploy tasks: one-time work owed by a release, run once the fleet has rolled over.** Declare a backfill, data fix or cache warm in `config/deploy_tasks.yml` (the shape of `recurring.yml` minus `schedule:`, plus `version:` and `every_deploy:`) or `config.deploy_tasks`; the scheduler enqueues it once per `(key, version)` (or once per revision for an `every_deploy:` task) after every live pgbus process reports the new app revision and a worker on it is alive, so it never runs on old code during the overlap window and two schedulers never both fire it. It is not exactly-once: a failure between the enqueue and recording the run can repeat a task after a scheduler restart, and a scheduler killed between claiming and enqueueing loses that arm, so deploy tasks must be idempotent. Every pgbus process stamps the revision (`config.app_revision`, `PGBUS_REVISION`, `DASH_VERSION`, `KAMAL_VERSION` or the `REVISION` file) into its heartbeat. The dashboard gets a "Deploy tasks" page with the rollover status, Run now and Enable/Disable; run history lives in `pgbus_deploy_task_runs` (`deploy_task_run_retention`, default 90 days); `pgbus.deploy_task.enqueue` is instrumented and the MCP server gains `pgbus_deploy_tasks`. Set up with `rails generate pgbus:add_deploy_tasks` (`pgbus:update` detects the missing tables). Refs #536.
57
+
58
+ ### Fixed
59
+
60
+ - **The dashboard's confirm and alert dialogs open centred again (issue #529).** The engine's stylesheet is built with Tailwind 4, whose preflight sets `margin: 0` on every element. A modal `<dialog>` keeps the browser's `inset: 0` but lost the `margin: auto` that centres it inside that inset, so every Pause, Purge and Delete confirmation opened in the top-left corner. Both dialogs now carry `m-auto`. The confirm dialog's Confirm button was white on yellow (1.91:1). It now uses the dark-on-yellow warning colours (`bg-yellow-400 text-yellow-950`), and the axe gate audits an open confirm dialog in light and dark mode. Refs #529.
61
+
62
+ ## [0.19.2] - 2026-10-17
63
+
64
+ ### Added
65
+
12
66
  - **`pgbus.event_dead_lettered`: the event consumer says when it dead-letters an event.** A job that exhausted `max_retries` emitted `pgbus.job_dead_lettered`; an event moved to `<queue>_dlq` the same way emitted nothing, so a pager, an error tracker or a metrics counter had no push signal for dead events. `Process::Consumer` now emits `pgbus.event_dead_lettered` once per dead-lettered event, after `move_to_dead_letter` returns and the failed-event row is cleared, with `queue_name` (logical), `routing_key`, `handler` (the class that owns the queue in this process, or nil), `event_id`, `read_ct`, `msg_id` and `error` (the class of the last recorded failure, or nil). `handler` is nil unless exactly one subscriber owns the queue. A body that is not a JSON object (unparseable text, or a jsonb scalar or array written straight to the queue) is still dead-lettered, with `routing_key`, `handler` and `event_id` nil, and its `error` is the consumer's own failure to read it (`JSON::ParserError`, `NoMethodError` or `TypeError`): no handler ran. A move that raises emits nothing. A subscriber that raises is logged at debug level and does not change the outcome: the message stays dead-lettered, with one `dead_lettered` stat, no `failed` stat and no circuit-breaker failure. The AppSignal and metrics subscribers count it as `pgbus_event_count` with `status: "dead_lettered"`, tagged `handler` and `routing_key`. `pgbus.job_dead_lettered` and the consumer's success path are unchanged: the envelope is parsed a second time only on the dead-letter branch. Refs #543.
13
67
  - **feat(batches): `Pgbus.batch do |b| … end`, a builder for starting a batch.** `b.on_complete`, `b.on_success` and `b.on_failure` take an ActiveJob class or a configured instance; `b.add(*jobs)` takes job instances (each keeps its own `.set(wait:, queue:, priority:)`) and sends them in one bulk enqueue. The batch row exists, and a `uniqueness_key` is held, before the block runs, and the batch stays pending until the block returns, so the calls can come in any order and a job that finishes while the block is still adding cannot finish the batch early. Every `perform_later` inside the block joins the batch, as inside `Batch#enqueue`. With `on_conflict: :discard` or `:log` and the key held, the block does not run. Inside a job, `batch(description: …) { |b| … }` opens a new, independent batch; `batch` without a block still reads the job's own batch. A member job adds siblings with `batch.add(NextJob.new(id))`, the open-batch form of `batch.enqueue { … }`. `Pgbus::Batch.start` is the same as `Pgbus.batch`, and `Pgbus::Batch.new` + `#enqueue` stay as the lower-level API. Refs #538.
14
68
 
@@ -16,12 +70,11 @@
16
70
 
17
71
  - **The batch completion callback is `on_complete`.** `Pgbus::Batch.new(on_complete:)`, the builder's `b.on_complete` and the `#on_complete` reader replace `on_finish`, which keeps working as a deprecated alias until 1.0 and logs one warning per process. The database columns stay `on_finish_class` / `on_finish_job` until the 1.0 schema cleanup, so batches created before upgrading still fire, and the `pgbus.batch_finished` instrumentation event keeps its name. The dashboard's batch page labels the callback "On Complete". Refs #538.
18
72
 
19
- ### Fixed
73
+ ## [0.19.1] - 2026-10-17
20
74
 
21
- - **A blank signal `idempotency_key` is no key (issue #550).** `wf.signal!(…, idempotency_key: "")` (or whitespace) stored `""` as a real key, so every later signal sent with a blank key dedup'd against the first: it answered `duplicate_delivery?` and was never stored, leaving a step waiting for it. `Signals.deliver` now treats a blank key as none, for app code, the dashboard and the MCP tool alike. Refs #550.
22
- - **`Pgbus.batch` inside a database transaction counts the jobs that enqueue after commit, and waits for them.** A job with Active Job's `enqueue_after_transaction_commit` added inside an open transaction is enqueued only when the transaction commits, after the block has returned and its thread tag is gone. The batch counted nothing, finished empty when the block returned and sent its `on_complete` (and `on_success`) callback before any member job existed; the jobs then ran outside the batch. Even a rolled-back transaction sent the callback. Now `b.add`, `batch.add` and `perform_later` record the batch on the job instance (`enqueue_batch_id`, transient: never serialized, cleared once the enqueue is done), the adapter tags and counts each job from it at the real enqueue, and the batch starts processing (or an open batch re-checks for finish) through `ActiveRecord.after_all_transactions_commit`, after every deferred enqueue has counted itself in. On a rollback nothing is sent, no callback fires and the batch row is rolled back with the transaction. A deferred first attempt discarded at enqueue (uniqueness or concurrency `on_conflict: :discard`) is uncounted as before, and a `retry_on` re-enqueue still re-joins without a second count. Outside a transaction nothing changes, including the queries a batch runs. Known gaps: a bare `ActiveJob.perform_all_later` inside the block does not record the batch (use `b.add`); a `batch.add` inside a transaction raises `AlreadyFinished` when the transaction commits, not at the `add` line, if the batch finished in the meantime; a batch row on a separate `connects_to` database is not part of the caller's transaction. Refs #553.
23
- - **AppSignal: the job's transaction is current while the job runs, and carries its error (issue #552).** The AppSignal subscriber opened the `background_job` transaction from a finish-only notification callback, after the job had run, and completed it in the same call. `pgbus.job_failed` fired after that, so its `Appsignal.set_error` found no current transaction and reported nothing: a job whose class no longer exists (`ActiveJob::UnknownJobClassError`) or whose arguments cannot be deserialized retried and dead-lettered with nothing in AppSignal but the counters, an error raised inside `perform` arrived only as an action-less sample from the Rails error reporter, and job code calling `Appsignal.add_tags`, `set_error` or `instrument` attached to nothing. Event handlers had the same shape. The subscriber now listens to `pgbus.executor.execute` and `pgbus.event_processed` on both sides (`start` and `finish`): the `<JobClass>#perform` / `<Handler>#handle` transaction opens before the job or handler runs, is current for the whole execution (deserialize, perform, archive), records the exception the execution raised and completes at the end, so an error raised inside `perform` lands on it once, with its duration and queue start. A failure raised before that span opens (a message body that is not JSON, an event that cannot be built, a dead-letter move that failed) is reported from `pgbus.job_failed` / `pgbus.event_failed` on a transaction of its own with the same action and tags; an error the span already recorded is not reported twice. A transaction that is already current is reused and left to its owner to complete, as AppSignal's own ActiveJob hook does, and a span inside another of its kind restores the outer one when it finishes. Like that hook, the span records any exception that ends the execution, so a job interrupted between `perform` and the archive (an `Async::Stop` on shutdown under `execution_mode: :async`, an `Interrupt`) now shows up as an error sample too. The counters and the `pgbus_job_duration_ms` / `pgbus_event_duration_ms` distributions are recorded as before, once per execution. Refs #552.
24
- - **The dashboard's confirm and alert dialogs open centred again (issue #529).** The engine's stylesheet is built with Tailwind 4, whose preflight sets `margin: 0` on every element. A modal `<dialog>` keeps the browser's `inset: 0` but lost the `margin: auto` that centres it inside that inset, so every Pause, Purge and Delete confirmation opened in the top-left corner. Both dialogs now carry `m-auto`. The confirm dialog's Confirm button was white on yellow (1.91:1). It now uses the dark-on-yellow warning colours (`bg-yellow-400 text-yellow-950`), and the axe gate audits an open confirm dialog in light and dark mode. Refs #529.
75
+ ### Added
76
+
77
+ - **Deploy tasks: one-time work owed by a release, run once the fleet has rolled over.** Declare a backfill, data fix or cache warm in `config/deploy_tasks.yml` (the shape of `recurring.yml` minus `schedule:`, plus `version:` and `every_deploy:`) or `config.deploy_tasks`; the scheduler enqueues it once per `(key, version)` (or once per revision for an `every_deploy:` task) after every live pgbus process reports the new app revision and a worker on it is alive, so it never runs on old code during the overlap window and two schedulers never both fire it. It is not exactly-once: a failure between the enqueue and recording the run can repeat a task after a scheduler restart, and a scheduler killed between claiming and enqueueing loses that arm, so deploy tasks must be idempotent. Every pgbus process stamps the revision (`config.app_revision`, `PGBUS_REVISION`, `DASH_VERSION`, `KAMAL_VERSION` or the `REVISION` file) into its heartbeat. The dashboard gets a "Deploy tasks" page with the rollover status, Run now and Enable/Disable; run history lives in `pgbus_deploy_task_runs` (`deploy_task_run_retention`, default 90 days); `pgbus.deploy_task.enqueue` is instrumented and the MCP server gains `pgbus_deploy_tasks`. Set up with `rails generate pgbus:add_deploy_tasks` (`pgbus:update` detects the missing tables). Refs #536.
25
78
 
26
79
  ## [0.19.0] - 2026-10-16
27
80
 
data/README.md CHANGED
@@ -887,7 +887,7 @@ wf.events.map(&:event) # ["started", "step_schedule
887
887
  - **Idempotent by contract**: `output(charge_id: …)` is durable once the step completes and later steps read it with `output_of(:charge)`; `step_idempotency_key` is the same on every attempt, for the external API; `start!(order, idempotency_key: "checkout-#{order.id}")` returns the existing run.
888
888
  - **One ongoing run per subject** and class: a second `start!` raises `Pgbus::Workflow::AlreadyRunning`. Subject-less runs are not limited.
889
889
  - **Fan-out**: `fan_out :notify_vendors do |b| b.add(*jobs) end` opens one batch for the jobs and parks the step until every member finished; the batch's `on_complete` resumes the run, and a dead-lettered member applies `on_batch_failure:` (`:pause!` by default) with `Pgbus::Workflow::FanOutFailed` recorded. A reattempt, or `resume!` of a failed fan-out step, re-runs the whole body on a fresh batch, so members must be idempotent; a run resumed while its batch is still running keeps waiting on that batch.
890
- - **Signals**: `step :capture, wait_for: :payment_confirmed do … received_signal.payload["amount"] … end` parks until `wf.signal!(:payment_confirmed, payload: {…}, idempotency_key: event.id)`, or by subject, `OrderWorkflow.signal!(order, :payment_confirmed, …, missing: :ignore)`. Early signals are buffered, a redelivery with the same key is a no-op (`duplicate_delivery?`), and matchers (`Pgbus::Workflow::SignalMatcher.new(:shipped) { |payload| … }`) narrow what a step takes. `wait_timeout: 2.days, on_timeout: :pause!` (or `:cancel!`, `:skip!`, `:run`) puts a deadline on the wait, fired by the dispatcher's sweep.
890
+ - **Signals**: `step :capture, wait_for: :payment_confirmed do … received_signal.payload["amount"] … end` parks until `wf.signal!(:payment_confirmed, payload: {…}, idempotency_key: event.id)`, or by subject, `OrderWorkflow.signal!(order, :payment_confirmed, …, missing: :ignore)`. Early signals are buffered, a redelivery with the same key is a no-op (`duplicate_delivery?`), and matchers (`Pgbus::Workflow::SignalMatcher.new(:shipped) { |payload| … }`) narrow what a step takes. `wait_timeout: 2.days, on_timeout: :pause!` (or `:cancel!`, `:skip!`, `:run`) puts a deadline on the wait, fired by the dispatcher's sweep; `on_timeout: :notify` makes it a soft deadline that reports the late signal (a `wait_timed_out` event and `pgbus_workflow_wait_overdue_total`) and keeps waiting for it, its deadline moving one `wait_timeout:` on each time (a sweep that missed several reports them once). `wait_timeout:` also takes a method name or a lambda, evaluated on the run when the step parks: `wait_timeout: -> { subject.expires_at }` returns a `Time` (or any non-String value with `#to_time`, such as a DateTime or a TimeWithZone: the deadline itself, however long the run took to reach the wait), a positive Duration or number of seconds counted from park, or `nil` for no timeout.
891
891
  - **Housekeeping**: a worker that dies mid-step is noticed when its message comes back (the visibility heartbeat keeps a live one invisible); a hung step stalls after its `timeout:` or `workflow_stall_threshold`; the dispatcher re-sends lost steps and deletes finished runs after `workflow_retention` (30 days).
892
892
  - **Metrics** for Prometheus/StatsD and AppSignal: per-step duration histograms and counters, and gauges of runs by status and by stuck reason.
893
893
  - **Dashboard**: `/pgbus/workflows` lists runs with what each one does next, filters by state, class, step, error, subject, id and creation date, and shows a run's timeline, attempts, signals and audit trail with its actions (Resume, Resume at, Run now, Skip step, Send signal, Rerun, Cancel); `/pgbus/workflows/attention` groups the stuck runs for bulk Resume/Cancel. `config.web_actor = ->(request) { … }` names the operator on the audit trail and `config.workflow_subject_url = ->(type, id) { … }` links the subject. The MCP server reads the same through `pgbus_workflows`, `pgbus_workflow` and `pgbus_workflows_attention`, and with `config.mcp_write_tools = true` acts on one run per call through `pgbus_workflow_resume`, `_cancel`, `_skip_step`, `_resume_at`, `_run_now` and `_signal` ([write tools](#write-tools-opt-in)).
@@ -1073,6 +1073,7 @@ c.metrics_backend = MyOpenTelemetryBackend.new
1073
1073
  | `pgbus_workflows_finished_total` / `pgbus_workflows_cancelled_total` | counter | `workflow_class` |
1074
1074
  | `pgbus_workflows_paused_total` | counter | `workflow_class`, `error_class` (absent for a manual pause) |
1075
1075
  | `pgbus_workflow_stalls_total` | counter | `workflow_class`, `step`, `detection` (`redelivery`/`sweep`) |
1076
+ | `pgbus_workflow_wait_overdue_total` | counter | `workflow_class`, `step` — one per late notice of an `on_timeout: :notify` wait (a sweep that missed several deadlines sends one) |
1076
1077
  | `pgbus_workflows` | gauge | `workflow_class`, `status` |
1077
1078
  | `pgbus_workflows_stuck` | gauge | `workflow_class`, `reason` (`paused_error`/`stalled`/`paused_manual`/`overdue`/`waiting_overdue`) |
1078
1079
 
@@ -1102,7 +1103,7 @@ ActiveSupport::Notifications.subscribe(/^pgbus\./) do |name, start, finish, _id,
1102
1103
  end
1103
1104
  ```
1104
1105
 
1105
- Metrics-relevant events emitted (the subset the metrics subscriber maps; see `lib/pgbus/instrumentation.rb` for the full `pgbus.*` catalog): `pgbus.executor.execute`, `pgbus.job_completed`, `pgbus.job_failed`, `pgbus.job_dead_lettered`, `pgbus.event_processed`, `pgbus.event_failed`, `pgbus.event_dead_lettered`, `pgbus.event_skipped`, `pgbus.job_visibility_extended`, `pgbus.client.send_message`, `pgbus.client.send_batch`, `pgbus.client.read_batch`, `pgbus.client.pool`, `pgbus.stream.broadcast`, `pgbus.outbox.publish`, `pgbus.recurring.enqueue`, `pgbus.worker.recycle`, `pgbus.consumer.recycle`, `pgbus.workflow.step`, `pgbus.workflow_finished`, `pgbus.workflow_paused`, `pgbus.workflow_cancelled`, `pgbus.workflow.stall_detected`, `pgbus.workflow.gauge`. Payload keys are documented in `lib/pgbus/instrumentation.rb`.
1106
+ Metrics-relevant events emitted (the subset the metrics subscriber maps; see `lib/pgbus/instrumentation.rb` for the full `pgbus.*` catalog): `pgbus.executor.execute`, `pgbus.job_completed`, `pgbus.job_failed`, `pgbus.job_dead_lettered`, `pgbus.event_processed`, `pgbus.event_failed`, `pgbus.event_dead_lettered`, `pgbus.event_skipped`, `pgbus.job_visibility_extended`, `pgbus.client.send_message`, `pgbus.client.send_batch`, `pgbus.client.read_batch`, `pgbus.client.pool`, `pgbus.stream.broadcast`, `pgbus.outbox.publish`, `pgbus.recurring.enqueue`, `pgbus.worker.recycle`, `pgbus.consumer.recycle`, `pgbus.workflow.step`, `pgbus.workflow_finished`, `pgbus.workflow_paused`, `pgbus.workflow_cancelled`, `pgbus.workflow.stall_detected`, `pgbus.workflow.wait_timed_out`, `pgbus.workflow.gauge`. Payload keys are documented in `lib/pgbus/instrumentation.rb`.
1106
1107
 
1107
1108
  ### Connection pool metrics
1108
1109
 
@@ -233,9 +233,27 @@ export async function renderTimeline(data, i18n) {
233
233
  yaxis: { labels: { style: { colors: colors.text } } },
234
234
  legend: { position: "top", labels: { colors: colors.text } },
235
235
  tooltip: { theme: colors.mode, custom: timelineTooltip },
236
+ annotations: { xaxis: waitTimeoutMarks(data, i18n, colors) },
236
237
  });
237
238
  }
238
239
 
240
+ // A dashed line where a wait passed its deadline (issue #565): the waiting
241
+ // colour, labelled in the page's text colour on the card, so it reads in
242
+ // both themes. The page lists the same moments as text.
243
+ function waitTimeoutMarks(data, i18n, colors) {
244
+ return (data.wait_timeouts || []).map((mark) => ({
245
+ x: mark.at,
246
+ borderColor: colors.timeline.waiting,
247
+ strokeDashArray: 4,
248
+ label: {
249
+ text: i18n.wait_timed_out,
250
+ orientation: "horizontal",
251
+ borderColor: colors.timeline.waiting,
252
+ style: { color: colors.text, background: colors.card },
253
+ },
254
+ }));
255
+ }
256
+
239
257
  // The workflow insights (issue #549): runs started, finished and cancelled
240
258
  // per bucket (cancelled in the neutral grey the dashboard uses for it, not
241
259
  // the failure red), and the end-to-end duration histogram of the finished
@@ -1,2 +1,2 @@
1
- function x(t){return{mode:t?"dark":"light",text:t?"#d1d5db":"#4b5563",grid:t?"#374151":"#e5e7eb",axis:t?"#4b5563":"#d1d5db",card:t?"#1f2937":"#ffffff",throughput:t?"#818cf8":"#6366f1",latencyAvg:t?"#818cf8":"#6366f1",latencyP95:t?"#fbbf24":"#f59e0b",status:{success:"#10b981",failed:"#ef4444",dead_lettered:"#f97316"},other:t?"#9ca3af":"#6b7280",timeline:{waited:t?"#4b5563":"#d1d5db",queued:t?"#64748b":"#94a3b8",waiting:t?"#fbbf24":"#f59e0b",paused:t?"#fb923c":"#ea580c",ran_ok:"#10b981",ran_failed:"#ef4444",ran_open:t?"#818cf8":"#6366f1",ran_cancelled:t?"#9ca3af":"#6b7280"}}}function w(){return window.matchMedia("(prefers-reduced-motion: reduce)").matches}function l(t,o,r){return{chart:{id:t,type:o,height:280,toolbar:{show:!1},background:"transparent",foreColor:r.text,animations:{enabled:!w()}},theme:{mode:r.mode},tooltip:{theme:r.mode},grid:{borderColor:r.grid},legend:{labels:{colors:r.text}},dataLabels:{enabled:!1}}}function p(t){return{type:"datetime",labels:{style:{colors:t.text}},axisBorder:{color:t.axis},axisTicks:{color:t.axis},tooltip:{enabled:!1}}}function f(t,o){return{labels:{style:{colors:t.text},formatter:o},axisBorder:{show:!0,color:t.axis}}}function c(t,o){return t.map((r)=>({x:new Date(r.time).getTime(),y:r[o]}))}var m=null;function b(){return m||=import("apexcharts").then(()=>globalThis.ApexCharts).catch((t)=>{throw m=null,t}),m}var u={};function i(t,o,r){if(u[o])u[o].destroy();delete u[o];let e=document.getElementById(o);if(!e)return;u[o]=new t(e,r),u[o].render()}async function v(t,o){let r=await b(),e=x(document.documentElement.classList.contains("dark"));i(r,"throughput-chart",{...l("throughput-chart","area",e),series:[{name:o.seriesName,data:c(t.throughput||[],"count")}],stroke:{curve:"smooth",width:2},fill:{type:"gradient",gradient:{shadeIntensity:1,opacityFrom:0.4,opacityTo:0.05,stops:[0,100]}},colors:[e.throughput],xaxis:p(e),yaxis:f(e,(a)=>Math.round(a))});let s=Object.keys(t.status_counts||{});i(r,"status-chart",{...l("status-chart","donut",e),series:s.map((a)=>t.status_counts[a]),labels:s.map((a)=>o.statuses&&o.statuses[a]||a),colors:s.map((a)=>e.status[a]||e.other),stroke:{colors:[e.card]},legend:{position:"bottom",labels:{colors:e.text}},plotOptions:{pie:{donut:{size:"60%",labels:{show:!0,name:{color:e.text},value:{color:e.text},total:{show:!0,color:e.text}}}}}}),i(r,"latency-chart",{...l("latency-chart","line",e),series:[{name:o.latencyAvg,data:c(t.latency_trend||[],"avg_ms")},{name:o.latencyP95,data:c(t.latency_trend||[],"p95_ms")}],stroke:{curve:"smooth",width:[2,2],dashArray:[0,5]},colors:[e.latencyAvg,e.latencyP95],xaxis:p(e),yaxis:f(e,(a)=>o.milliseconds.replace("%n",Math.round(a))),legend:{position:"top",labels:{colors:e.text}}})}var g=["waited","queued","waiting","ran_ok","ran_failed","ran_open","ran_cancelled","paused"];function _(t,o){let r=Object.fromEntries(g.map((s)=>[s,[]])),e=(s,a,n)=>({x:s,y:[a.from,a.to],duration:a.duration,status:n&&o.statuses?o.statuses[n.status]||n.status:null,error:n?n.error_class:null});return(t.rows||[]).forEach((s)=>{s.segments.forEach((a)=>{if(r[a.series])r[a.series].push(e(s.label,a,s))})}),(t.paused||[]).forEach((s)=>r.paused.push(e(o.paused_row,s,null))),g.filter((s)=>r[s].length>0).map((s)=>({name:o.series&&o.series[s]||s,key:s,data:r[s]}))}function d(t){let o=document.createElement("div");return o.textContent=t==null?"":String(t),o.innerHTML}function E({seriesIndex:t,dataPointIndex:o,w:r}){let e=r.config.series[t],s=e.data[o],a=[`${d(e.name)}: ${d(s.duration)}`];if(s.status)a.push(d(s.status));if(s.error)a.push(d(s.error));return`<div class="apexcharts-tooltip-title">${d(s.x)}</div><div style="padding: 4px 10px">${a.join("<br>")}</div>`}async function A(t,o){let r=await b(),e=x(document.documentElement.classList.contains("dark")),s=_(t,o),a=(t.rows||[]).length+((t.paused||[]).length>0?1:0),n=l("workflow-timeline-chart","rangeBar",e);i(r,"workflow-timeline-chart",{...n,chart:{...n.chart,height:Math.max(160,a*36+90),zoom:{enabled:!0,type:"x"},toolbar:{show:!0,tools:{download:!1,selection:!1,zoom:!0,zoomin:!0,zoomout:!0,pan:!0,reset:!0}}},series:s,colors:s.map((y)=>e.timeline[y.key]||e.other),plotOptions:{bar:{horizontal:!0,rangeBarGroupRows:!0,barHeight:"60%"}},xaxis:p(e),yaxis:{labels:{style:{colors:e.text}}},legend:{position:"top",labels:{colors:e.text}},tooltip:{theme:e.mode,custom:E}})}async function C(t,o){let r=await b(),e=x(document.documentElement.classList.contains("dark")),s=t.throughput||[];i(r,"workflow-throughput-chart",{...l("workflow-throughput-chart","line",e),series:[{name:o.started,data:c(s,"started")},{name:o.finished,data:c(s,"finished")},{name:o.cancelled,data:c(s,"cancelled")}],stroke:{curve:"smooth",width:[2,2,2],dashArray:[0,0,5]},colors:[e.throughput,e.status.success,e.other],xaxis:p(e),yaxis:f(e,(n)=>Math.round(n)),legend:{position:"top",labels:{colors:e.text}}});let a=t.durations||[];i(r,"workflow-duration-chart",{...l("workflow-duration-chart","bar",e),series:[{name:o.runs,data:a.map((n)=>n.count)}],colors:[e.throughput],plotOptions:{bar:{columnWidth:"70%",borderRadius:2}},xaxis:{categories:a.map((n)=>n.label),labels:{style:{colors:e.text}},axisBorder:{color:e.axis},axisTicks:{color:e.axis}},yaxis:f(e,(n)=>Math.round(n))})}var h=null;function T(t,o,r=v){if(h)h.disconnect();h=new MutationObserver(()=>{let e=typeof t==="function"?t():t;if(e)r(e,o)}),h.observe(document.documentElement,{attributes:!0,attributeFilter:["class"]})}export{T as observeThemeChanges,v as renderCharts,A as renderTimeline,C as renderWorkflowInsights};
1
+ function x(t){return{mode:t?"dark":"light",text:t?"#d1d5db":"#4b5563",grid:t?"#374151":"#e5e7eb",axis:t?"#4b5563":"#d1d5db",card:t?"#1f2937":"#ffffff",throughput:t?"#818cf8":"#6366f1",latencyAvg:t?"#818cf8":"#6366f1",latencyP95:t?"#fbbf24":"#f59e0b",status:{success:"#10b981",failed:"#ef4444",dead_lettered:"#f97316"},other:t?"#9ca3af":"#6b7280",timeline:{waited:t?"#4b5563":"#d1d5db",queued:t?"#64748b":"#94a3b8",waiting:t?"#fbbf24":"#f59e0b",paused:t?"#fb923c":"#ea580c",ran_ok:"#10b981",ran_failed:"#ef4444",ran_open:t?"#818cf8":"#6366f1",ran_cancelled:t?"#9ca3af":"#6b7280"}}}function w(){return window.matchMedia("(prefers-reduced-motion: reduce)").matches}function l(t,o,s){return{chart:{id:t,type:o,height:280,toolbar:{show:!1},background:"transparent",foreColor:s.text,animations:{enabled:!w()}},theme:{mode:s.mode},tooltip:{theme:s.mode},grid:{borderColor:s.grid},legend:{labels:{colors:s.text}},dataLabels:{enabled:!1}}}function p(t){return{type:"datetime",labels:{style:{colors:t.text}},axisBorder:{color:t.axis},axisTicks:{color:t.axis},tooltip:{enabled:!1}}}function m(t,o){return{labels:{style:{colors:t.text},formatter:o},axisBorder:{show:!0,color:t.axis}}}function i(t,o){return t.map((s)=>({x:new Date(s.time).getTime(),y:s[o]}))}var f=null;function b(){return f||=import("apexcharts").then(()=>globalThis.ApexCharts).catch((t)=>{throw f=null,t}),f}var u={};function c(t,o,s){if(u[o])u[o].destroy();delete u[o];let e=document.getElementById(o);if(!e)return;u[o]=new t(e,s),u[o].render()}async function _(t,o){let s=await b(),e=x(document.documentElement.classList.contains("dark"));c(s,"throughput-chart",{...l("throughput-chart","area",e),series:[{name:o.seriesName,data:i(t.throughput||[],"count")}],stroke:{curve:"smooth",width:2},fill:{type:"gradient",gradient:{shadeIntensity:1,opacityFrom:0.4,opacityTo:0.05,stops:[0,100]}},colors:[e.throughput],xaxis:p(e),yaxis:m(e,(r)=>Math.round(r))});let a=Object.keys(t.status_counts||{});c(s,"status-chart",{...l("status-chart","donut",e),series:a.map((r)=>t.status_counts[r]),labels:a.map((r)=>o.statuses&&o.statuses[r]||r),colors:a.map((r)=>e.status[r]||e.other),stroke:{colors:[e.card]},legend:{position:"bottom",labels:{colors:e.text}},plotOptions:{pie:{donut:{size:"60%",labels:{show:!0,name:{color:e.text},value:{color:e.text},total:{show:!0,color:e.text}}}}}}),c(s,"latency-chart",{...l("latency-chart","line",e),series:[{name:o.latencyAvg,data:i(t.latency_trend||[],"avg_ms")},{name:o.latencyP95,data:i(t.latency_trend||[],"p95_ms")}],stroke:{curve:"smooth",width:[2,2],dashArray:[0,5]},colors:[e.latencyAvg,e.latencyP95],xaxis:p(e),yaxis:m(e,(r)=>o.milliseconds.replace("%n",Math.round(r))),legend:{position:"top",labels:{colors:e.text}}})}var g=["waited","queued","waiting","ran_ok","ran_failed","ran_open","ran_cancelled","paused"];function v(t,o){let s=Object.fromEntries(g.map((a)=>[a,[]])),e=(a,r,n)=>({x:a,y:[r.from,r.to],duration:r.duration,status:n&&o.statuses?o.statuses[n.status]||n.status:null,error:n?n.error_class:null});return(t.rows||[]).forEach((a)=>{a.segments.forEach((r)=>{if(s[r.series])s[r.series].push(e(a.label,r,a))})}),(t.paused||[]).forEach((a)=>s.paused.push(e(o.paused_row,a,null))),g.filter((a)=>s[a].length>0).map((a)=>({name:o.series&&o.series[a]||a,key:a,data:s[a]}))}function d(t){let o=document.createElement("div");return o.textContent=t==null?"":String(t),o.innerHTML}function E({seriesIndex:t,dataPointIndex:o,w:s}){let e=s.config.series[t],a=e.data[o],r=[`${d(e.name)}: ${d(a.duration)}`];if(a.status)r.push(d(a.status));if(a.error)r.push(d(a.error));return`<div class="apexcharts-tooltip-title">${d(a.x)}</div><div style="padding: 4px 10px">${r.join("<br>")}</div>`}async function C(t,o){let s=await b(),e=x(document.documentElement.classList.contains("dark")),a=v(t,o),r=(t.rows||[]).length+((t.paused||[]).length>0?1:0),n=l("workflow-timeline-chart","rangeBar",e);c(s,"workflow-timeline-chart",{...n,chart:{...n.chart,height:Math.max(160,r*36+90),zoom:{enabled:!0,type:"x"},toolbar:{show:!0,tools:{download:!1,selection:!1,zoom:!0,zoomin:!0,zoomout:!0,pan:!0,reset:!0}}},series:a,colors:a.map((y)=>e.timeline[y.key]||e.other),plotOptions:{bar:{horizontal:!0,rangeBarGroupRows:!0,barHeight:"60%"}},xaxis:p(e),yaxis:{labels:{style:{colors:e.text}}},legend:{position:"top",labels:{colors:e.text}},tooltip:{theme:e.mode,custom:E},annotations:{xaxis:A(t,o,e)}})}function A(t,o,s){return(t.wait_timeouts||[]).map((e)=>({x:e.at,borderColor:s.timeline.waiting,strokeDashArray:4,label:{text:o.wait_timed_out,orientation:"horizontal",borderColor:s.timeline.waiting,style:{color:s.text,background:s.card}}}))}async function k(t,o){let s=await b(),e=x(document.documentElement.classList.contains("dark")),a=t.throughput||[];c(s,"workflow-throughput-chart",{...l("workflow-throughput-chart","line",e),series:[{name:o.started,data:i(a,"started")},{name:o.finished,data:i(a,"finished")},{name:o.cancelled,data:i(a,"cancelled")}],stroke:{curve:"smooth",width:[2,2,2],dashArray:[0,0,5]},colors:[e.throughput,e.status.success,e.other],xaxis:p(e),yaxis:m(e,(n)=>Math.round(n)),legend:{position:"top",labels:{colors:e.text}}});let r=t.durations||[];c(s,"workflow-duration-chart",{...l("workflow-duration-chart","bar",e),series:[{name:o.runs,data:r.map((n)=>n.count)}],colors:[e.throughput],plotOptions:{bar:{columnWidth:"70%",borderRadius:2}},xaxis:{categories:r.map((n)=>n.label),labels:{style:{colors:e.text}},axisBorder:{color:e.axis},axisTicks:{color:e.axis}},yaxis:m(e,(n)=>Math.round(n))})}var h=null;function T(t,o,s=_){if(h)h.disconnect();h=new MutationObserver(()=>{let e=typeof t==="function"?t():t;if(e)s(e,o)}),h.observe(document.documentElement,{attributes:!0,attributeFilter:["class"]})}export{T as observeThemeChanges,_ as renderCharts,C as renderTimeline,k as renderWorkflowInsights};
2
2
  //# sourceMappingURL=charts.map
@@ -1 +1 @@
1
- {"version":3,"sources":["src/charts.js"],"sourcesContent":["// Charts module for the Pgbus Insights page, the workflow timeline and the\n// workflow insights.\n// Loaded only on those pages via an inline <script type=\"module\">, which\n// passes it the series the page embeds. ApexCharts is fetched here, on first\n// draw, so no other dashboard page downloads it (issue #517). The page renders each chart's caption,\n// data table and empty state; this module only draws into the containers that\n// exist, and redraws them when <html class=\"dark\"> toggles.\n\n// Colours for both themes in one place (issue #492). card is the chart card's\n// own background (white / gray-800): the donut's slice separators use it so\n// they read as gaps, not white lines, in dark mode.\nfunction palette(isDark) {\n return {\n mode: isDark ? \"dark\" : \"light\",\n text: isDark ? \"#d1d5db\" : \"#4b5563\",\n grid: isDark ? \"#374151\" : \"#e5e7eb\",\n axis: isDark ? \"#4b5563\" : \"#d1d5db\",\n card: isDark ? \"#1f2937\" : \"#ffffff\",\n throughput: isDark ? \"#818cf8\" : \"#6366f1\",\n latencyAvg: isDark ? \"#818cf8\" : \"#6366f1\",\n latencyP95: isDark ? \"#fbbf24\" : \"#f59e0b\",\n status: {\n success: \"#10b981\",\n failed: \"#ef4444\",\n dead_lettered: \"#f97316\",\n },\n other: isDark ? \"#9ca3af\" : \"#6b7280\",\n // The workflow timeline's segments (issue #542): the waits in neutral\n // tones, ran coloured by how the attempt ended.\n timeline: {\n waited: isDark ? \"#4b5563\" : \"#d1d5db\",\n queued: isDark ? \"#64748b\" : \"#94a3b8\",\n waiting: isDark ? \"#fbbf24\" : \"#f59e0b\",\n paused: isDark ? \"#fb923c\" : \"#ea580c\",\n ran_ok: \"#10b981\",\n ran_failed: \"#ef4444\",\n ran_open: isDark ? \"#818cf8\" : \"#6366f1\",\n ran_cancelled: isDark ? \"#9ca3af\" : \"#6b7280\",\n },\n };\n}\n\nfunction reducedMotion() {\n return window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n}\n\n// Options every chart shares: theme, text, axes, grid, tooltip, animation.\nfunction baseOptions(id, type, colors) {\n return {\n chart: {\n id,\n type,\n height: 280,\n toolbar: { show: false },\n background: \"transparent\",\n foreColor: colors.text,\n animations: { enabled: !reducedMotion() },\n },\n theme: { mode: colors.mode },\n tooltip: { theme: colors.mode },\n grid: { borderColor: colors.grid },\n legend: { labels: { colors: colors.text } },\n dataLabels: { enabled: false },\n };\n}\n\nfunction timeAxis(colors) {\n return {\n type: \"datetime\",\n labels: { style: { colors: colors.text } },\n axisBorder: { color: colors.axis },\n axisTicks: { color: colors.axis },\n tooltip: { enabled: false },\n };\n}\n\nfunction valueAxis(colors, formatter) {\n return {\n labels: { style: { colors: colors.text }, formatter },\n axisBorder: { show: true, color: colors.axis },\n };\n}\n\nfunction points(series, key) {\n return series.map((p) => ({ x: new Date(p.time).getTime(), y: p[key] }));\n}\n\n// vendor/apexcharts.js is a UMD bundle: run as a module it defines the global.\n// Memoized, so the theme-toggle redraws reuse the one download. Every caller\n// awaits the same promise, and its callbacks run in call order, so a redraw\n// can't overtake the first render. A failed import clears the memo, so the\n// next draw (a theme toggle) retries instead of awaiting the same rejection.\nlet apexCharts = null;\nfunction loadApexCharts() {\n apexCharts ||= import(\"apexcharts\")\n .then(() => globalThis.ApexCharts)\n .catch((error) => {\n apexCharts = null;\n throw error;\n });\n return apexCharts;\n}\n\nconst charts = {};\n\nfunction draw(ApexCharts, id, options) {\n if (charts[id]) charts[id].destroy();\n delete charts[id];\n\n const el = document.getElementById(id);\n if (!el) return;\n\n charts[id] = new ApexCharts(el, options);\n charts[id].render();\n}\n\nexport async function renderCharts(data, i18n) {\n const ApexCharts = await loadApexCharts();\n const colors = palette(document.documentElement.classList.contains(\"dark\"));\n\n draw(ApexCharts, \"throughput-chart\", {\n ...baseOptions(\"throughput-chart\", \"area\", colors),\n series: [{ name: i18n.seriesName, data: points(data.throughput || [], \"count\") }],\n stroke: { curve: \"smooth\", width: 2 },\n fill: { type: \"gradient\", gradient: { shadeIntensity: 1, opacityFrom: 0.4, opacityTo: 0.05, stops: [0, 100] } },\n colors: [colors.throughput],\n xaxis: timeAxis(colors),\n yaxis: valueAxis(colors, (v) => Math.round(v)),\n });\n\n const statuses = Object.keys(data.status_counts || {});\n draw(ApexCharts, \"status-chart\", {\n ...baseOptions(\"status-chart\", \"donut\", colors),\n series: statuses.map((s) => data.status_counts[s]),\n labels: statuses.map((s) => (i18n.statuses && i18n.statuses[s]) || s),\n colors: statuses.map((s) => colors.status[s] || colors.other),\n stroke: { colors: [colors.card] },\n legend: { position: \"bottom\", labels: { colors: colors.text } },\n plotOptions: {\n pie: {\n donut: {\n size: \"60%\",\n labels: {\n show: true,\n name: { color: colors.text },\n value: { color: colors.text },\n total: { show: true, color: colors.text },\n },\n },\n },\n },\n });\n\n draw(ApexCharts, \"latency-chart\", {\n ...baseOptions(\"latency-chart\", \"line\", colors),\n series: [\n { name: i18n.latencyAvg, data: points(data.latency_trend || [], \"avg_ms\") },\n { name: i18n.latencyP95, data: points(data.latency_trend || [], \"p95_ms\") },\n ],\n stroke: { curve: \"smooth\", width: [2, 2], dashArray: [0, 5] },\n colors: [colors.latencyAvg, colors.latencyP95],\n xaxis: timeAxis(colors),\n yaxis: valueAxis(colors, (v) => i18n.milliseconds.replace(\"%n\", Math.round(v))),\n legend: { position: \"top\", labels: { colors: colors.text } },\n });\n}\n\nconst TIMELINE_SERIES = [\"waited\", \"queued\", \"waiting\", \"ran_ok\", \"ran_failed\", \"ran_open\", \"ran_cancelled\", \"paused\"];\n\n// One range bar per segment, grouped by series so each kind keeps its colour.\n// Rows are the attempts (oldest first) plus a \"Paused\" row for the pauses.\nfunction timelineSeries(data, i18n) {\n const points = Object.fromEntries(TIMELINE_SERIES.map((name) => [name, []]));\n const point = (label, segment, row) => ({\n x: label,\n y: [segment.from, segment.to],\n duration: segment.duration,\n status: row && i18n.statuses ? i18n.statuses[row.status] || row.status : null,\n error: row ? row.error_class : null,\n });\n\n (data.rows || []).forEach((row) => {\n row.segments.forEach((segment) => {\n if (points[segment.series]) points[segment.series].push(point(row.label, segment, row));\n });\n });\n (data.paused || []).forEach((segment) => points.paused.push(point(i18n.paused_row, segment, null)));\n\n return TIMELINE_SERIES.filter((name) => points[name].length > 0).map((name) => ({\n name: (i18n.series && i18n.series[name]) || name,\n key: name,\n data: points[name],\n }));\n}\n\n// The tooltip: the segment's kind and duration, then the attempt's status and\n// error class. Text only (textContent-safe): every value is escaped.\nfunction escapeText(value) {\n const div = document.createElement(\"div\");\n div.textContent = value == null ? \"\" : String(value);\n return div.innerHTML;\n}\n\nfunction timelineTooltip({ seriesIndex, dataPointIndex, w }) {\n const series = w.config.series[seriesIndex];\n const p = series.data[dataPointIndex];\n const lines = [`${escapeText(series.name)}: ${escapeText(p.duration)}`];\n if (p.status) lines.push(escapeText(p.status));\n if (p.error) lines.push(escapeText(p.error));\n return `<div class=\"apexcharts-tooltip-title\">${escapeText(p.x)}</div><div style=\"padding: 4px 10px\">${lines.join(\"<br>\")}</div>`;\n}\n\nexport async function renderTimeline(data, i18n) {\n const ApexCharts = await loadApexCharts();\n const colors = palette(document.documentElement.classList.contains(\"dark\"));\n const series = timelineSeries(data, i18n);\n const rows = (data.rows || []).length + ((data.paused || []).length > 0 ? 1 : 0);\n const base = baseOptions(\"workflow-timeline-chart\", \"rangeBar\", colors);\n\n draw(ApexCharts, \"workflow-timeline-chart\", {\n ...base,\n chart: {\n ...base.chart,\n height: Math.max(160, rows * 36 + 90),\n // Waits can span days: zoom and pan along time, reset to see it all.\n zoom: { enabled: true, type: \"x\" },\n toolbar: { show: true, tools: { download: false, selection: false, zoom: true, zoomin: true, zoomout: true, pan: true, reset: true } },\n },\n series,\n colors: series.map((s) => colors.timeline[s.key] || colors.other),\n plotOptions: { bar: { horizontal: true, rangeBarGroupRows: true, barHeight: \"60%\" } },\n xaxis: timeAxis(colors),\n yaxis: { labels: { style: { colors: colors.text } } },\n legend: { position: \"top\", labels: { colors: colors.text } },\n tooltip: { theme: colors.mode, custom: timelineTooltip },\n });\n}\n\n// The workflow insights (issue #549): runs started, finished and cancelled\n// per bucket (cancelled in the neutral grey the dashboard uses for it, not\n// the failure red), and the end-to-end duration histogram of the finished\n// runs. The bucket times arrive as the dashboard zone's wall clock written\n// as UTC, which the UTC time axis prints as is.\nexport async function renderWorkflowInsights(data, i18n) {\n const ApexCharts = await loadApexCharts();\n const colors = palette(document.documentElement.classList.contains(\"dark\"));\n const throughput = data.throughput || [];\n\n draw(ApexCharts, \"workflow-throughput-chart\", {\n ...baseOptions(\"workflow-throughput-chart\", \"line\", colors),\n series: [\n { name: i18n.started, data: points(throughput, \"started\") },\n { name: i18n.finished, data: points(throughput, \"finished\") },\n { name: i18n.cancelled, data: points(throughput, \"cancelled\") },\n ],\n stroke: { curve: \"smooth\", width: [2, 2, 2], dashArray: [0, 0, 5] },\n colors: [colors.throughput, colors.status.success, colors.other],\n xaxis: timeAxis(colors),\n yaxis: valueAxis(colors, (v) => Math.round(v)),\n legend: { position: \"top\", labels: { colors: colors.text } },\n });\n\n const durations = data.durations || [];\n draw(ApexCharts, \"workflow-duration-chart\", {\n ...baseOptions(\"workflow-duration-chart\", \"bar\", colors),\n series: [{ name: i18n.runs, data: durations.map((bin) => bin.count) }],\n colors: [colors.throughput],\n plotOptions: { bar: { columnWidth: \"70%\", borderRadius: 2 } },\n xaxis: {\n categories: durations.map((bin) => bin.label),\n labels: { style: { colors: colors.text } },\n axisBorder: { color: colors.axis },\n axisTicks: { color: colors.axis },\n },\n yaxis: valueAxis(colors, (v) => Math.round(v)),\n });\n}\n\nlet themeObserver = null;\n\n// The page toggles dark mode with a class on <html>; redraw in the new palette.\n// render is the page's draw function (renderCharts on Insights, renderTimeline\n// on a workflow, renderWorkflowInsights on a workflow class).\nexport function observeThemeChanges(getDataFn, i18n, render = renderCharts) {\n if (themeObserver) themeObserver.disconnect();\n\n themeObserver = new MutationObserver(() => {\n const data = typeof getDataFn === \"function\" ? getDataFn() : getDataFn;\n if (data) render(data, i18n);\n });\n themeObserver.observe(document.documentElement, { attributes: true, attributeFilter: [\"class\"] });\n}\n"],"mappings":"AAWA,SAAS,CAAO,CAAC,EAAQ,CACvB,MAAO,CACL,KAAM,EAAS,OAAS,QACxB,KAAM,EAAS,UAAY,UAC3B,KAAM,EAAS,UAAY,UAC3B,KAAM,EAAS,UAAY,UAC3B,KAAM,EAAS,UAAY,UAC3B,WAAY,EAAS,UAAY,UACjC,WAAY,EAAS,UAAY,UACjC,WAAY,EAAS,UAAY,UACjC,OAAQ,CACN,QAAS,UACT,OAAQ,UACR,cAAe,SACjB,EACA,MAAO,EAAS,UAAY,UAG5B,SAAU,CACR,OAAQ,EAAS,UAAY,UAC7B,OAAQ,EAAS,UAAY,UAC7B,QAAS,EAAS,UAAY,UAC9B,OAAQ,EAAS,UAAY,UAC7B,OAAQ,UACR,WAAY,UACZ,SAAU,EAAS,UAAY,UAC/B,cAAe,EAAS,UAAY,SACtC,CACF,EAGF,SAAS,CAAa,EAAG,CACvB,OAAO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AU,EAAQ,CAAC,IAAM,KAAK,MAAM,CAAC,CAAC,EAC7C,OAAQ,CAAE,SAAU,MAAO,OAAQ,CAAE,OAAQ,EAAO,IAAK,CAAE,CAC7D,CAAC,EAED,IAAM,EAAY,EAAK,WAAa,CAAC,EACrC,EAAK,EAAY,0BAA2B,IACvC,EAAY,0BAA2B,MAAO,CAAM,EACvD,OAAQ,CAAC,CAAE,KAAM,EAAK,KAAM,KAAM,EAAU,IAAI,CAAC,IAAQ,EAAI,KAAK,CAAE,CAAC,EACrE,OAAQ,CAAC,EAAO,UAAU,EAC1B,YAAa,CAAE,IAAK,CAAE,YAAa,MAAO,aAAc,CAAE,CAAE,EAC5D,MAAO,CACL,WAAY,EAAU,IAAI,CAAC,IAAQ,EAAI,KAAK,EAC5C,OAAQ,CAAE,MAAO,CAAE,OAAQ,EAAO,IAAK,CAAE,EACzC,WAAY,CAAE,MAAO,EAAO,IAAK,EACjC,UAAW,CAAE,MAAO,EAAO,IAAK,CAClC,EACA,MAAO,EAAU,EAAQ,CAAC,IAAM,KAAK,MAAM,CAAC,CAAC,CAC/C,CAAC,EAGH,IAAI,EAAgB,KAKb,SAAS,CAAmB,CAAC,EAAW,EAAM,EAAS,EAAc,CAC1E,GAAI,EAAe,EAAc,WAAW,EAE5C,EAAgB,IAAI,iBAAiB,IAAM,CACzC,IAAM,EAAO,OAAO,IAAc,WAAa,EAAU,EAAI,EAC7D,GAAI,EAAM,EAAO,EAAM,CAAI,EAC5B,EACD,EAAc,QAAQ,SAAS,gBAAiB,CAAE,WAAY,GAAM,gBAAiB,CAAC,OAAO,CAAE,CAAC","names":[]}
1
+ {"version":3,"sources":["src/charts.js"],"sourcesContent":["// Charts module for the Pgbus Insights page, the workflow timeline and the\n// workflow insights.\n// Loaded only on those pages via an inline <script type=\"module\">, which\n// passes it the series the page embeds. ApexCharts is fetched here, on first\n// draw, so no other dashboard page downloads it (issue #517). The page renders each chart's caption,\n// data table and empty state; this module only draws into the containers that\n// exist, and redraws them when <html class=\"dark\"> toggles.\n\n// Colours for both themes in one place (issue #492). card is the chart card's\n// own background (white / gray-800): the donut's slice separators use it so\n// they read as gaps, not white lines, in dark mode.\nfunction palette(isDark) {\n return {\n mode: isDark ? \"dark\" : \"light\",\n text: isDark ? \"#d1d5db\" : \"#4b5563\",\n grid: isDark ? \"#374151\" : \"#e5e7eb\",\n axis: isDark ? \"#4b5563\" : \"#d1d5db\",\n card: isDark ? \"#1f2937\" : \"#ffffff\",\n throughput: isDark ? \"#818cf8\" : \"#6366f1\",\n latencyAvg: isDark ? \"#818cf8\" : \"#6366f1\",\n latencyP95: isDark ? \"#fbbf24\" : \"#f59e0b\",\n status: {\n success: \"#10b981\",\n failed: \"#ef4444\",\n dead_lettered: \"#f97316\",\n },\n other: isDark ? \"#9ca3af\" : \"#6b7280\",\n // The workflow timeline's segments (issue #542): the waits in neutral\n // tones, ran coloured by how the attempt ended.\n timeline: {\n waited: isDark ? \"#4b5563\" : \"#d1d5db\",\n queued: isDark ? \"#64748b\" : \"#94a3b8\",\n waiting: isDark ? \"#fbbf24\" : \"#f59e0b\",\n paused: isDark ? \"#fb923c\" : \"#ea580c\",\n ran_ok: \"#10b981\",\n ran_failed: \"#ef4444\",\n ran_open: isDark ? \"#818cf8\" : \"#6366f1\",\n ran_cancelled: isDark ? \"#9ca3af\" : \"#6b7280\",\n },\n };\n}\n\nfunction reducedMotion() {\n return window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n}\n\n// Options every chart shares: theme, text, axes, grid, tooltip, animation.\nfunction baseOptions(id, type, colors) {\n return {\n chart: {\n id,\n type,\n height: 280,\n toolbar: { show: false },\n background: \"transparent\",\n foreColor: colors.text,\n animations: { enabled: !reducedMotion() },\n },\n theme: { mode: colors.mode },\n tooltip: { theme: colors.mode },\n grid: { borderColor: colors.grid },\n legend: { labels: { colors: colors.text } },\n dataLabels: { enabled: false },\n };\n}\n\nfunction timeAxis(colors) {\n return {\n type: \"datetime\",\n labels: { style: { colors: colors.text } },\n axisBorder: { color: colors.axis },\n axisTicks: { color: colors.axis },\n tooltip: { enabled: false },\n };\n}\n\nfunction valueAxis(colors, formatter) {\n return {\n labels: { style: { colors: colors.text }, formatter },\n axisBorder: { show: true, color: colors.axis },\n };\n}\n\nfunction points(series, key) {\n return series.map((p) => ({ x: new Date(p.time).getTime(), y: p[key] }));\n}\n\n// vendor/apexcharts.js is a UMD bundle: run as a module it defines the global.\n// Memoized, so the theme-toggle redraws reuse the one download. Every caller\n// awaits the same promise, and its callbacks run in call order, so a redraw\n// can't overtake the first render. A failed import clears the memo, so the\n// next draw (a theme toggle) retries instead of awaiting the same rejection.\nlet apexCharts = null;\nfunction loadApexCharts() {\n apexCharts ||= import(\"apexcharts\")\n .then(() => globalThis.ApexCharts)\n .catch((error) => {\n apexCharts = null;\n throw error;\n });\n return apexCharts;\n}\n\nconst charts = {};\n\nfunction draw(ApexCharts, id, options) {\n if (charts[id]) charts[id].destroy();\n delete charts[id];\n\n const el = document.getElementById(id);\n if (!el) return;\n\n charts[id] = new ApexCharts(el, options);\n charts[id].render();\n}\n\nexport async function renderCharts(data, i18n) {\n const ApexCharts = await loadApexCharts();\n const colors = palette(document.documentElement.classList.contains(\"dark\"));\n\n draw(ApexCharts, \"throughput-chart\", {\n ...baseOptions(\"throughput-chart\", \"area\", colors),\n series: [{ name: i18n.seriesName, data: points(data.throughput || [], \"count\") }],\n stroke: { curve: \"smooth\", width: 2 },\n fill: { type: \"gradient\", gradient: { shadeIntensity: 1, opacityFrom: 0.4, opacityTo: 0.05, stops: [0, 100] } },\n colors: [colors.throughput],\n xaxis: timeAxis(colors),\n yaxis: valueAxis(colors, (v) => Math.round(v)),\n });\n\n const statuses = Object.keys(data.status_counts || {});\n draw(ApexCharts, \"status-chart\", {\n ...baseOptions(\"status-chart\", \"donut\", colors),\n series: statuses.map((s) => data.status_counts[s]),\n labels: statuses.map((s) => (i18n.statuses && i18n.statuses[s]) || s),\n colors: statuses.map((s) => colors.status[s] || colors.other),\n stroke: { colors: [colors.card] },\n legend: { position: \"bottom\", labels: { colors: colors.text } },\n plotOptions: {\n pie: {\n donut: {\n size: \"60%\",\n labels: {\n show: true,\n name: { color: colors.text },\n value: { color: colors.text },\n total: { show: true, color: colors.text },\n },\n },\n },\n },\n });\n\n draw(ApexCharts, \"latency-chart\", {\n ...baseOptions(\"latency-chart\", \"line\", colors),\n series: [\n { name: i18n.latencyAvg, data: points(data.latency_trend || [], \"avg_ms\") },\n { name: i18n.latencyP95, data: points(data.latency_trend || [], \"p95_ms\") },\n ],\n stroke: { curve: \"smooth\", width: [2, 2], dashArray: [0, 5] },\n colors: [colors.latencyAvg, colors.latencyP95],\n xaxis: timeAxis(colors),\n yaxis: valueAxis(colors, (v) => i18n.milliseconds.replace(\"%n\", Math.round(v))),\n legend: { position: \"top\", labels: { colors: colors.text } },\n });\n}\n\nconst TIMELINE_SERIES = [\"waited\", \"queued\", \"waiting\", \"ran_ok\", \"ran_failed\", \"ran_open\", \"ran_cancelled\", \"paused\"];\n\n// One range bar per segment, grouped by series so each kind keeps its colour.\n// Rows are the attempts (oldest first) plus a \"Paused\" row for the pauses.\nfunction timelineSeries(data, i18n) {\n const points = Object.fromEntries(TIMELINE_SERIES.map((name) => [name, []]));\n const point = (label, segment, row) => ({\n x: label,\n y: [segment.from, segment.to],\n duration: segment.duration,\n status: row && i18n.statuses ? i18n.statuses[row.status] || row.status : null,\n error: row ? row.error_class : null,\n });\n\n (data.rows || []).forEach((row) => {\n row.segments.forEach((segment) => {\n if (points[segment.series]) points[segment.series].push(point(row.label, segment, row));\n });\n });\n (data.paused || []).forEach((segment) => points.paused.push(point(i18n.paused_row, segment, null)));\n\n return TIMELINE_SERIES.filter((name) => points[name].length > 0).map((name) => ({\n name: (i18n.series && i18n.series[name]) || name,\n key: name,\n data: points[name],\n }));\n}\n\n// The tooltip: the segment's kind and duration, then the attempt's status and\n// error class. Text only (textContent-safe): every value is escaped.\nfunction escapeText(value) {\n const div = document.createElement(\"div\");\n div.textContent = value == null ? \"\" : String(value);\n return div.innerHTML;\n}\n\nfunction timelineTooltip({ seriesIndex, dataPointIndex, w }) {\n const series = w.config.series[seriesIndex];\n const p = series.data[dataPointIndex];\n const lines = [`${escapeText(series.name)}: ${escapeText(p.duration)}`];\n if (p.status) lines.push(escapeText(p.status));\n if (p.error) lines.push(escapeText(p.error));\n return `<div class=\"apexcharts-tooltip-title\">${escapeText(p.x)}</div><div style=\"padding: 4px 10px\">${lines.join(\"<br>\")}</div>`;\n}\n\nexport async function renderTimeline(data, i18n) {\n const ApexCharts = await loadApexCharts();\n const colors = palette(document.documentElement.classList.contains(\"dark\"));\n const series = timelineSeries(data, i18n);\n const rows = (data.rows || []).length + ((data.paused || []).length > 0 ? 1 : 0);\n const base = baseOptions(\"workflow-timeline-chart\", \"rangeBar\", colors);\n\n draw(ApexCharts, \"workflow-timeline-chart\", {\n ...base,\n chart: {\n ...base.chart,\n height: Math.max(160, rows * 36 + 90),\n // Waits can span days: zoom and pan along time, reset to see it all.\n zoom: { enabled: true, type: \"x\" },\n toolbar: { show: true, tools: { download: false, selection: false, zoom: true, zoomin: true, zoomout: true, pan: true, reset: true } },\n },\n series,\n colors: series.map((s) => colors.timeline[s.key] || colors.other),\n plotOptions: { bar: { horizontal: true, rangeBarGroupRows: true, barHeight: \"60%\" } },\n xaxis: timeAxis(colors),\n yaxis: { labels: { style: { colors: colors.text } } },\n legend: { position: \"top\", labels: { colors: colors.text } },\n tooltip: { theme: colors.mode, custom: timelineTooltip },\n annotations: { xaxis: waitTimeoutMarks(data, i18n, colors) },\n });\n}\n\n// A dashed line where a wait passed its deadline (issue #565): the waiting\n// colour, labelled in the page's text colour on the card, so it reads in\n// both themes. The page lists the same moments as text.\nfunction waitTimeoutMarks(data, i18n, colors) {\n return (data.wait_timeouts || []).map((mark) => ({\n x: mark.at,\n borderColor: colors.timeline.waiting,\n strokeDashArray: 4,\n label: {\n text: i18n.wait_timed_out,\n orientation: \"horizontal\",\n borderColor: colors.timeline.waiting,\n style: { color: colors.text, background: colors.card },\n },\n }));\n}\n\n// The workflow insights (issue #549): runs started, finished and cancelled\n// per bucket (cancelled in the neutral grey the dashboard uses for it, not\n// the failure red), and the end-to-end duration histogram of the finished\n// runs. The bucket times arrive as the dashboard zone's wall clock written\n// as UTC, which the UTC time axis prints as is.\nexport async function renderWorkflowInsights(data, i18n) {\n const ApexCharts = await loadApexCharts();\n const colors = palette(document.documentElement.classList.contains(\"dark\"));\n const throughput = data.throughput || [];\n\n draw(ApexCharts, \"workflow-throughput-chart\", {\n ...baseOptions(\"workflow-throughput-chart\", \"line\", colors),\n series: [\n { name: i18n.started, data: points(throughput, \"started\") },\n { name: i18n.finished, data: points(throughput, \"finished\") },\n { name: i18n.cancelled, data: points(throughput, \"cancelled\") },\n ],\n stroke: { curve: \"smooth\", width: [2, 2, 2], dashArray: [0, 0, 5] },\n colors: [colors.throughput, colors.status.success, colors.other],\n xaxis: timeAxis(colors),\n yaxis: valueAxis(colors, (v) => Math.round(v)),\n legend: { position: \"top\", labels: { colors: colors.text } },\n });\n\n const durations = data.durations || [];\n draw(ApexCharts, \"workflow-duration-chart\", {\n ...baseOptions(\"workflow-duration-chart\", \"bar\", colors),\n series: [{ name: i18n.runs, data: durations.map((bin) => bin.count) }],\n colors: [colors.throughput],\n plotOptions: { bar: { columnWidth: \"70%\", borderRadius: 2 } },\n xaxis: {\n categories: durations.map((bin) => bin.label),\n labels: { style: { colors: colors.text } },\n axisBorder: { color: colors.axis },\n axisTicks: { color: colors.axis },\n },\n yaxis: valueAxis(colors, (v) => Math.round(v)),\n });\n}\n\nlet themeObserver = null;\n\n// The page toggles dark mode with a class on <html>; redraw in the new palette.\n// render is the page's draw function (renderCharts on Insights, renderTimeline\n// on a workflow, renderWorkflowInsights on a workflow class).\nexport function observeThemeChanges(getDataFn, i18n, render = renderCharts) {\n if (themeObserver) themeObserver.disconnect();\n\n themeObserver = new MutationObserver(() => {\n const data = typeof getDataFn === \"function\" ? getDataFn() : getDataFn;\n if (data) render(data, i18n);\n });\n themeObserver.observe(document.documentElement, { attributes: true, attributeFilter: [\"class\"] });\n}\n"],"mappings":"AAWA,SAAS,CAAO,CAAC,EAAQ,CACvB,MAAO,CACL,KAAM,EAAS,OAAS,QACxB,KAAM,EAAS,UAAY,UAC3B,KAAM,EAAS,UAAY,UAC3B,KAAM,EAAS,UAAY,UAC3B,KAAM,EAAS,UAAY,UAC3B,WAAY,EAAS,UAAY,UACjC,WAAY,EAAS,UAAY,UACjC,WAAY,EAAS,UAAY,UACjC,OAAQ,CACN,QAAS,UACT,OAAQ,UACR,cAAe,SACjB,EACA,MAAO,EAAS,UAAY,UAG5B,SAAU,CACR,OAAQ,EAAS,UAAY,UAC7B,OAAQ,EAAS,UAAY,UAC7B,QAAS,EAAS,UAAY,UAC9B,OAAQ,EAAS,UAAY,UAC7B,OAAQ,UACR,WAAY,UACZ,SAAU,EAAS,UAAY,UAC/B,cAAe,EAAS,UAAY,SACtC,CACF,EAGF,SAAS,CAAa,EAAG,CACvB,OAAO,OAAO,WAAW,kCAAkC,EAAE,QAI/D,SAAS,CAAW,CAAC,EAAI,EAAM,EAAQ,CACrC,MAAO,CACL,MAAO,CACL,KACA,OACA,OAAQ,IACR,QAAS,CAAE,KAAM,EAAM,EACvB,WAAY,cACZ,UAAW,EAAO,KAClB,WAAY,CAAE,QAAS,CAAC,EAAc,CAAE,CAC1C,EACA,MAAO,CAAE,KAAM,EAAO,IAAK,EAC3B,QAAS,CAAE,MAAO,EAAO,IAAK,EAC9B,KAAM,CAAE,YAAa,EAAO,IAAK,EACjC,OAAQ,CAAE,OAAQ,CAAE,OAAQ,EAAO,IAAK,CAAE,EAC1C,WAAY,CAAE,QAAS,EAAM,CAC/B,EAGF,SAAS,CAAQ,CAAC,EAAQ,CACxB,MAAO,CACL,KAAM,WACN,OAAQ,CAAE,MAAO,CAAE,OAAQ,EAAO,IAAK,CAAE,EACzC,WAAY,CAAE,MAAO,EAAO,IAAK,EACjC,UAAW,CAAE,MAAO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E,KAAM,EAAK,KAAM,KAAM,EAAU,IAAI,CAAC,IAAQ,EAAI,KAAK,CAAE,CAAC,EACrE,OAAQ,CAAC,EAAO,UAAU,EAC1B,YAAa,CAAE,IAAK,CAAE,YAAa,MAAO,aAAc,CAAE,CAAE,EAC5D,MAAO,CACL,WAAY,EAAU,IAAI,CAAC,IAAQ,EAAI,KAAK,EAC5C,OAAQ,CAAE,MAAO,CAAE,OAAQ,EAAO,IAAK,CAAE,EACzC,WAAY,CAAE,MAAO,EAAO,IAAK,EACjC,UAAW,CAAE,MAAO,EAAO,IAAK,CAClC,EACA,MAAO,EAAU,EAAQ,CAAC,IAAM,KAAK,MAAM,CAAC,CAAC,CAC/C,CAAC,EAGH,IAAI,EAAgB,KAKb,SAAS,CAAmB,CAAC,EAAW,EAAM,EAAS,EAAc,CAC1E,GAAI,EAAe,EAAc,WAAW,EAE5C,EAAgB,IAAI,iBAAiB,IAAM,CACzC,IAAM,EAAO,OAAO,IAAc,WAAa,EAAU,EAAI,EAC7D,GAAI,EAAM,EAAO,EAAM,CAAI,EAC5B,EACD,EAAc,QAAQ,SAAS,gBAAiB,CAAE,WAAY,GAAM,gBAAiB,CAAC,OAAO,CAAE,CAAC","names":[]}
@@ -72,6 +72,14 @@ module Pgbus
72
72
 
73
73
  def pgbus_insight_definition_path(name, window) = pgbus.workflow_definition_path(name, window: window.name)
74
74
 
75
+ # A signal step's wait timeout: its length, or "dynamic (per run)" for a
76
+ # wait_timeout: lambda or method name (issue #574).
77
+ def pgbus_insight_wait_timeout(seconds)
78
+ return t("pgbus.workflow_insights.show.pipeline.wait_timeout_dynamic") if seconds == "dynamic"
79
+
80
+ pgbus_duration(seconds)
81
+ end
82
+
75
83
  def pgbus_insight_kind(step)
76
84
  key = step[:declared] ? step[:kind] : "undeclared"
77
85
  return unless KIND_TONES.key?(key)
@@ -73,7 +73,7 @@ module Pgbus
73
73
  case key
74
74
  when :step, :signal then [key, tag.code(value, class: "font-mono")]
75
75
  when :batch then [key, pgbus_workflow_batch_link(value, row[:batch_id])]
76
- when :ago then [key, pgbus_time(value)]
76
+ when :ago, :late then [key, pgbus_time(value)]
77
77
  when :time then [key, pgbus_time(value, clock: true)]
78
78
  else [key, value]
79
79
  end
@@ -158,7 +158,7 @@ module Pgbus
158
158
  def pgbus_workflow_chart_i18n
159
159
  series = %w[waited queued waiting paused ran_ok ran_failed ran_open ran_cancelled]
160
160
  { series: series.index_with { |name| t("pgbus.workflows.timeline.series.#{name}") },
161
- paused_row: t("pgbus.workflows.timeline.paused_row"),
161
+ paused_row: t("pgbus.workflows.timeline.paused_row"), wait_timed_out: t("pgbus.workflows.events.wait_timed_out"),
162
162
  statuses: Workflow::States::STEP.index_with { |status| t("pgbus.workflows.step_statuses.#{status}") } }
163
163
  end
164
164
  end
@@ -62,5 +62,17 @@ module Pgbus
62
62
  .limit(limit)
63
63
  .pluck(:concurrency_key)
64
64
  end
65
+
66
+ # The uniqueness keys, out of +lock_keys+, that a parked job carries. Its
67
+ # :until_executed row stays unbound until it runs and it sits in no queue,
68
+ # so the uniqueness reaper asks here before calling the row an orphan. A
69
+ # row parked before the double-encoding fix is read through its string,
70
+ # since the reaper can run before the promote sweep repairs it.
71
+ def self.parked_uniqueness_keys(lock_keys)
72
+ key = Uniqueness::METADATA_KEY
73
+ key_path = "CASE WHEN jsonb_typeof(payload) = 'string' " \
74
+ "THEN (payload #>> '{}')::jsonb ->> '#{key}' ELSE payload ->> '#{key}' END"
75
+ where("#{key_path} IN (?)", lock_keys).distinct.pluck(Arel.sql(key_path)).to_set
76
+ end
65
77
  end
66
78
  end
@@ -40,10 +40,11 @@ module Pgbus
40
40
  # Does not touch created_at — the reaper's age floor is from acquire time.
41
41
  # Restricted to this enqueue's unbound row (msg_id=0, matching created_at
42
42
  # when acquire! stamped one) so a completed job's bind cannot retarget a
43
- # successor that re-acquired the key.
44
- def self.bind!(lock_key, queue_name:, msg_id:)
43
+ # successor that re-acquired the key. A retry passes the stamp unbind!
44
+ # returned as +acquired_at+.
45
+ def self.bind!(lock_key, queue_name:, msg_id:, acquired_at: nil)
45
46
  stamps = Thread.current[:pgbus_uniqueness_created_at]
46
- created_at = stamps&.delete(lock_key)
47
+ created_at = acquired_at || stamps&.delete(lock_key)
47
48
  sql = "UPDATE #{table_name} SET queue_name = $2, msg_id = $3 " \
48
49
  "WHERE lock_key = $1 AND msg_id = 0"
49
50
  binds = [lock_key, queue_name, msg_id]
@@ -86,6 +87,90 @@ module Pgbus
86
87
  )
87
88
  end
88
89
 
90
+ # The row a message holds: bound to that message (queue + msg_id, see
91
+ # release_if_bound!), or still unbound — a parked job's row, or one whose
92
+ # bind failed — and acquired no later than the message was sent ($4). A
93
+ # lock is always taken before its message exists, so an unbound row
94
+ # acquired after the send is a successor's. created_at is a timestamp
95
+ # without time zone written in the session's zone, and the cast compares
96
+ # it in that same zone; PGMQ's enqueued_at string carries its offset.
97
+ # Unbound means msg_id <= 0 only: "pending" (the placeholder queue) is a
98
+ # name a real queue can have too, and a lock bound to one of its messages
99
+ # is bound.
100
+ HELD_BY_CONDITION = "lock_key = $1 AND ((queue_name = $2 AND msg_id = $3 AND msg_id > 0) OR " \
101
+ "(msg_id <= 0 AND created_at <= $4::timestamptz))"
102
+ private_constant :HELD_BY_CONDITION
103
+
104
+ # Release the lock a finishing message holds (success or DLQ). Unlike the
105
+ # key-only release!, it leaves a row that has moved on: handed to the
106
+ # message's retry (unbind!), or taken by a successor after this
107
+ # message's own row went. +enqueued_at+ is when that message was sent;
108
+ # without it only the row bound to the message goes (an unbound row could
109
+ # be a retry's it handed over), and the reaper takes an orphan.
110
+ def self.release_held_by!(lock_key, queue_name:, msg_id:, enqueued_at:)
111
+ Thread.current[:pgbus_uniqueness_created_at]&.delete(lock_key)
112
+ connection.exec_delete(
113
+ "DELETE FROM #{table_name} WHERE #{HELD_BY_CONDITION}",
114
+ "UniquenessKey Release Held By", held_by_binds(lock_key, queue_name, msg_id, sent_at_literal(enqueued_at))
115
+ )
116
+ end
117
+
118
+ # Hand the running attempt's lock to its retry, which goes out through the
119
+ # key the attempt still holds (issue #333) and never acquires one. Called
120
+ # before the retry exists, as a first enqueue acquires before its send:
121
+ # the row is left unbound on the retry's queue and stamped now, after the
122
+ # attempt was sent, so the attempt's release no longer counts it as its
123
+ # own while the retry's (sent later) does, even if the retry finishes
124
+ # before bind! points the row at it. Returns the new stamp for that
125
+ # bind!, or nil when the attempt no longer holds the key. Without the
126
+ # attempt's enqueued_at only a row bound to it is taken.
127
+ def self.unbind!(lock_key, from_queue_name:, from_msg_id:, from_enqueued_at:, queue_name:)
128
+ binds = held_by_binds(lock_key, from_queue_name, from_msg_id, sent_at_literal(from_enqueued_at))
129
+ result = connection.exec_query(
130
+ "UPDATE #{table_name} SET queue_name = $5, msg_id = 0, created_at = CURRENT_TIMESTAMP " \
131
+ "WHERE #{HELD_BY_CONDITION} RETURNING created_at",
132
+ "UniquenessKey Unbind", binds + [queue_name.to_s]
133
+ )
134
+ result.rows.first&.first
135
+ end
136
+
137
+ # release_held_by! for many messages at once — the dashboard's bulk
138
+ # discards and queue purges — as [lock_key, queue_name, msg_id,
139
+ # enqueued_at] tuples, one statement per thousand.
140
+ def self.release_all_held_by!(holders)
141
+ holders.each_slice(1000).sum do |slice|
142
+ values = slice.map do |lock_key, queue_name, msg_id, enqueued_at|
143
+ "(#{connection.quote(lock_key.to_s)}, #{connection.quote(queue_name.to_s)}, #{msg_id.to_i}, " \
144
+ "#{connection.quote(sent_at_literal(enqueued_at))}::timestamptz)"
145
+ end
146
+ connection.exec_delete(
147
+ "DELETE FROM #{table_name} AS u USING (VALUES #{values.join(", ")}) AS m(lock_key, queue_name, msg_id, sent_at) " \
148
+ "WHERE u.lock_key = m.lock_key AND ((u.queue_name = m.queue_name AND u.msg_id = m.msg_id AND u.msg_id > 0) OR " \
149
+ "(u.msg_id <= 0 AND u.created_at <= m.sent_at))",
150
+ "UniquenessKey Release All Held By"
151
+ )
152
+ end
153
+ end
154
+
155
+ def self.held_by_binds(lock_key, queue_name, msg_id, sent_at)
156
+ [lock_key, queue_name.to_s, msg_id.to_i, sent_at]
157
+ end
158
+ private_class_method :held_by_binds
159
+
160
+ # A message's enqueued_at as a timestamptz literal that carries its own
161
+ # offset. PGMQ hands over a String that already does; a Time (the
162
+ # dashboard reads it through ActiveRecord) or epoch seconds would
163
+ # otherwise be bound as zone-less digits and read in the session's zone.
164
+ # An unknown send time is -infinity: no unbound row counts as the
165
+ # message's own.
166
+ def self.sent_at_literal(enqueued_at)
167
+ sent_at = enqueued_at.is_a?(Numeric) ? Time.at(enqueued_at) : enqueued_at
168
+ return "-infinity" if sent_at.nil?
169
+
170
+ sent_at.respond_to?(:iso8601) ? sent_at.iso8601(6) : sent_at.to_s
171
+ end
172
+ private_class_method :sent_at_literal
173
+
89
174
  # Release a lock only while it is still UNBOUND (msg_id = 0) AND was
90
175
  # acquired no later than +acquired_before+. Used when discarding a parked
91
176
  # job: a parked job never got a msg_id, so its lock was never bound, and
@@ -55,8 +55,9 @@ module Pgbus
55
55
  # paused_manual paused otherwise (the step called pause!, or an outside pause!)
56
56
  # overdue ready, its scheduled step due more than +threshold+ ago
57
57
  # waiting_overdue waiting longer than +waiting_threshold+ on a step
58
- # without a wait_timeout: (only with +waiting_threshold+,
59
- # which needs the waiting-step columns)
58
+ # without a deadline (no timeout_at: no wait_timeout:, or
59
+ # a per-run one that set none or cleared it; only with
60
+ # +waiting_threshold+, which needs the waiting-step columns)
60
61
  # With +workflow_class+, that class's runs only (the workflow insights,
61
62
  # issue #549).
62
63
  def self.stuck_reason_counts(threshold:, now: Time.current, waiting_threshold: nil, workflow_class: nil)
@@ -55,6 +55,13 @@ module Pgbus
55
55
  where(id: id, status: "waiting").update_all(status: "in_progress")
56
56
  end
57
57
 
58
+ # A waiting row's next deadline (on_timeout: :notify keeps the attempt
59
+ # waiting). Written without the model callback, so no StepJob goes out.
60
+ # Returns the number of rows moved.
61
+ def self.move_deadline!(id, timeout_at:)
62
+ where(id: id, status: "waiting").update_all(timeout_at: timeout_at)
63
+ end
64
+
58
65
  # waiting rows of waiting runs past their timeout_at (the sweep applies
59
66
  # their on_timeout).
60
67
  def self.wait_expired(now: Time.current)
@@ -1,6 +1,7 @@
1
1
  <%# The timeline (issue #542): one range-bar row per attempt, split into
2
2
  waited, queued and ran (ran coloured by how the attempt ended), with the
3
- waits and pauses as their own segments. charts.js renderTimeline draws
3
+ waits and pauses as their own segments and a dashed line per missed wait
4
+ deadline. charts.js renderTimeline draws
4
5
  it from the payload embedded once below; the attempts table under it is
5
6
  the full text alternative. %>
6
7
  <% timeline = @workflow[:timeline] %>
@@ -24,6 +25,15 @@
24
25
  <% end %>
25
26
  </ul>
26
27
  <% end %>
28
+ <%# The missed wait deadlines as text: the chart's dashed lines (issue #565). %>
29
+ <% if timeline[:wait_timeouts].any? %>
30
+ <ul class="mt-2 space-y-1 text-sm text-gray-700 dark:text-gray-300" data-testid="workflow-wait-timeouts">
31
+ <% timeline[:wait_timeouts].each do |mark| %>
32
+ <li><%= t("pgbus.workflows.timeline.wait_timeout_html", step: tag.code(mark[:step], class: "font-mono"),
33
+ time: pgbus_absolute_time(Time.at(mark[:at] / 1000.0)), action: tag.code(mark[:action], class: "font-mono")) %></li>
34
+ <% end %>
35
+ </ul>
36
+ <% end %>
27
37
  <%# json_escape keeps a value from closing the tag; a data block needs no nonce. %>
28
38
  <%= tag.script(json_escape(timeline.to_json).html_safe, type: "application/json", id: "pgbus-workflow-timeline-data") %> <%# herb:disable erb-no-unsafe-raw %>
29
39
  <script type="module" nonce="<%= content_security_policy_nonce %>">