ensure we dont collect hourly snapshot too soon after startup
Some checks failed
continuous-integration/drone/push Build was killed
Some checks failed
continuous-integration/drone/push Build was killed
This commit is contained in:
@@ -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":
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -13,4 +13,5 @@ type CronTask struct {
|
|||||||
Database db.Database
|
Database db.Database
|
||||||
Settings *settings.Settings
|
Settings *settings.Settings
|
||||||
VcCreds *vcenter.VcenterLogin
|
VcCreds *vcenter.VcenterLogin
|
||||||
|
FirstHourlySnapshotCheck bool
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user