The "Scan Floating IP" button failed with a client timeout: the project now holds ~6.4k floating IPs and the scan listed them all in one unpaginated, timeout-less Neutron request on the HTTP request context. openstack: ListFreeFloatingIPs reads marker-based pages (fields= keeps them small) with per-page retry/backoff on transport errors, 5xx and 429, and every request now has a timeout (also ends hangs inside the orchestrator tick). orchestrator: the scan is a single-flight background job on the process context with progress (clearing/listing/enqueuing/done/error), dry_run, full discovery before anything is enqueued, then SubmitIPs in chunks of 500 in ascending IP order; a failed read leaves the queue untouched. The auto-cycle gets a "scanning" phase that polls the job, so the control loop and autoCycleMu are never held across OpenStack/DB work; it recovers after a restart and waits for (instead of adopting) a scan started by someone else. db: migration 0009 (indexes), paged ListIPsPage/ListRegistryPage, GROUP BY counters, EXISTS completion check, set-based ClearAllIPs. API: POST /admin/ips/scan -> 202 (dry_run, wait), GET /admin/ips/scan, paging and filters on /admin/ips and /admin/registry (bare arrays without limit), results_by_overall in /admin/status. dashboard: scan progress panel and dry-run button, paginated /ips and /registry with server-side filters, Overview on counters and capped lists with progress/ETA, "select all N by filter", hx-params fix for per-row buttons, real counts in confirmations. Also: docs (API, USAGE, DASHBOARD, README), plan and review under docs/changes/, bin/ rebuilt with new SHA256SUMS. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
122 lines
4.3 KiB
Go
122 lines
4.3 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// MinAutoCycleIntervalSeconds is the smallest allowed pause between
|
|
// automatic cycles; it protects the OpenStack API from overly frequent
|
|
// floating-IP scans.
|
|
const MinAutoCycleIntervalSeconds = 60
|
|
|
|
// GetAutoCycle returns the singleton auto_cycle row. Migration 0008 inserts
|
|
// it, so sql.ErrNoRows would indicate a broken database, not a normal case.
|
|
func (d *DB) GetAutoCycle(ctx context.Context) (AutoCycle, error) {
|
|
var a AutoCycle
|
|
var enabled int
|
|
var runStarted, nextRun, lastStarted, lastFinished sql.NullString
|
|
err := d.QueryRowContext(ctx, `
|
|
SELECT enabled, interval_seconds, max_run_seconds, phase,
|
|
run_started_at, next_run_at, last_run_started_at, last_run_finished_at,
|
|
last_outcome, last_error, last_scanned_free, runs_total
|
|
FROM auto_cycle WHERE id=1
|
|
`).Scan(&enabled, &a.IntervalSeconds, &a.MaxRunSeconds, &a.Phase,
|
|
&runStarted, &nextRun, &lastStarted, &lastFinished,
|
|
&a.LastOutcome, &a.LastError, &a.LastScannedFree, &a.RunsTotal)
|
|
if err != nil {
|
|
return AutoCycle{}, err
|
|
}
|
|
a.Enabled = enabled != 0
|
|
if a.RunStartedAt, err = nullStringToTimePtr(runStarted); err != nil {
|
|
return AutoCycle{}, err
|
|
}
|
|
if a.NextRunAt, err = nullStringToTimePtr(nextRun); err != nil {
|
|
return AutoCycle{}, err
|
|
}
|
|
if a.LastRunStartedAt, err = nullStringToTimePtr(lastStarted); err != nil {
|
|
return AutoCycle{}, err
|
|
}
|
|
if a.LastRunFinishedAt, err = nullStringToTimePtr(lastFinished); err != nil {
|
|
return AutoCycle{}, err
|
|
}
|
|
return a, nil
|
|
}
|
|
|
|
// SetAutoCycleParams updates the auto-cycle configuration. A nil argument
|
|
// leaves the corresponding field unchanged (partial update). Validation:
|
|
// interval >= MinAutoCycleIntervalSeconds, maxRun >= 0 (0 = no limit).
|
|
func (d *DB) SetAutoCycleParams(ctx context.Context, intervalSeconds, maxRunSeconds *int) error {
|
|
if intervalSeconds != nil && *intervalSeconds < MinAutoCycleIntervalSeconds {
|
|
return fmt.Errorf("interval_seconds must be >= %d: %w", MinAutoCycleIntervalSeconds, ErrValidation)
|
|
}
|
|
if maxRunSeconds != nil && *maxRunSeconds < 0 {
|
|
return fmt.Errorf("max_run_seconds must be >= 0: %w", ErrValidation)
|
|
}
|
|
now := timeToDB(Now())
|
|
if intervalSeconds != nil {
|
|
if _, err := d.ExecContext(ctx, `
|
|
UPDATE auto_cycle SET interval_seconds=?, updated_at=? WHERE id=1
|
|
`, *intervalSeconds, now); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if maxRunSeconds != nil {
|
|
if _, err := d.ExecContext(ctx, `
|
|
UPDATE auto_cycle SET max_run_seconds=?, updated_at=? WHERE id=1
|
|
`, *maxRunSeconds, now); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SetAutoCycleEnabled flips the enabled flag and resets the runtime phase to
|
|
// idle (clearing run_started_at). nextRunAt sets next_run_at (nil clears it).
|
|
// A non-empty outcome is stored as last_outcome (and last_error is cleared);
|
|
// an empty one leaves the last outcome untouched.
|
|
func (d *DB) SetAutoCycleEnabled(ctx context.Context, enabled bool, nextRunAt *time.Time, outcome string) error {
|
|
en := 0
|
|
if enabled {
|
|
en = 1
|
|
}
|
|
now := timeToDB(Now())
|
|
_, err := d.ExecContext(ctx, `
|
|
UPDATE auto_cycle SET
|
|
enabled=?,
|
|
phase=?,
|
|
run_started_at=NULL,
|
|
next_run_at=?,
|
|
last_outcome=CASE WHEN ?='' THEN last_outcome ELSE ? END,
|
|
last_error=CASE WHEN ?='' THEN last_error ELSE '' END,
|
|
updated_at=?
|
|
WHERE id=1
|
|
`, en, AutoCyclePhaseIdle, timePtrToDB(nextRunAt), outcome, outcome, outcome, now)
|
|
return err
|
|
}
|
|
|
|
// UpdateAutoCycleState replaces all runtime-state columns (phase,
|
|
// timestamps, last outcome/error, counters) in one statement. It does not
|
|
// touch the configuration or the enabled flag.
|
|
func (d *DB) UpdateAutoCycleState(ctx context.Context, s AutoCycleState) error {
|
|
switch s.Phase {
|
|
case AutoCyclePhaseIdle, AutoCyclePhaseScanning, AutoCyclePhaseRunning, AutoCyclePhaseWaiting:
|
|
default:
|
|
return fmt.Errorf("invalid auto-cycle phase %q: %w", s.Phase, ErrValidation)
|
|
}
|
|
now := timeToDB(Now())
|
|
_, err := d.ExecContext(ctx, `
|
|
UPDATE auto_cycle SET
|
|
phase=?, run_started_at=?, next_run_at=?,
|
|
last_run_started_at=?, last_run_finished_at=?,
|
|
last_outcome=?, last_error=?, last_scanned_free=?, runs_total=?,
|
|
updated_at=?
|
|
WHERE id=1
|
|
`, s.Phase, timePtrToDB(s.RunStartedAt), timePtrToDB(s.NextRunAt),
|
|
timePtrToDB(s.LastRunStartedAt), timePtrToDB(s.LastRunFinishedAt),
|
|
s.LastOutcome, s.LastError, s.LastScannedFree, s.RunsTotal, now)
|
|
return err
|
|
}
|