pgbus 0.19.0 → 0.19.1

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 (57) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +323 -147
  3. data/README.md +44 -0
  4. data/app/controllers/pgbus/deploy_tasks_controller.rb +38 -0
  5. data/app/helpers/pgbus/deploy_tasks_helper.rb +77 -0
  6. data/app/helpers/pgbus/processes_helper.rb +1 -1
  7. data/app/models/pgbus/deploy_task.rb +104 -0
  8. data/app/models/pgbus/deploy_task_run.rb +45 -0
  9. data/app/views/layouts/pgbus/application.html.erb +2 -0
  10. data/app/views/pgbus/deploy_tasks/_rollover_banner.html.erb +25 -0
  11. data/app/views/pgbus/deploy_tasks/_tasks_table.html.erb +71 -0
  12. data/app/views/pgbus/deploy_tasks/index.html.erb +6 -0
  13. data/app/views/pgbus/deploy_tasks/show.html.erb +134 -0
  14. data/config/i18n-tasks.yml +2 -0
  15. data/config/locales/da.yml +97 -0
  16. data/config/locales/de.yml +97 -0
  17. data/config/locales/en.yml +97 -0
  18. data/config/locales/es.yml +97 -0
  19. data/config/locales/fi.yml +97 -0
  20. data/config/locales/fr.yml +97 -0
  21. data/config/locales/it.yml +97 -0
  22. data/config/locales/ja.yml +97 -0
  23. data/config/locales/nb.yml +97 -0
  24. data/config/locales/nl.yml +97 -0
  25. data/config/locales/pt.yml +97 -0
  26. data/config/locales/sv.yml +97 -0
  27. data/config/routes.rb +7 -0
  28. data/lib/generators/pgbus/add_deploy_tasks_generator.rb +51 -0
  29. data/lib/generators/pgbus/templates/add_deploy_tasks_tables.rb.erb +43 -0
  30. data/lib/generators/pgbus/templates/deploy_tasks.yml.erb +42 -0
  31. data/lib/generators/pgbus/templates/migration.rb.erb +43 -0
  32. data/lib/pgbus/configuration/deploy_settings.rb +46 -0
  33. data/lib/pgbus/configuration.rb +3 -0
  34. data/lib/pgbus/deploy/revision.rb +59 -0
  35. data/lib/pgbus/deploy/rollover.rb +65 -0
  36. data/lib/pgbus/deploy/runner.rb +259 -0
  37. data/lib/pgbus/deploy/task.rb +105 -0
  38. data/lib/pgbus/engine.rb +17 -0
  39. data/lib/pgbus/generators/migration_detector.rb +11 -0
  40. data/lib/pgbus/instrumentation.rb +3 -0
  41. data/lib/pgbus/mcp/server.rb +2 -1
  42. data/lib/pgbus/mcp/tools/deploy_tasks_tool.rb +86 -0
  43. data/lib/pgbus/mcp.rb +1 -0
  44. data/lib/pgbus/outbox/poller.rb +1 -0
  45. data/lib/pgbus/process/consumer.rb +1 -0
  46. data/lib/pgbus/process/dispatcher.rb +18 -1
  47. data/lib/pgbus/process/heartbeat.rb +7 -2
  48. data/lib/pgbus/process/supervisor.rb +35 -3
  49. data/lib/pgbus/process/worker.rb +1 -0
  50. data/lib/pgbus/recurring/scheduler.rb +54 -20
  51. data/lib/pgbus/version.rb +1 -1
  52. data/lib/pgbus/web/data_source/deploy_task_list.rb +114 -0
  53. data/lib/pgbus/web/data_source/list_counts.rb +2 -1
  54. data/lib/pgbus/web/data_source.rb +1 -0
  55. data/lib/pgbus/web/deploy_task_state.rb +51 -0
  56. data/sig/pgbus/configuration.rbs +10 -0
  57. metadata +20 -1
data/README.md CHANGED
@@ -1217,6 +1217,7 @@ All tools are read-only — no tool mutates state, and there is no raw-SQL passt
1217
1217
  | `pgbus_dlq` / `pgbus_dlq_detail` | Dead-letter inspection. Paginated. |
1218
1218
  | `pgbus_locks` | Active uniqueness locks (the leaked-lock diagnostic). Paginated (`page`, `per_page` max 100; returns `total`, capped at 10,000, and `has_more`). |
1219
1219
  | `pgbus_throughput` / `pgbus_stats` | Recent throughput time series and status counts. |
1220
+ | `pgbus_deploy_tasks` | Deploy tasks, their state and the rollover status (read-only). |
1220
1221
  | `pgbus_recurring` | Recurring task schedule + last/next run times. All tasks unless `page` / `per_page` is given (then `total` is capped at 10,000); returns `total`, `has_more`. |
1221
1222
 
1222
1223
  #### Security
@@ -2216,6 +2217,42 @@ pgbus start --workers-only --capsule default
2216
2217
 
2217
2218
  The capsule name is the `:name` you passed to `c.capsule` in your initializer (or the first queue token when using the string DSL).
2218
2219
 
2220
+ ### Deploy tasks
2221
+
2222
+ One-time work owed by a release (a backfill, a data fix, a cache warm). A deploy task runs once per `(key, version)`, only after every live pgbus process reports the new app revision and at least one worker on that revision is alive. A scheduled run never happens on old code during the deploy overlap window (Run now, below, is deliberately ungated), two schedulers (multi-host, overlap) never both fire it, and an owed task is not forgotten. It is not exactly-once: a failure between the enqueue and recording the run can repeat it after a restart, so keep deploy tasks idempotent.
2223
+
2224
+ ```yaml
2225
+ # config/deploy_tasks.yml
2226
+ production:
2227
+ backfill_order_totals:
2228
+ class: BackfillOrderTotalsJob # or command: "Orders.backfill_totals!"
2229
+ version: 2 # bump to run again after the next rollover (default 1)
2230
+ queue: maintenance
2231
+ args: [{ batch_size: 500 }]
2232
+ description: Recompute totals after the currency migration
2233
+ warm_fragment_caches:
2234
+ command: "FragmentCacheWarmer.run"
2235
+ every_deploy: true # runs after every rollover, dedup'd on the app revision
2236
+ ```
2237
+
2238
+ The file has the shape of `config/recurring.yml` minus `schedule:`, plus `version:` and `every_deploy:`. It is environment-scoped or flat, ERB is allowed, and `config.deploy_tasks_files` lists several files (explicit files replace the default). A task is a `class:` (an ActiveJob class; anything else is skipped with a warning at scheduler boot) or a `command:` (`Const.method`, run by `Pgbus::Recurring::CommandJob`), with optional `queue`, `args`, `priority` and `description`.
2239
+
2240
+ - **`version:`** (default `"1"`, compared as a string): bump it in the same commit as a new data fix to run the task again after that deploy.
2241
+ - **`every_deploy: true`**: runs after every rollover, dedup'd on the revision, so a rollback deploy re-fires it and a scheduler crash-restart does not. `version` is ignored (a warning is logged).
2242
+ - **Revision**, first non-blank wins: `config.app_revision` (a String or a callable) → `ENV["PGBUS_REVISION"]` → `ENV["DASH_VERSION"]` → `ENV["KAMAL_VERSION"]` → the `REVISION` file in `Rails.root` (Capistrano). dash and Kamal need no configuration. Every pgbus process stamps it into its heartbeat row (`metadata["revision"]`, shown in the Processes page's expanded row). Without a revision the gate stays closed: tasks wait, the scheduler warns once at boot, and only Run now enqueues.
2243
+ - **The gate** (checked by the scheduler at most every 5 s): live processes are those with a heartbeat in the last 5 minutes. It is closed while any live process reports another revision (a process with no revision, such as a pre-upgrade pgbus, counts as lagging) or while no live worker runs the new revision. A gracefully stopped process deletes its row at once, so a clean dash/Kamal deploy opens the gate within a scheduler tick after the last old container drains; a hard-killed old container keeps it closed until its row goes stale (5 minutes).
2244
+ - **Who runs it**: the scheduler that runs recurring tasks. The supervisor forks it when recurring or deploy tasks are configured (`config.deploy_tasks_enabled`, default `true`); `--scheduler-only` works as before. At boot it syncs the declared tasks into `pgbus_deploy_tasks` and arms the owed ones. A compare-and-set claim on the row means two schedulers never both fire.
2245
+ - **"Ran" means enqueued**, like recurring: the task goes through ActiveJob `perform_later`, so uniqueness, batches, concurrency, priority, retries and the DLQ apply, and the outcome is on the Jobs page. A failed enqueue re-arms the task, logs it, reports it to the error reporters and retries after 5 s, doubling up to 5 minutes (Disable the task to stop it). The run row marks a version as done: if it cannot be written right after a successful enqueue, the next scheduler boot runs the task again, so keep deploy tasks idempotent. Due tasks are enqueued in key order with no completion ordering between them.
2246
+ - **Dashboard**: the "Deploy tasks" page (`/pgbus/deploy_tasks`, plus a show page per task with its configuration and last 25 runs) shows whether the fleet has rolled over to the scheduler's revision, with live process counts per revision, and each task as ran, waiting (and why), due, disabled or never ran. **Run now** enqueues immediately, with no gate and no claim, and records a manual run; it does not cancel a pending rollover run (Disable the task for that). **Enable/Disable** is honoured on every tick.
2247
+ - **History**: `pgbus_deploy_task_runs`, kept for `config.deploy_task_run_retention` (default 90 days, `nil` keeps forever); the dispatcher prunes hourly. Each enqueue emits `pgbus.deploy_task.enqueue` (`task`, `version`, `revision`, `triggered_by`).
2248
+
2249
+ ```bash
2250
+ rails generate pgbus:add_deploy_tasks # migration + config/deploy_tasks.yml
2251
+ rails generate pgbus:add_deploy_tasks --database=pgbus # for a separate database
2252
+ ```
2253
+
2254
+ `rails generate pgbus:update` detects the missing tables; fresh installs (`pgbus:install`) already have them.
2255
+
2219
2256
  ### Dashboard
2220
2257
 
2221
2258
  The dashboard is a mountable Rails engine at `/pgbus` with:
@@ -2290,6 +2327,8 @@ Pgbus uses these tables (created via PGMQ and migrations):
2290
2327
  | `pgbus_outbox_entries` | Transactional outbox entries pending publication |
2291
2328
  | `pgbus_recurring_tasks` | Recurring job definitions |
2292
2329
  | `pgbus_recurring_executions` | Recurring job execution history |
2330
+ | `pgbus_deploy_tasks` | Declared deploy tasks, their arm (`armed_at`/`armed_for`) and `enabled` flag |
2331
+ | `pgbus_deploy_task_runs` | Deploy task run history (task_key, version, revision, triggered_by, queue_name, msg_id, run_at) |
2293
2332
  | `pgbus_presence_members` | Stream presence tracking (who is subscribed) |
2294
2333
  | `pgbus_stream_stats` | Stream broadcast/connect/disconnect metrics (opt-in) |
2295
2334
 
@@ -2388,6 +2427,11 @@ Curated headline options for the README. The full operator reference (with types
2388
2427
  | `outbox_batch_size` | `100` | Max entries per outbox poll cycle |
2389
2428
  | `outbox_retention` | `1.day` | How long to keep published outbox entries. Accepts seconds, Duration, or `nil` to disable cleanup |
2390
2429
  | `idempotency_ttl` | `7.days` | How long to keep processed event records. Accepts seconds, Duration, or `nil` to disable cleanup |
2430
+ | `app_revision` | `nil` | App revision for deploy tasks: a String or a callable returning one. Falls back to `PGBUS_REVISION`, `DASH_VERSION`, `KAMAL_VERSION`, then the `REVISION` file |
2431
+ | `deploy_tasks` | `nil` | Hash of deploy tasks, same shape as `config/deploy_tasks.yml` |
2432
+ | `deploy_tasks_files` | `nil` | Array of deploy-task YAML files (replaces the default `config/deploy_tasks.yml`) |
2433
+ | `deploy_tasks_enabled` | `true` | Fork the scheduler for deploy tasks; set `false` to turn them off |
2434
+ | `deploy_task_run_retention` | `90.days` | How long to keep deploy task run history. Accepts seconds, Duration, or `nil` to keep forever |
2391
2435
  | `allowed_global_id_models` | `nil` | Allowlist of Class/Module models permitted as GlobalID **job arguments** and EventBus payloads. `nil` = allow-all (default). `[]` = deny-all. Apps with ActiveStorage should include `ActiveStorage::Blob` (and related models). |
2392
2436
  | `base_controller_class` | `"::ActionController::Base"` | Base class for dashboard controllers (string, constantized at load time) |
2393
2437
  | `return_to_app_url` | `nil` | URL for "back to app" button in dashboard nav (nil hides the button) |
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pgbus
4
+ class DeployTasksController < ApplicationController
5
+ def index
6
+ @page = page_param
7
+ @per_page = per_page
8
+ @deploy_tasks = data_source.deploy_tasks(page: @page, per_page: @per_page)
9
+ @deploy_tasks_count = data_source.list_count(:deploy_tasks)
10
+ @rollover = data_source.deploy_rollover
11
+ render_frame("pgbus/deploy_tasks/tasks_table") if params[:frame] == "deploy_tasks"
12
+ end
13
+
14
+ def show
15
+ @task = data_source.deploy_task(params[:id])
16
+ return redirect_to pgbus.deploy_tasks_path, alert: t("pgbus.deploy_tasks.not_found") unless @task
17
+
18
+ @rollover = data_source.deploy_rollover
19
+ end
20
+
21
+ def toggle
22
+ result = data_source.toggle_deploy_task(params[:id])
23
+ if result
24
+ redirect_to pgbus.deploy_tasks_path, notice: t("pgbus.deploy_tasks.toggle.#{result}")
25
+ else
26
+ redirect_to pgbus.deploy_tasks_path, alert: t("pgbus.deploy_tasks.toggle.failed")
27
+ end
28
+ end
29
+
30
+ def enqueue
31
+ if data_source.enqueue_deploy_task_now(params[:id]) == :enqueued
32
+ redirect_to pgbus.deploy_tasks_path, notice: t("pgbus.deploy_tasks.enqueue.enqueued")
33
+ else
34
+ redirect_to pgbus.deploy_tasks_path, alert: t("pgbus.deploy_tasks.enqueue.failed")
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pgbus
4
+ # The Deploy tasks page's words (issue #536): DeployTaskState results and
5
+ # the rollover banner as badges and sentences. Moments go through
6
+ # pgbus_time; the _html keys let the <time> and <code> through, translate
7
+ # escapes every other argument.
8
+ module DeployTasksHelper
9
+ BANNER_TONE_CSS = {
10
+ green: "bg-green-50 text-green-800 ring-green-200 dark:bg-green-900/30 dark:text-green-200 dark:ring-green-800",
11
+ yellow: "bg-yellow-50 text-yellow-800 ring-yellow-200 dark:bg-yellow-900/30 dark:text-yellow-200 dark:ring-yellow-800",
12
+ red: "bg-red-50 text-red-800 ring-red-200 dark:bg-red-900/30 dark:text-red-200 dark:ring-red-800",
13
+ gray: "bg-gray-50 text-gray-700 ring-gray-200 dark:bg-gray-900/40 dark:text-gray-200 dark:ring-gray-700"
14
+ }.freeze
15
+
16
+ def pgbus_deploy_state_badge(result)
17
+ tag.span(t("pgbus.deploy_tasks.states.#{result.state}"),
18
+ class: "#{ApplicationHelper::BATCH_BADGE_BASE} #{ApplicationHelper::JOB_STATE_BADGE_CSS[result.badge_tone]}")
19
+ end
20
+
21
+ # "Waiting for rollover: 2 processes are not on abc123 yet. · ran 1m ago (manual)"
22
+ def pgbus_deploy_task_reason(result)
23
+ reason = translate("pgbus.deploy_tasks.reasons.#{result.reason_key}_html", **pgbus_reason_args(result.reason_args))
24
+ return reason unless result.manual_args
25
+
26
+ safe_join([reason, " ", translate("pgbus.deploy_tasks.reasons.manual_html", **pgbus_reason_args(result.manual_args))])
27
+ end
28
+
29
+ def pgbus_deploy_rollover_key(rollover)
30
+ return "unavailable" unless rollover
31
+ return "no_scheduler" unless rollover[:scheduler]
32
+
33
+ rollover[:reason].to_s
34
+ end
35
+
36
+ def pgbus_deploy_rollover_tone(rollover)
37
+ case pgbus_deploy_rollover_key(rollover)
38
+ when "ready" then :green
39
+ when "no_scheduler", "unavailable" then :red
40
+ when "no_revision" then :gray
41
+ else :yellow
42
+ end
43
+ end
44
+
45
+ def pgbus_deploy_rollover_sentence(rollover)
46
+ key = pgbus_deploy_rollover_key(rollover)
47
+ args = { revision: rollover&.dig(:revision) }
48
+ args[:count] = rollover[:lagging].size if key == "lagging"
49
+ translate("pgbus.deploy_tasks.banner.#{key}_html", **args)
50
+ end
51
+
52
+ def pgbus_deploy_lagging_process(process)
53
+ t("pgbus.deploy_tasks.banner.lagging_process",
54
+ kind: process[:kind], hostname: process[:hostname], pid: process[:pid],
55
+ revision: process[:revision] || t("pgbus.deploy_tasks.banner.no_revision_label"))
56
+ end
57
+
58
+ # "6 on 8d41b07 · 2 on 3f2a9c1", the most common revision first.
59
+ def pgbus_deploy_live_counts(rollover)
60
+ counts = rollover&.dig(:live_by_revision) || {}
61
+ parts = counts.sort_by { |revision, count| [-count, revision.to_s] }.map do |revision, count|
62
+ t("pgbus.deploy_tasks.banner.live_count", count: count,
63
+ revision: revision || t("pgbus.deploy_tasks.banner.no_revision_label"))
64
+ end
65
+ safe_join(parts, ProcessesHelper::PART_SEPARATOR)
66
+ end
67
+
68
+ def pgbus_deploy_version(task)
69
+ task[:every_deploy] ? t("pgbus.deploy_tasks.every_deploy") : t("pgbus.deploy_tasks.version", version: task[:version])
70
+ end
71
+
72
+ # The newer of the last rollover and the last manual run.
73
+ def pgbus_deploy_last_run(task)
74
+ [task[:last_rollover_run], task[:last_manual_run]].compact.max_by { |run| Web::JobState.time(run[:run_at]).to_f }
75
+ end
76
+ end
77
+ end
@@ -51,7 +51,7 @@ module Pgbus
51
51
  def pgbus_process_raw_metadata(metadata)
52
52
  return {} unless metadata.is_a?(Hash)
53
53
 
54
- metadata.reject { |k, _| RENDERED_METADATA_KEYS.include?(k.to_s) }
54
+ metadata.reject { |k, v| v.nil? || RENDERED_METADATA_KEYS.include?(k.to_s) }
55
55
  end
56
56
 
57
57
  private
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pgbus
4
+ # A declared deploy task and its pending arm (issue #536). armed_at/armed_for
5
+ # is a row-level compare-and-set: sync arms an owed task, the scheduler's
6
+ # claim! clears the arm before it enqueues, and a failed enqueue re-arms it.
7
+ class DeployTask < BusRecord
8
+ self.table_name = "pgbus_deploy_tasks"
9
+
10
+ validates :key, presence: true, uniqueness: true
11
+ validates :version, presence: true
12
+
13
+ scope :static_tasks, -> { where(static: true) }
14
+ scope :enabled, -> { where(enabled: true) }
15
+ scope :armed, -> { where.not(armed_at: nil) }
16
+
17
+ # Any enabled task waiting to fire: the runner's cheap per-tick check.
18
+ def self.pending?
19
+ enabled.armed.exists?
20
+ end
21
+
22
+ # Upserts every declared task (valid Deploy::Task objects) and arms the
23
+ # owed ones, then removes static rows no longer declared. Never disarms
24
+ # and never touches enabled; an arm already held keeps its armed_at and
25
+ # takes the new armed_for (a later bump supersedes an unfired one).
26
+ def self.sync_from_config!(tasks, revision:, now: Time.current)
27
+ transaction do
28
+ tasks.each { |task| sync_task!(task, revision: revision, now: now) }
29
+ static_tasks.where.not(key: tasks.map(&:key)).delete_all
30
+ end
31
+ end
32
+
33
+ # Owed-ness comes from the rollover runs, never from the stored row: any
34
+ # scheduler that boots syncs, including an old-revision one.
35
+ def self.owed?(task, revision)
36
+ return !DeployTaskRun.rollover_run_for?(task.key, task.version) unless task.every_deploy
37
+ return false if revision.nil?
38
+
39
+ DeployTaskRun.last_rollover_for(task.key)&.revision != revision
40
+ end
41
+
42
+ def self.sync_task!(task, revision:, now:)
43
+ record = find_or_initialize_by(key: task.key)
44
+ record.assign_attributes(
45
+ class_name: task.class_name, command: task.command, arguments: JSON.generate(task.arguments),
46
+ queue_name: task.queue_name, priority: task.priority, version: task.version,
47
+ every_deploy: task.every_deploy, description: task.description, static: true
48
+ )
49
+ if owed?(task, revision)
50
+ record.armed_at ||= now
51
+ record.armed_for = task.armed_for(revision)
52
+ end
53
+ record.save!
54
+ end
55
+ private_class_method :sync_task!
56
+
57
+ # true when this caller took the arm; false when another scheduler took
58
+ # it first or the arm is for another version or revision.
59
+ def claim!(armed_for:, now: Time.current) # rubocop:disable Naming/PredicateMethod -- compare-and-set that reports whether it won
60
+ self.class.armed.where(id: id, armed_for: armed_for).update_all(armed_at: nil, updated_at: now) == 1
61
+ end
62
+
63
+ # Re-stamps a held arm for +armed_for+, if it is still armed for what
64
+ # this row was read with. The runner does this when its gate is open on
65
+ # its own revision and its own version is owed: the other arm belongs to
66
+ # code no live process runs.
67
+ def take_over!(armed_for:, now: Time.current) # rubocop:disable Naming/PredicateMethod -- compare-and-set, like #claim!
68
+ self.class.armed.where(id: id, armed_for: self.armed_for).update_all(armed_for: armed_for, updated_at: now) == 1
69
+ end
70
+
71
+ # Restores this caller's claimed arm after a failed enqueue. Compare-and-set:
72
+ # only while the arm is still cleared and still for +armed_for+, so a
73
+ # failed enqueue never overwrites an arm another scheduler set meanwhile.
74
+ def rearm!(armed_for:, now: Time.current)
75
+ self.class.where(id: id, armed_at: nil, armed_for: armed_for).update_all(armed_at: now, updated_at: now)
76
+ end
77
+
78
+ def armed?
79
+ !armed_at.nil?
80
+ end
81
+
82
+ # For display: a corrupt value shows as no arguments.
83
+ def parsed_arguments
84
+ decoded_arguments
85
+ rescue JSON::ParserError
86
+ []
87
+ end
88
+
89
+ # For Run now: a corrupt value raises (the enqueue reports :failed and
90
+ # logs it) rather than running the job without its arguments.
91
+ def to_task
92
+ Deploy::Task.new(
93
+ key: key, class_name: class_name, command: command, version: version, every_deploy: every_deploy,
94
+ queue_name: queue_name, arguments: decoded_arguments, priority: priority, description: description
95
+ )
96
+ end
97
+
98
+ private
99
+
100
+ def decoded_arguments
101
+ arguments.blank? ? [] : Array(JSON.parse(arguments))
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pgbus
4
+ # One enqueue of a deploy task (issue #536): by the scheduler after a
5
+ # rollover, or by "Run now" on the dashboard. Rollover runs are what
6
+ # DeployTask.owed? reads; manual runs never satisfy an obligation.
7
+ class DeployTaskRun < BusRecord
8
+ self.table_name = "pgbus_deploy_task_runs"
9
+
10
+ ROLLOVER = "rollover"
11
+ MANUAL = "manual"
12
+
13
+ validates :task_key, :version, :triggered_by, :run_at, presence: true
14
+
15
+ scope :for_task, ->(key) { where(task_key: key) }
16
+ scope :rollover, -> { where(triggered_by: ROLLOVER) }
17
+ scope :manual, -> { where(triggered_by: MANUAL) }
18
+ scope :recent, ->(limit = 25) { order(id: :desc).limit(limit) }
19
+ scope :older_than, ->(time) { where(run_at: ...time) }
20
+
21
+ # Runs the dispatcher may prune: older than +time+, except the newest
22
+ # rollover run per (task_key, version). That run is what marks a version
23
+ # done (DeployTask.owed?); pruning it would re-arm the task on the next
24
+ # scheduler boot and run it again.
25
+ def self.prunable_before(time)
26
+ older_than(time).where.not(id: rollover.group(:task_key, :version).select("MAX(id)"))
27
+ end
28
+
29
+ def self.last_rollover_for(key)
30
+ rollover.for_task(key).order(id: :desc).first
31
+ end
32
+
33
+ # The newest run per (task_key, triggered_by) for +keys+: the dashboard's
34
+ # "last rollover run" and "last manual run" columns, in one query.
35
+ def self.latest_for(keys)
36
+ where(task_key: keys)
37
+ .select("DISTINCT ON (task_key, triggered_by) #{quoted_table_name}.*")
38
+ .order(:task_key, :triggered_by, id: :desc)
39
+ end
40
+
41
+ def self.rollover_run_for?(key, version)
42
+ rollover.for_task(key).exists?(version: version.to_s)
43
+ end
44
+ end
45
+ end
@@ -126,6 +126,7 @@
126
126
  <%= pgbus_nav_link t("pgbus.layout.nav.queues"), pgbus.queues_path %>
127
127
  <%= pgbus_nav_link t("pgbus.layout.nav.jobs"), pgbus.jobs_path %>
128
128
  <%= pgbus_nav_link t("pgbus.layout.nav.recurring"), pgbus.recurring_tasks_path %>
129
+ <%= pgbus_nav_link t("pgbus.layout.nav.deploy_tasks"), pgbus.deploy_tasks_path %>
129
130
  <%= pgbus_nav_link t("pgbus.layout.nav.processes"), pgbus.processes_path %>
130
131
  <%= pgbus_nav_link t("pgbus.layout.nav.events"), pgbus.events_path %>
131
132
  <%= pgbus_nav_link t("pgbus.layout.nav.batches"), pgbus.batches_path %>
@@ -186,6 +187,7 @@
186
187
  <%= pgbus_mobile_nav_link t("pgbus.layout.nav.queues"), pgbus.queues_path %>
187
188
  <%= pgbus_mobile_nav_link t("pgbus.layout.nav.jobs"), pgbus.jobs_path %>
188
189
  <%= pgbus_mobile_nav_link t("pgbus.layout.nav.recurring"), pgbus.recurring_tasks_path %>
190
+ <%= pgbus_mobile_nav_link t("pgbus.layout.nav.deploy_tasks"), pgbus.deploy_tasks_path %>
189
191
  <%= pgbus_mobile_nav_link t("pgbus.layout.nav.processes"), pgbus.processes_path %>
190
192
  <%= pgbus_mobile_nav_link t("pgbus.layout.nav.events"), pgbus.events_path %>
191
193
  <%= pgbus_mobile_nav_link t("pgbus.layout.nav.batches"), pgbus.batches_path %>
@@ -0,0 +1,25 @@
1
+ <%# The rollover the scheduler acts on (DataSource#deploy_rollover): the same
2
+ reason every waiting task's sentence names. %>
3
+ <% key = pgbus_deploy_rollover_key(@rollover) %>
4
+ <% lagging = Array(@rollover&.dig(:lagging)) %>
5
+ <section aria-labelledby="deploy-rollover-heading" data-rollover="<%= key %>"
6
+ class="mb-6 rounded-lg p-4 ring-1 <%= Pgbus::DeployTasksHelper::BANNER_TONE_CSS.fetch(pgbus_deploy_rollover_tone(@rollover)) %>">
7
+ <h2 id="deploy-rollover-heading" class="text-sm font-semibold"><%= t("pgbus.deploy_tasks.banner.heading") %></h2>
8
+ <p class="mt-1 text-sm"><%= pgbus_deploy_rollover_sentence(@rollover) %></p>
9
+ <% if key == "no_revision" %>
10
+ <p class="mt-1 text-sm"><%= t("pgbus.deploy_tasks.banner.no_revision_hint_html") %></p>
11
+ <% end %>
12
+ <% if lagging.any? %>
13
+ <ul class="mt-2 list-disc pl-5 text-sm">
14
+ <% lagging.first(10).each do |process| %>
15
+ <li><%= pgbus_deploy_lagging_process(process) %></li>
16
+ <% end %>
17
+ <% if lagging.size > 10 %>
18
+ <li><%= t("pgbus.deploy_tasks.banner.lagging_more", count: lagging.size - 10) %></li>
19
+ <% end %>
20
+ </ul>
21
+ <% end %>
22
+ <% if @rollover&.dig(:live_by_revision).present? %>
23
+ <p class="mt-2 text-xs"><%= t("pgbus.deploy_tasks.banner.live_heading") %> <%= pgbus_deploy_live_counts(@rollover) %></p>
24
+ <% end %>
25
+ </section>
@@ -0,0 +1,71 @@
1
+ <turbo-frame id="deploy-tasks" data-auto-refresh data-turbo-action="advance"
2
+ data-src="<%= pgbus.deploy_tasks_path(request.query_parameters.merge(frame: "deploy_tasks")) %>">
3
+ <%= render "pgbus/deploy_tasks/rollover_banner" %>
4
+ <div class="rounded-lg bg-white dark:bg-gray-800 shadow ring-1 ring-gray-200 dark:ring-gray-700">
5
+ <table class="pgbus-table min-w-full divide-y divide-gray-200 dark:divide-gray-700">
6
+ <thead class="bg-gray-50 dark:bg-gray-900">
7
+ <tr>
8
+ <th class="px-4 py-3 text-left text-xs font-medium uppercase text-gray-500 dark:text-gray-400"><%= t("pgbus.deploy_tasks.tasks_table.headers.task") %></th>
9
+ <th class="px-4 py-3 text-left text-xs font-medium uppercase text-gray-500 dark:text-gray-400"><%= t("pgbus.deploy_tasks.tasks_table.headers.version") %></th>
10
+ <th class="px-4 py-3 text-left text-xs font-medium uppercase text-gray-500 dark:text-gray-400"><%= t("pgbus.deploy_tasks.tasks_table.headers.queue") %></th>
11
+ <th class="px-4 py-3 text-left text-xs font-medium uppercase text-gray-500 dark:text-gray-400"><%= t("pgbus.deploy_tasks.tasks_table.headers.last_run") %></th>
12
+ <th class="px-4 py-3 text-left text-xs font-medium uppercase text-gray-500 dark:text-gray-400"><%= t("pgbus.deploy_tasks.tasks_table.headers.state") %></th>
13
+ <th class="px-4 py-3 text-right text-xs font-medium uppercase text-gray-500 dark:text-gray-400"><%= t("pgbus.deploy_tasks.tasks_table.headers.actions") %></th>
14
+ </tr>
15
+ </thead>
16
+ <tbody class="divide-y divide-gray-100 dark:divide-gray-700">
17
+ <% @deploy_tasks.each do |task| %>
18
+ <% result = Pgbus::Web::DeployTaskState.present(task, @rollover) %>
19
+ <% last_run = pgbus_deploy_last_run(task) %>
20
+ <tr data-state="<%= result.state %>" class="hover:bg-gray-50 dark:hover:bg-gray-700/50">
21
+ <td data-label="<%= t("pgbus.deploy_tasks.tasks_table.headers.task") %>" class="px-4 py-3">
22
+ <div>
23
+ <%= pgbus_link_to task[:key], pgbus.deploy_task_path(task[:id]), data: { turbo_frame: "_top" } %>
24
+ </div>
25
+ <div class="text-xs text-gray-500 dark:text-gray-400"><%= task[:class_name] || task[:command]&.truncate(40) %></div>
26
+ <% if task[:description] %>
27
+ <div class="text-xs text-gray-500 dark:text-gray-400 mt-0.5"><%= task[:description] %></div>
28
+ <% end %>
29
+ </td>
30
+ <td data-label="<%= t("pgbus.deploy_tasks.tasks_table.headers.version") %>" class="px-4 py-3 text-sm text-gray-900 dark:text-white">
31
+ <%= pgbus_deploy_version(task) %>
32
+ </td>
33
+ <td data-label="<%= t("pgbus.deploy_tasks.tasks_table.headers.queue") %>" class="px-4 py-3 text-sm text-gray-600 dark:text-gray-300">
34
+ <%= task[:queue_name] || t("pgbus.deploy_tasks.default_queue") %>
35
+ </td>
36
+ <td data-label="<%= t("pgbus.deploy_tasks.tasks_table.headers.last_run") %>" class="px-4 py-3 text-sm text-gray-600 dark:text-gray-300">
37
+ <% if last_run %>
38
+ <%= pgbus_time(last_run[:run_at]) %>
39
+ <span class="block text-xs text-gray-500 dark:text-gray-400"><%= t("pgbus.deploy_tasks.triggered_by.#{last_run[:triggered_by]}") %></span>
40
+ <% else %>
41
+ <%= t("pgbus.deploy_tasks.tasks_table.never") %>
42
+ <% end %>
43
+ </td>
44
+ <td data-label="<%= t("pgbus.deploy_tasks.tasks_table.headers.state") %>" class="px-4 py-3">
45
+ <%= pgbus_deploy_state_badge(result) %>
46
+ <p class="mt-1 text-xs text-gray-600 dark:text-gray-300"><%= pgbus_deploy_task_reason(result) %></p>
47
+ </td>
48
+ <td data-label="<%= t("pgbus.deploy_tasks.tasks_table.headers.actions") %>" class="px-4 py-3 text-right space-x-1">
49
+ <%= pgbus_button_to task[:enabled] ? t("pgbus.deploy_tasks.disable") : t("pgbus.deploy_tasks.enable"),
50
+ pgbus.toggle_deploy_task_path(task[:id]),
51
+ variant: task[:enabled] ? :warning : :success, size: :sm, form: { data: { turbo_frame: "_top" } } %>
52
+ <% if task[:enabled] %>
53
+ <%= pgbus_button_to t("pgbus.deploy_tasks.run_now"), pgbus.enqueue_deploy_task_path(task[:id]),
54
+ variant: :primary, size: :sm, form: { data: { turbo_frame: "_top" } } %>
55
+ <% end %>
56
+ </td>
57
+ </tr>
58
+ <% end %>
59
+ <% if @deploy_tasks.empty? %>
60
+ <%= render "pgbus/shared/empty_row", colspan: 6,
61
+ message: safe_join([content_tag(:p, t("pgbus.deploy_tasks.tasks_table.empty_title"), class: "text-lg font-medium"),
62
+ content_tag(:p, t("pgbus.deploy_tasks.tasks_table.empty_hint_html"), class: "mt-1")]) %>
63
+ <% end %>
64
+ </tbody>
65
+ </table>
66
+ <%= render "pgbus/shared/pager", page: @page, per_page: @per_page, shown: @deploy_tasks.size,
67
+ total_count: @deploy_tasks_count.total,
68
+ has_more: @deploy_tasks_count.capped? && @deploy_tasks.size == @per_page,
69
+ page_path: ->(n) { pgbus.deploy_tasks_path(page: n) } %>
70
+ </div>
71
+ </turbo-frame>
@@ -0,0 +1,6 @@
1
+ <div class="flex items-center justify-between mb-6">
2
+ <h1 class="text-2xl font-bold text-gray-900 dark:text-white"><%= t("pgbus.deploy_tasks.index.title") %></h1>
3
+ <span class="text-sm text-gray-500 dark:text-gray-400"><%= t("pgbus.deploy_tasks.index.task_count", count: @deploy_tasks_count.total) %></span>
4
+ </div>
5
+
6
+ <%= render "pgbus/deploy_tasks/tasks_table" %>
@@ -0,0 +1,134 @@
1
+ <% result = Pgbus::Web::DeployTaskState.present(@task, @rollover) %>
2
+ <% last_run = pgbus_deploy_last_run(@task) %>
3
+ <div class="mb-6">
4
+ <div class="flex items-center justify-between">
5
+ <div>
6
+ <h1 class="text-2xl font-bold text-gray-900 dark:text-white"><%= @task[:key] %></h1>
7
+ <p class="mt-1 text-sm text-gray-500 dark:text-gray-400"><%= @task[:class_name] || @task[:command] %></p>
8
+ </div>
9
+ <div class="flex space-x-2">
10
+ <%= pgbus_button_to @task[:enabled] ? t("pgbus.deploy_tasks.disable") : t("pgbus.deploy_tasks.enable"),
11
+ pgbus.toggle_deploy_task_path(@task[:id]),
12
+ variant: @task[:enabled] ? :warning : :success %>
13
+ <% if @task[:enabled] %>
14
+ <%= pgbus_button_to t("pgbus.deploy_tasks.run_now"), pgbus.enqueue_deploy_task_path(@task[:id]), variant: :primary %>
15
+ <% end %>
16
+ <%= pgbus_link_to t("pgbus.deploy_tasks.show.back"), pgbus.deploy_tasks_path, variant: :secondary %>
17
+ </div>
18
+ </div>
19
+ </div>
20
+
21
+ <div class="grid grid-cols-1 lg:grid-cols-3 gap-6 mb-8">
22
+ <div class="rounded-lg bg-white dark:bg-gray-800 p-5 shadow ring-1 ring-gray-200 dark:ring-gray-700">
23
+ <p class="text-sm font-medium text-gray-500 dark:text-gray-400"><%= t("pgbus.deploy_tasks.show.state") %></p>
24
+ <p class="mt-1"><%= pgbus_deploy_state_badge(result) %></p>
25
+ <p class="mt-1 text-xs text-gray-600 dark:text-gray-300"><%= pgbus_deploy_task_reason(result) %></p>
26
+ </div>
27
+
28
+ <div class="rounded-lg bg-white dark:bg-gray-800 p-5 shadow ring-1 ring-gray-200 dark:ring-gray-700">
29
+ <p class="text-sm font-medium text-gray-500 dark:text-gray-400"><%= t("pgbus.deploy_tasks.show.version") %></p>
30
+ <p class="mt-1 text-lg font-semibold text-gray-900 dark:text-white"><%= pgbus_deploy_version(@task) %></p>
31
+ </div>
32
+
33
+ <div class="rounded-lg bg-white dark:bg-gray-800 p-5 shadow ring-1 ring-gray-200 dark:ring-gray-700">
34
+ <p class="text-sm font-medium text-gray-500 dark:text-gray-400"><%= t("pgbus.deploy_tasks.show.last_run") %></p>
35
+ <% if last_run %>
36
+ <p class="mt-1 text-lg font-semibold text-gray-900 dark:text-white"><%= pgbus_time(last_run[:run_at]) %></p>
37
+ <p class="text-xs text-gray-500 dark:text-gray-400"><%= pgbus_absolute_time(last_run[:run_at]) %> · <%= t("pgbus.deploy_tasks.triggered_by.#{last_run[:triggered_by]}") %></p>
38
+ <% else %>
39
+ <p class="mt-1 text-lg font-semibold text-gray-500 dark:text-gray-400"><%= t("pgbus.deploy_tasks.tasks_table.never") %></p>
40
+ <% end %>
41
+ </div>
42
+ </div>
43
+
44
+ <div class="rounded-lg bg-white dark:bg-gray-800 shadow ring-1 ring-gray-200 dark:ring-gray-700 mb-8">
45
+ <div class="px-4 py-3 border-b border-gray-200 dark:border-gray-700">
46
+ <h2 class="text-lg font-medium text-gray-900 dark:text-white"><%= t("pgbus.deploy_tasks.show.configuration") %></h2>
47
+ </div>
48
+ <div class="p-4">
49
+ <dl class="grid grid-cols-1 sm:grid-cols-2 gap-4">
50
+ <div>
51
+ <dt class="text-sm font-medium text-gray-500 dark:text-gray-400"><%= t("pgbus.deploy_tasks.show.labels.job_class") %></dt>
52
+ <dd class="mt-1 text-sm text-gray-900 dark:text-white"><%= @task[:class_name] || "—" %></dd>
53
+ </div>
54
+ <% if @task[:command] %>
55
+ <div>
56
+ <dt class="text-sm font-medium text-gray-500 dark:text-gray-400"><%= t("pgbus.deploy_tasks.show.labels.command") %></dt>
57
+ <dd class="mt-1 text-sm text-gray-900 dark:text-white font-mono text-xs"><%= @task[:command] %></dd>
58
+ </div>
59
+ <% end %>
60
+ <div>
61
+ <dt class="text-sm font-medium text-gray-500 dark:text-gray-400"><%= t("pgbus.deploy_tasks.show.labels.queue") %></dt>
62
+ <dd class="mt-1 text-sm text-gray-900 dark:text-white"><%= @task[:queue_name] || t("pgbus.deploy_tasks.default_queue") %></dd>
63
+ </div>
64
+ <div>
65
+ <dt class="text-sm font-medium text-gray-500 dark:text-gray-400"><%= t("pgbus.deploy_tasks.show.labels.priority") %></dt>
66
+ <dd class="mt-1 text-sm text-gray-900 dark:text-white"><%= @task[:priority] %></dd>
67
+ </div>
68
+ <% if @task[:armed_for] && @task[:armed_at] %>
69
+ <div>
70
+ <dt class="text-sm font-medium text-gray-500 dark:text-gray-400"><%= t("pgbus.deploy_tasks.show.labels.armed") %></dt>
71
+ <dd class="mt-1 text-sm text-gray-900 dark:text-white"><%= t("pgbus.deploy_tasks.show.armed_html", ago: pgbus_time(@task[:armed_at]),
72
+ armed_for: @task[:every_deploy] ? @task[:armed_for] : t("pgbus.deploy_tasks.version", version: @task[:armed_for])) %></dd>
73
+ </div>
74
+ <% end %>
75
+ <% if @task[:arguments]&.any? %>
76
+ <div class="sm:col-span-2">
77
+ <dt class="text-sm font-medium text-gray-500 dark:text-gray-400"><%= t("pgbus.deploy_tasks.show.labels.arguments") %></dt>
78
+ <dd class="mt-1 text-sm text-gray-900 dark:text-white font-mono text-xs"><%= @task[:arguments].inspect %></dd>
79
+ </div>
80
+ <% end %>
81
+ <% if @task[:description] %>
82
+ <div class="sm:col-span-2">
83
+ <dt class="text-sm font-medium text-gray-500 dark:text-gray-400"><%= t("pgbus.deploy_tasks.show.labels.description") %></dt>
84
+ <dd class="mt-1 text-sm text-gray-900 dark:text-white"><%= @task[:description] %></dd>
85
+ </div>
86
+ <% end %>
87
+ <div>
88
+ <dt class="text-sm font-medium text-gray-500 dark:text-gray-400"><%= t("pgbus.deploy_tasks.show.labels.source") %></dt>
89
+ <dd class="mt-1 text-sm text-gray-900 dark:text-white"><%= @task[:static] ? t("pgbus.deploy_tasks.show.source_config") : t("pgbus.deploy_tasks.show.source_dynamic") %></dd>
90
+ </div>
91
+ </dl>
92
+ </div>
93
+ </div>
94
+
95
+ <div class="rounded-lg bg-white dark:bg-gray-800 shadow ring-1 ring-gray-200 dark:ring-gray-700">
96
+ <div class="px-4 py-3 border-b border-gray-200 dark:border-gray-700">
97
+ <h2 class="text-lg font-medium text-gray-900 dark:text-white"><%= t("pgbus.deploy_tasks.show.recent_runs") %></h2>
98
+ </div>
99
+ <table class="pgbus-table min-w-full divide-y divide-gray-200 dark:divide-gray-700">
100
+ <thead class="bg-gray-50 dark:bg-gray-900">
101
+ <tr>
102
+ <th class="px-4 py-3 text-left text-xs font-medium uppercase text-gray-500 dark:text-gray-400"><%= t("pgbus.deploy_tasks.show.run_headers.triggered_by") %></th>
103
+ <th class="px-4 py-3 text-left text-xs font-medium uppercase text-gray-500 dark:text-gray-400"><%= t("pgbus.deploy_tasks.show.run_headers.version") %></th>
104
+ <th class="px-4 py-3 text-left text-xs font-medium uppercase text-gray-500 dark:text-gray-400"><%= t("pgbus.deploy_tasks.show.run_headers.revision") %></th>
105
+ <th class="px-4 py-3 text-left text-xs font-medium uppercase text-gray-500 dark:text-gray-400"><%= t("pgbus.deploy_tasks.show.run_headers.queue") %></th>
106
+ <th class="px-4 py-3 text-left text-xs font-medium uppercase text-gray-500 dark:text-gray-400"><%= t("pgbus.deploy_tasks.show.run_headers.enqueued_at") %></th>
107
+ </tr>
108
+ </thead>
109
+ <tbody class="divide-y divide-gray-100 dark:divide-gray-700">
110
+ <% Array(@task[:runs]).each do |run| %>
111
+ <tr class="hover:bg-gray-50 dark:hover:bg-gray-700/50">
112
+ <td data-label="<%= t("pgbus.deploy_tasks.show.run_headers.triggered_by") %>" class="px-4 py-3 text-sm text-gray-900 dark:text-white">
113
+ <%= t("pgbus.deploy_tasks.triggered_by.#{run[:triggered_by]}") %>
114
+ </td>
115
+ <td data-label="<%= t("pgbus.deploy_tasks.show.run_headers.version") %>" class="px-4 py-3 text-sm text-gray-600 dark:text-gray-300">
116
+ <%= t("pgbus.deploy_tasks.version", version: run[:version]) %>
117
+ </td>
118
+ <td data-label="<%= t("pgbus.deploy_tasks.show.run_headers.revision") %>" class="px-4 py-3 text-sm font-mono text-gray-600 dark:text-gray-300">
119
+ <%= run[:revision] || "—" %>
120
+ </td>
121
+ <td data-label="<%= t("pgbus.deploy_tasks.show.run_headers.queue") %>" class="px-4 py-3 text-sm text-gray-600 dark:text-gray-300">
122
+ <%= run[:queue_name] || "—" %>
123
+ </td>
124
+ <td data-label="<%= t("pgbus.deploy_tasks.show.run_headers.enqueued_at") %>" class="px-4 py-3 text-sm text-gray-600 dark:text-gray-300">
125
+ <%= pgbus_time(run[:run_at]) %>
126
+ </td>
127
+ </tr>
128
+ <% end %>
129
+ <% if Array(@task[:runs]).empty? %>
130
+ <%= render "pgbus/shared/empty_row", colspan: 5, message: t("pgbus.deploy_tasks.show.no_runs") %>
131
+ <% end %>
132
+ </tbody>
133
+ </table>
134
+ </div>
@@ -39,6 +39,8 @@ ignore_unused:
39
39
  - "pgbus.dashboard.queue_health.words.*"
40
40
  - "pgbus.insights.show.charts.series_name"
41
41
  - "pgbus.recurring_tasks.index.task_count.*"
42
+ - "pgbus.deploy_tasks.banner.lagging_html.*"
43
+ - "pgbus.deploy_tasks.reasons.waiting_lagging_html.*"
42
44
  - "pgbus.processes.state.activity.schedule.*"
43
45
  - "pgbus.processes.state.activity.slots.*"
44
46
  - "pgbus.processes.state.activity.threads.*"