项目文件夹

文件
wehub-resource-sync 75f3dd141c
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (actions) (push) Has been cancelled
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
CI and Release / lint-frontend (push) Has been cancelled
CI and Release / dockerfile-scan (push) Has been cancelled
CI and Release / test-frontend (push) Has been cancelled
CI and Release / lint-verification-agent (push) Has been cancelled
CI and Release / test-verification-agent (push) Has been cancelled
CI and Release / e2e-verification-agent (push) Has been cancelled
CI and Release / test-backend (push) Has been cancelled
CI and Release / build-dev-image (push) Has been cancelled
CI and Release / push-dev-image (push) Has been cancelled
CI and Release / build-image (push) Has been cancelled
CI and Release / build-verification-image (push) Has been cancelled
CI and Release / determine-version (push) Has been cancelled
CI and Release / push-image (push) Has been cancelled
CI and Release / push-verification-image (12) (push) Has been cancelled
CI and Release / lint-backend (push) Has been cancelled
CI and Release / push-verification-image (17) (push) Has been cancelled
CI and Release / push-verification-image (18) (push) Has been cancelled
CI and Release / release (push) Has been cancelled
CI and Release / publish-helm-chart (push) Has been cancelled
CI and Release / push-verification-image (13) (push) Has been cancelled
CI and Release / push-verification-image (14) (push) Has been cancelled
CI and Release / push-verification-image (15) (push) Has been cancelled
CI and Release / push-verification-image (16) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:11:07 +08:00

59 行
1.8 KiB
Go

package container
import (
"context"
"fmt"
"log/slog"
)
// purgeEngine is the Docker seam for the startup purge — the same three methods
// the unit tests fake, rather than the whole Docker surface.
type purgeEngine interface {
ListManaged(ctx context.Context, agentID string) ([]ManagedContainer, error)
RemoveContainer(ctx context.Context, containerID string) error
RemoveNetwork(ctx context.Context, networkID string) error
}
// purgeAgentContainers runs once at startup, with no age grace and no
// active-set check, so each agent process begins from a clean slate
// ("always fresh").
//
// Best-effort by design: a per-item failure is logged and skipped, and a
// failed listing is logged and ignored, never blocking startup. Container and
// network names are per-UUID (databasus-verif-<uuid>), so a leftover never
// collides with a fresh run; degraded (a few stale containers until the next
// restart) beats refusing to start on a transient Docker hiccup.
func purgeAgentContainers(ctx context.Context, engine purgeEngine, agentID string, log *slog.Logger) {
managed, err := engine.ListManaged(ctx, agentID)
if err != nil {
log.Warn("startup purge could not list managed containers", "error", err)
return
}
if len(managed) == 0 {
return
}
removed := 0
for _, mc := range managed {
if err := engine.RemoveContainer(ctx, mc.ID); err != nil {
log.Warn("startup purge failed to remove container",
"verification_id", mc.VerificationID, "error", err)
continue
}
if mc.NetworkID != "" {
if err := engine.RemoveNetwork(ctx, mc.NetworkID); err != nil {
log.Warn("startup purge failed to remove network",
"verification_id", mc.VerificationID, "error", err)
}
}
removed++
}
log.Info(fmt.Sprintf("startup purge removed %d stale container(s)", removed))
}