Refactor code to use 'any' type and improve context handling
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2026-02-18 16:16:27 +11:00
parent 6517a30fa2
commit f2d6b3158b
36 changed files with 197 additions and 175 deletions

View File

@@ -24,8 +24,6 @@ import (
"github.com/vmware/govmomi/vim25/types"
)
type ctxLoggerKey struct{}
type deletionCandidate struct {
vmID string
vmUUID string
@@ -42,10 +40,7 @@ type vcenterResources 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 {
if l := db.LoggerFromContext(ctx); l != nil {
return l
}
return fallback
@@ -132,10 +127,7 @@ func (c *CronTask) RunVcenterSnapshotHourly(ctx context.Context, logger *slog.Lo
if err != nil {
return err
}
minIntervalSeconds := intWithDefault(c.Settings.Values.Settings.VcenterInventorySnapshotSeconds, 3600) / 3
if minIntervalSeconds < 1 {
minIntervalSeconds = 1
}
minIntervalSeconds := max(intWithDefault(c.Settings.Values.Settings.VcenterInventorySnapshotSeconds, 3600)/3, 1)
if !lastSnapshot.IsZero() && startTime.Sub(lastSnapshot) < time.Duration(minIntervalSeconds)*time.Second {
c.Logger.Info("Skipping hourly snapshot, last snapshot too recent",
"last_snapshot", lastSnapshot,
@@ -217,7 +209,7 @@ func (c *CronTask) RunVcenterSnapshotHourly(ctx context.Context, logger *slog.Lo
metrics.RecordHourlySnapshot(startTime, rowCount, err)
var deferredTables []string
deferredReportTables.Range(func(key, _ interface{}) bool {
deferredReportTables.Range(func(key, _ any) bool {
name, ok := key.(string)
if ok && strings.TrimSpace(name) != "" && name != tableName {
deferredTables = append(deferredTables, name)
@@ -488,10 +480,7 @@ func buildUnionQuery(tables []string, columns []string, whereClause string) (str
batches := make([]string, 0, (len(tables)/maxCompoundTerms)+1)
batchIndex := 0
for start := 0; start < len(tables); start += maxCompoundTerms {
end := start + maxCompoundTerms
if end > len(tables) {
end = len(tables)
}
end := min(start+maxCompoundTerms, len(tables))
queries := make([]string, 0, end-start)
for _, table := range tables[start:end] {
safeName, err := db.SafeTableName(table)
@@ -1337,7 +1326,7 @@ func (c *CronTask) initVcenterResources(ctx context.Context, log *slog.Logger, u
func (c *CronTask) captureHourlySnapshotForVcenter(ctx context.Context, startTime time.Time, tableName string, url string, deferredReportTables *sync.Map) error {
log := c.Logger.With("vcenter", url)
ctx = context.WithValue(ctx, ctxLoggerKey{}, log)
ctx = db.WithLoggerContext(ctx, log)
started := time.Now()
log.Debug("connecting to vcenter for hourly snapshot", "url", url)
vc, resources, cleanup, err := c.initVcenterResources(ctx, log, url, startTime, started)