package jobs import ( "pool-stats/constants" "pool-stats/database" "time" "github.com/jackc/pgx/v5/pgxpool" ) type RecalculateCurrentDayStatsJob struct { DB *pgxpool.Pool } func NewRecalculateCurrentDayStatsJob(db *pgxpool.Pool) *RecalculateCurrentDayStatsJob { return &RecalculateCurrentDayStatsJob{DB: db} } func (job *RecalculateCurrentDayStatsJob) Run() error { ticker := time.NewTicker(constants.RecalculateCurrentDayStatsJobInterval) defer ticker.Stop() for { <-ticker.C job.recalculateCurrentDayStats() } } func (job *RecalculateCurrentDayStatsJob) recalculateCurrentDayStats() { today := time.Now() yesterday := today.Add(-24 * time.Hour) database.DeleteDailyStatsForDay(job.DB, today) database.GetDailyStats(job.DB, today) // Need to keep yesterday's stats cache updated database.DeleteDailyStatsForDay(job.DB, yesterday) database.GetDailyStats(job.DB, yesterday) }