MCPcopy Index your code
hub / github.com/PatchMon/PatchMon / RehydrateScheduledReports

Function RehydrateScheduledReports

server-source-code/internal/queue/server.go:263–313  ·  view source on GitHub ↗

RehydrateScheduledReports enqueues delayed tasks for all enabled scheduled reports. Call this at startup so that event-driven report chains are seeded after a restart.

(qc *asynq.Client, defaultDB *database.DB, poolCache *hostctx.PoolCache, log *slog.Logger)

Source from the content-addressed store, hash-verified

261// RehydrateScheduledReports enqueues delayed tasks for all enabled scheduled reports.
262// Call this at startup so that event-driven report chains are seeded after a restart.
263func RehydrateScheduledReports(qc *asynq.Client, defaultDB *database.DB, poolCache *hostctx.PoolCache, log *slog.Logger) {
264 if qc == nil || defaultDB == nil {
265 return
266 }
267 ctx := context.Background()
268 rehydrateDB := func(d *database.DB, host string) {
269 now := time.Now()
270 // Use a far-future cutoff to get ALL enabled reports with a next_run_at.
271 rows, err := d.Queries.ListScheduledReportsDue(ctx, pgtime.From(now.Add(365*24*time.Hour)))
272 if err != nil {
273 if log != nil {
274 log.Error("rehydrate scheduled reports: list failed", "error", err)
275 }
276 return
277 }
278 for _, r := range rows {
279 runAt := r.NextRunAt.Time
280 if !r.NextRunAt.Valid || runAt.Before(now) {
281 // Compute actual next cron time instead of running immediately,
282 // to avoid spurious duplicate runs on every restart.
283 tz := r.Timezone
284 if tz == "" {
285 tz = "UTC"
286 }
287 if next, nerr := notifications.NextCronRun(r.CronExpr, tz, now); nerr == nil {
288 runAt = next
289 } else {
290 runAt = now
291 }
292 }
293 if err := EnqueueScheduledReportAt(qc, r.ID, host, runAt); err != nil {
294 if log != nil {
295 log.Error("rehydrate scheduled reports: enqueue failed", "report_id", r.ID, "error", err)
296 }
297 }
298 }
299 if log != nil && len(rows) > 0 {
300 log.Info("rehydrate scheduled reports", "host", host, "count", len(rows))
301 }
302 }
303 rehydrateDB(defaultDB, "")
304 if poolCache != nil {
305 for _, host := range poolCache.ListHosts() {
306 d, err := poolCache.GetOrCreate(ctx, host)
307 if err != nil || d == nil {
308 continue
309 }
310 rehydrateDB(d, host)
311 }
312 }
313}

Callers

nothing calls this directly

Calls 5

EnqueueScheduledReportAtFunction · 0.85
ErrorMethod · 0.80
ListHostsMethod · 0.65
GetOrCreateMethod · 0.45

Tested by

no test coverage detected