banger/internal/daemon/operations.go
Thales Maciel 3c0af3a2de
opstate,daemon: list in-flight operations via daemon.operations.list
Prerequisite for `banger update`'s preflight, which refuses to swap
binaries while anything is in flight. Today's opstate.Registry
exposes Insert/Get/Prune but no iteration; without a snapshot
accessor the update flow can't tell whether a vm.create is
mid-prepare-work-disk.

  * opstate.Registry.List(): returns a freshly-allocated snapshot
    of every entry. Mutating the slice doesn't poison the
    registry. Pinned by tests covering the snapshot semantics
    and the empty case.
  * api.OperationSummary / OperationsListResult: a public-shape
    record per op. Today the Kind is always "vm.create" — the
    field exists so future async kinds (image.pull, kernel.pull)
    plug in without an API change.
  * Daemon.ListOperations + daemon.operations.list RPC:
    walks vmService.createOps and emits OperationSummary entries.
    Done ops are included in the snapshot; the update preflight
    filters by Done itself.
  * dispatch_test's documented-methods list updated.

No behaviour change for existing flows; this is a read-only
addition.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-28 18:14:57 -03:00

37 lines
1.1 KiB
Go

package daemon
import (
"context"
"banger/internal/api"
)
// ListOperations returns a snapshot of every async operation tracked
// across the daemon's per-kind registries. Today the only kind is
// vm.create; future async kinds (image build, kernel pull) will plug
// in here.
//
// The primary consumer is `banger update`'s preflight, which refuses
// to swap binaries while anything is in flight. Done operations are
// included in the snapshot so an operator running an interactive
// `banger ... | jq` can see recently-completed work; the update
// preflight filters by Done itself.
func (d *Daemon) ListOperations(_ context.Context) (api.OperationsListResult, error) {
out := api.OperationsListResult{Operations: []api.OperationSummary{}}
if d.vm == nil {
return out, nil
}
for _, op := range d.vm.createOps.List() {
snap := op.snapshot()
out.Operations = append(out.Operations, api.OperationSummary{
ID: snap.ID,
Kind: "vm.create",
Stage: snap.Stage,
Detail: snap.Detail,
Done: snap.Done,
StartedAt: snap.StartedAt,
UpdatedAt: snap.UpdatedAt,
})
}
return out, nil
}