Files
cloud-ip-validator/internal/httpapi/handlers_autocycle.go
T
ayurishchevandClaude Sonnet 5.5 cd37b10f3b Add optional automatic check cycle (clear queue -> scan FIPs -> wait -> repeat)
An admin-controlled scenario that repeats what the operator does by hand:
clear the IP queue, scan and enqueue all free Floating IPs, wait until every
queued address reaches a terminal state (so results are in the Registry),
then wait a configurable interval and start over.

- control-api: new auto_cycle singleton table (migration 0008) holding
  enabled/interval/max-run settings and persisted phase state, so the cycle
  survives restarts; engine in orchestrator/autocycle.go driven from the
  existing loop tick with an injectable "now" for deterministic tests.
- Interval (default 1h, min 60s) and max wait (default unlimited, timeout
  outcome) are runtime settings, never hardcoded.
- The periodic fip_scan_interval_seconds scan is skipped while the cycle is
  enabled. An emptied queue mid-cycle counts as finished; stopping during
  the pause keeps the last cycle's outcome.
- API: GET/PUT /api/v1/admin/auto-cycle, POST .../start, POST .../stop.
- admin-dashboard: "Автоматический цикл" panel on /settings and an
  "Автоцикл активен" indicator on /overview.
- Tests for db, orchestrator, httpapi and dashboard; run-local-e2e.sh now
  exercises a full auto cycle; docs updated; bin/ rebuilt with refreshed
  SHA256SUMS.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-01 10:28:53 +03:00

70 lines
1.9 KiB
Go

package httpapi
import (
"net/http"
"cloudipvalidator/internal/db"
)
// Auto-cycle endpoints (see orchestrator/autocycle.go). Every endpoint
// answers with the full autoCycleDTO so callers never need a follow-up GET.
func toAutoCycleDTO(a db.AutoCycle) autoCycleDTO {
return autoCycleDTO{
Enabled: a.Enabled,
IntervalSeconds: a.IntervalSeconds,
MaxRunSeconds: a.MaxRunSeconds,
Phase: a.Phase,
RunStartedAt: a.RunStartedAt,
NextRunAt: a.NextRunAt,
LastRunStartedAt: a.LastRunStartedAt,
LastRunFinishedAt: a.LastRunFinishedAt,
LastOutcome: a.LastOutcome,
LastError: a.LastError,
LastScannedFree: a.LastScannedFree,
RunsTotal: a.RunsTotal,
}
}
func (s *Server) respondAutoCycle(w http.ResponseWriter, r *http.Request) {
ac, err := s.Orch.GetAutoCycle(r.Context())
if err != nil {
writeDBError(w, err)
return
}
writeJSON(w, http.StatusOK, toAutoCycleDTO(ac))
}
func (s *Server) handleAdminGetAutoCycle(w http.ResponseWriter, r *http.Request) {
s.respondAutoCycle(w, r)
}
func (s *Server) handleAdminPutAutoCycle(w http.ResponseWriter, r *http.Request) {
var req putAutoCycleRequest
if err := readJSON(r, &req); err != nil {
writeError(w, http.StatusBadRequest, "invalid body: "+err.Error())
return
}
if err := s.DB.SetAutoCycleParams(r.Context(), req.IntervalSeconds, req.MaxRunSeconds); err != nil {
writeDBError(w, err)
return
}
s.respondAutoCycle(w, r)
}
func (s *Server) handleAdminStartAutoCycle(w http.ResponseWriter, r *http.Request) {
if err := s.Orch.StartAutoCycle(r.Context()); err != nil {
writeDBError(w, err)
return
}
s.respondAutoCycle(w, r)
}
func (s *Server) handleAdminStopAutoCycle(w http.ResponseWriter, r *http.Request) {
if err := s.Orch.StopAutoCycle(r.Context()); err != nil {
writeDBError(w, err)
return
}
s.respondAutoCycle(w, r)
}