improve concurrency handling for inventory job
Some checks failed
continuous-integration/drone/push Build encountered an error

This commit is contained in:
2026-01-21 11:21:51 +11:00
parent 715b293894
commit b77f8671da
4 changed files with 29 additions and 13 deletions

View File

@@ -25,6 +25,16 @@ import (
type ctxLoggerKey struct{}
func loggerFromCtx(ctx context.Context, fallback *slog.Logger) *slog.Logger {
if ctx == nil {
return fallback
}
if l, ok := ctx.Value(ctxLoggerKey{}).(*slog.Logger); ok && l != nil {
return l
}
return fallback
}
// RunVcenterSnapshotHourly records hourly inventory snapshots into a daily table.
// If force is true, any in-progress marker will be cleared before starting (useful for manual recovery).
func (c *CronTask) RunVcenterSnapshotHourly(ctx context.Context, logger *slog.Logger, force bool) (err error) {
@@ -992,7 +1002,7 @@ func (c *CronTask) captureHourlySnapshotForVcenter(ctx context.Context, startTim
// If VM count dropped versus totals and we still haven't marked missing, try another comparison + wider event window.
if missingCount == 0 && prevVmCount.Valid && prevVmCount.Int64 > int64(totals.VmCount) {
// Fallback: compare against latest registered snapshot table.
if prevTable, err := latestHourlySnapshotBefore(ctx, dbConn, startTime); err == nil && prevTable != "" {
if prevTable, err := latestHourlySnapshotBefore(ctx, dbConn, startTime, loggerFromCtx(ctx, c.Logger)); err == nil && prevTable != "" {
moreMissing := c.markMissingFromPrevious(ctx, dbConn, prevTable, url, startTime, presentSnapshots, presentByUuid, presentByName, inventoryByVmID, inventoryByUuid, inventoryByName)
if moreMissing > 0 {
missingCount += moreMissing
@@ -1104,7 +1114,7 @@ func (c *CronTask) compareWithPreviousSnapshot(
inventoryByName map[string]queries.Inventory,
missingCount int,
) (string, int, int) {
prevTableName, prevTableErr := latestHourlySnapshotBefore(ctx, dbConn, startTime)
prevTableName, prevTableErr := latestHourlySnapshotBefore(ctx, dbConn, startTime, loggerFromCtx(ctx, c.Logger))
if prevTableErr != nil {
c.Logger.Warn("failed to locate previous hourly snapshot for deletion comparison", "error", prevTableErr, "url", url)
}
@@ -1115,8 +1125,8 @@ func (c *CronTask) compareWithPreviousSnapshot(
if prevTableName != "" {
moreMissing := c.markMissingFromPrevious(ctx, dbConn, prevTableName, url, startTime, presentSnapshots, presentByUuid, presentByName, inventoryByVmID, inventoryByUuid, inventoryByName)
missingCount += moreMissing
expectedSeconds := int64(durationFromSeconds(c.Settings.Values.Settings.VcenterInventorySnapshotSeconds, time.Hour).Seconds())
// Skip only if snapshots are much closer together than the configured cadence (e.g., rerun inside half interval).
expectedSeconds := int64(c.Settings.Values.Settings.VcenterInventorySnapshotSeconds)
// Skip only if snapshots are much closer together than the configured cadence.
if SnapshotTooSoon(prevSnapshotTime, startTime.Unix(), expectedSeconds) {
c.Logger.Info("skipping new-VM detection because snapshots are too close together", "prev_table", prevTableName, "prev_snapshot_unix", prevSnapshotTime, "current_snapshot_unix", startTime.Unix(), "expected_interval_seconds", expectedSeconds)
} else {