Files
arcadia-cloud/lib/arcadia_cloud/provisioning.ex
Giuliano Silvestro b1a124f044 Phase 2: first real DO write step — CreateDropletSnapshot
DigitalOcean.Client write methods:
- create_droplet_snapshot/3 — POST a snapshot action (async)
- get_droplet_action/3      — poll action status
- list_droplet_snapshots/2  — snapshots for a droplet
- delete_snapshot/2         — DELETE (used by compensation)
All use the "provisioning" token purpose.

Steps.CreateDropletSnapshot — the first saga step that touches real
infra:
- execute: deterministic snapshot name (arcadia-snap-<droplet>-<saga8>);
  checks context for a prior snapshot_id, then checks DO for a snapshot
  already carrying that name (crash-between-post-and-save recovery),
  then posts the action, polls to completion, finds the resulting
  snapshot, records snapshot_id + snapshot_name in context.
- compensate: deletes the snapshot; treats HTTP 404 as success.

Provisioning.snapshot_droplet/2 — convenience saga starter.

Two DO eventual-consistency gotchas surfaced + handled:
- After a snapshot action reports "completed", the snapshot lags a few
  seconds before appearing in /droplets/:id/snapshots. The step now
  retries the lookup (find_snapshot_with_retry, 12x5s) instead of
  failing with :snapshot_not_found_after_completion.
- Deletion has the same lag the other way — a deleted snapshot lingers
  in the listing briefly. compensate just trusts the DELETE 2xx/404;
  no post-delete verification needed.

Live smoke verified end-to-end against holyspiritbraypark.com:
[CreateDropletSnapshot, Fail] saga — the step created real snapshot
229305609, the Fail step triggered compensation, compensation deleted
the snapshot. Final: saga rolled_back, ledger
[create_droplet_snapshot: compensated, fail: failed], zero leftover on DO.

Test-harness note: smoke tests create sagas via Provisioning.create_saga
(no Oban enqueue) so a single manual Runner.perform/1 owns execution —
start_saga/1 enqueues an Oban job, and running both racing the same saga
corrupts the step ledger. Production only ever runs via Oban.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-20 08:38:35 +10:00

130 lines
3.7 KiB
Elixir

defmodule ArcadiaCloud.Provisioning do
@moduledoc """
Context for saga orchestration — provisioning, suspension, offboarding,
updates, anything that wants compensation-based rollback over Oban.
Pattern: caller assembles a step list (per template or hand-rolled),
calls `start_saga/1`, the Runner Oban worker walks the steps,
persisting results and rolling back on failure.
"""
import Ecto.Query, warn: false
alias ArcadiaCloud.Repo
alias ArcadiaCloud.Provisioning.{SagaRun, SagaStepResult}
@doc """
Inserts a saga_runs row + enqueues the Runner job.
Required:
:kind — provision | suspend | offboard | update | rollback | test
:step_modules — ordered list of step module atoms or fully-qualified strings
:inputs — map of saga inputs (stored in context.__inputs__)
Optional:
:deployment_id — links the saga to a deployment (nil for skyai-internal)
:triggered_by — user_id or "system:<reason>"
"""
def start_saga(opts) when is_list(opts) do
start_saga(Map.new(opts))
end
def start_saga(%{} = attrs) do
step_modules = Enum.map(attrs[:step_modules] || [], &to_string/1)
inputs = attrs[:inputs] || %{}
saga_attrs = %{
kind: attrs[:kind],
step_modules: step_modules,
deployment_id: attrs[:deployment_id],
triggered_by: attrs[:triggered_by],
context: %{"__inputs__" => inputs}
}
with {:ok, saga} <- create_saga(saga_attrs),
{:ok, _job} <-
%{"saga_id" => saga.id}
|> ArcadiaCloud.Provisioning.Runner.new()
|> Oban.insert() do
{:ok, saga}
end
end
def create_saga(attrs) do
%SagaRun{}
|> SagaRun.changeset(attrs)
|> Repo.insert()
end
@doc """
Starts a snapshot saga for a droplet. `droplet_provider_id` is the DO
numeric droplet id (string). Optional `:snapshot_label` and
`:triggered_by`.
"""
def snapshot_droplet(droplet_provider_id, opts \\ []) do
start_saga(%{
kind: "provision",
step_modules: [ArcadiaCloud.Provisioning.Steps.CreateDropletSnapshot],
inputs: %{
droplet_provider_id: to_string(droplet_provider_id),
snapshot_label: opts[:snapshot_label]
},
triggered_by: opts[:triggered_by] || "manual"
})
end
def get_saga(id), do: Repo.get(SagaRun, id)
def get_saga!(id), do: Repo.get!(SagaRun, id)
def update_saga(%SagaRun{} = saga, attrs) do
saga
|> SagaRun.changeset(attrs)
|> Repo.update()
end
def list_sagas(opts \\ []) do
base = from(s in SagaRun, order_by: [desc: s.inserted_at])
base
|> maybe_filter(:status, opts[:status])
|> maybe_filter(:kind, opts[:kind])
|> maybe_filter(:deployment_id, opts[:deployment_id])
|> maybe_limit(opts[:limit])
|> Repo.all()
end
def list_step_results(saga_id) do
from(r in SagaStepResult,
where: r.saga_id == ^saga_id,
order_by: [asc: r.step_idx]
)
|> Repo.all()
end
def cancel_saga(%SagaRun{} = saga) do
saga
|> SagaRun.changeset(%{cancel_requested: true})
|> Repo.update()
end
def upsert_step_result(saga_id, step_idx, attrs) do
case Repo.get_by(SagaStepResult, saga_id: saga_id, step_idx: step_idx) do
nil ->
%SagaStepResult{}
|> SagaStepResult.changeset(Map.merge(attrs, %{saga_id: saga_id, step_idx: step_idx}))
|> Repo.insert()
existing ->
existing
|> SagaStepResult.changeset(attrs)
|> Repo.update()
end
end
defp maybe_filter(q, _f, nil), do: q
defp maybe_filter(q, field, value), do: from(s in q, where: field(s, ^field) == ^value)
defp maybe_limit(q, nil), do: q
defp maybe_limit(q, n), do: from(s in q, limit: ^n)
end