module MaintenanceOrchestrator
Coordinates the maintenance lifecycle: materialises execution chains, pushes them through the job backend and hands the engine lifecycle to the supervisor.
Job-queue port and adapters used by the maintenance orchestrator.
Solid Queue adapter for the maintenance orchestrator.
Public Class Methods
Source
# File app/services/maintenance_orchestrator.rb, line 7 def self.backend @backend ||= SolidQueueBackend.new end
Returns the job-queue backend implementation (Solid Queue by default).
@return [MaintenanceOrchestrator::Backend] the backend
Source
# File app/services/maintenance_orchestrator.rb, line 151 def self.drain_engine backend.drain_engine end
Asks the backend to drain the engine.
# File app/services/maintenance_orchestrator.rb, line 158 def self.execute_maintenance(execution_history_id) backend.execute_maintenance(execution_history_id) end
Dispatches the maintenance chain of an execution via the backend.
@param execution_history_id [Integer] the execution to run
# File app/services/maintenance_orchestrator.rb, line 141 def self.retry_lock_acquisition(execution_history_id) backend.retry_lock_acquisition(execution_history_id) end
Retries acquiring the table lock for an execution via the backend.
@param execution_history_id [Integer] the execution to retry
# File app/services/maintenance_orchestrator.rb, line 173 def self.retry_maintenance(execution_history_id) backend.retry_maintenance(execution_history_id) end
Retries the maintenance of an execution via the backend.
@param execution_history_id [Integer] the execution to retry
# File app/services/maintenance_orchestrator.rb, line 166 def self.retry_pending_maintenance(execution_history_id) backend.retry_pending_maintenance(execution_history_id) end
Retries the maintenance chain shortly, for a dispatch consumed before the primary status: :running write was visible.
@param execution_history_id [Integer] the execution to retry
# File app/services/maintenance_orchestrator.rb, line 20 def self.run_plan(plan_id, at: Time.current, force: false) plan = MaintenancePlan.find(plan_id) # Idempotent: a table already queued or running does not queue again. # Without this, rapid "run now" clicks pile up N pending executions that # all resolve to "skipped" once the engine comes up. # Query by iceberg_table_id (not maintenance_plan_id) so two plans for the # same table never run concurrently. existing = ExecutionHistory.where(iceberg_table_id: plan.iceberg_table_id, status: %w[pending running]).first return existing if existing # A leftover lock from a finished execution must never block a new run. TableLock.reap_stale! execution, duplicate = create_execution(plan, at) return execution if duplicate plan.maintenance_steps.each do |step| running = step.enabled && (force || step.due_at?(at)) reason = running ? nil : skip_reason(step, at) execution.execution_steps.create!( maintenance_step: step, operation: step.operation, status: running ? "pending" : "skipped", skip_reason: reason ) end # Nothing due: not worth bringing the engine up. if execution.execution_steps.where(status: "pending").none? execution.update!(status: :success, current_step: "done", finished_at: at) return execution end TrinoEngineSupervisor.demand_arrived!( dispatch: -> { start_execution_on_engine(execution.id) } ) execution end
Enqueue a new run of a maintenance plan. Materialises the chain NOW (what will run and what stays out is visible before it starts, and the decision does not change mid-chain), then hands the engine lifecycle over to the supervisor. @param force [Boolean] operator-initiated run: bypass each stepβs CADENCE check so the plan runs now instead of only inside its cron minute. The step-level enabled flag is still honoured (cadence is a schedule, not a permission), and the duplicate guard below is untouched - a forced run is still one run.
# File app/services/maintenance_orchestrator.rb, line 97 def self.start_execution_on_engine(execution_history_id) execution = ExecutionHistory.find(execution_history_id) unless execution.status == "pending" Rails.logger.warn("start_execution_on_engine: execution #{execution.id} is #{execution.status}, not pending; skipping") return end execution.update!(status: :running, current_step: :start, last_heartbeat_at: Time.current) if TableLock.acquire(execution) execute_maintenance(execution_history_id) else skip_for_overlap(execution) end end
Releases a pending execution onto the engine: acquires the per-table lock and, when successful, enqueues the maintenance step. On lock contention the dispatch is skipped and recorded - never queued (queueing would grow forever on overlapping dispatches).
Source
# File app/services/maintenance_orchestrator.rb, line 146 def self.supervise_engine_start backend.supervise_engine_start end
Asks the backend to supervise the engine start.
# File app/services/maintenance_orchestrator.rb, line 134 def self.sync_catalog(catalog_id, force: false) backend.sync_catalog(catalog_id, force: force) end
Enqueues a catalog sync through the backend.
@param catalog_id [Integer] the catalog to sync @param force [Boolean] whether to force a full resync