121 lines
4.3 KiB
Go
121 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, 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
|
||
|
|
}
|