ensure we dont collect hourly snapshot too soon after startup
Some checks failed
continuous-integration/drone/push Build was killed

This commit is contained in:
2026-01-14 10:27:24 +11:00
parent b297b8293c
commit 5130d37632
4 changed files with 66 additions and 14 deletions

View File

@@ -361,6 +361,38 @@ ORDER BY snapshot_time ASC, table_name ASC
return records, rows.Err() return records, rows.Err()
} }
func LatestSnapshotTime(ctx context.Context, database db.Database, snapshotType string) (time.Time, error) {
dbConn := database.DB()
driver := strings.ToLower(dbConn.DriverName())
var maxTime sql.NullInt64
switch driver {
case "sqlite":
if err := dbConn.GetContext(ctx, &maxTime, `
SELECT MAX(snapshot_time)
FROM snapshot_registry
WHERE snapshot_type = ?
`, snapshotType); err != nil {
return time.Time{}, err
}
case "pgx", "postgres":
if err := dbConn.GetContext(ctx, &maxTime, `
SELECT MAX(snapshot_time)
FROM snapshot_registry
WHERE snapshot_type = $1
`, snapshotType); err != nil {
return time.Time{}, err
}
default:
return time.Time{}, fmt.Errorf("unsupported driver for listing snapshots: %s", driver)
}
if !maxTime.Valid || maxTime.Int64 <= 0 {
return time.Time{}, nil
}
return time.Unix(maxTime.Int64, 0), nil
}
func FormatSnapshotLabel(snapshotType string, snapshotTime time.Time, tableName string) string { func FormatSnapshotLabel(snapshotType string, snapshotTime time.Time, tableName string) string {
switch snapshotType { switch snapshotType {
case "hourly": case "hourly":

View File

@@ -51,6 +51,30 @@ func (c *CronTask) RunVcenterSnapshotHourly(ctx context.Context, logger *slog.Lo
logger.Info("Hourly snapshot job finished", "duration", time.Since(startedAt)) logger.Info("Hourly snapshot job finished", "duration", time.Since(startedAt))
}() }()
startTime := time.Now() startTime := time.Now()
// reload settings in case vcenter list has changed
c.Settings.ReadYMLSettings()
if c.FirstHourlySnapshotCheck {
if err := report.EnsureSnapshotRegistry(ctx, c.Database); err != nil {
return err
}
lastSnapshot, err := report.LatestSnapshotTime(ctx, c.Database, "hourly")
if err != nil {
return err
}
minIntervalSeconds := intWithDefault(c.Settings.Values.Settings.VcenterInventorySnapshotSeconds, 3600)
if !lastSnapshot.IsZero() && startTime.Sub(lastSnapshot) < time.Duration(minIntervalSeconds)*time.Second {
c.Logger.Info("Skipping hourly snapshot, last snapshot too recent",
"last_snapshot", lastSnapshot,
"min_interval_seconds", minIntervalSeconds,
)
c.FirstHourlySnapshotCheck = false
return nil
}
c.FirstHourlySnapshotCheck = false
}
tableName, err := hourlyInventoryTableName(startTime) tableName, err := hourlyInventoryTableName(startTime)
if err != nil { if err != nil {
return err return err
@@ -60,16 +84,10 @@ func (c *CronTask) RunVcenterSnapshotHourly(ctx context.Context, logger *slog.Lo
if err := ensureDailyInventoryTable(ctx, dbConn, tableName); err != nil { if err := ensureDailyInventoryTable(ctx, dbConn, tableName); err != nil {
return err return err
} }
if err := report.EnsureSnapshotRegistry(ctx, c.Database); err != nil {
return err
}
if err := report.RegisterSnapshot(ctx, c.Database, "hourly", tableName, startTime); err != nil { if err := report.RegisterSnapshot(ctx, c.Database, "hourly", tableName, startTime); err != nil {
c.Logger.Warn("failed to register hourly snapshot", "error", err, "table", tableName) c.Logger.Warn("failed to register hourly snapshot", "error", err, "table", tableName)
} }
// reload settings in case vcenter list has changed
c.Settings.ReadYMLSettings()
var wg sync.WaitGroup var wg sync.WaitGroup
var errCount int64 var errCount int64
concurrencyLimit := c.Settings.Values.Settings.HourlySnapshotConcurrency concurrencyLimit := c.Settings.Values.Settings.HourlySnapshotConcurrency

View File

@@ -9,8 +9,9 @@ import (
// CronTask stores runtime information to be used by tasks // CronTask stores runtime information to be used by tasks
type CronTask struct { type CronTask struct {
Logger *slog.Logger Logger *slog.Logger
Database db.Database Database db.Database
Settings *settings.Settings Settings *settings.Settings
VcCreds *vcenter.VcenterLogin VcCreds *vcenter.VcenterLogin
FirstHourlySnapshotCheck bool
} }

View File

@@ -161,10 +161,11 @@ func main() {
// Pass useful information to the cron jobs // Pass useful information to the cron jobs
ct := &tasks.CronTask{ ct := &tasks.CronTask{
Logger: logger, Logger: logger,
Database: database, Database: database,
Settings: s, Settings: s,
VcCreds: &creds, VcCreds: &creds,
FirstHourlySnapshotCheck: true,
} }
/* /*