It was bothering me, so I thought about it a little bit and came up with this. I really like it. 😄
wg.Add(1)
go func() {
defer wg.Done()
ticker := time.NewTicker(time.Hour)
defer ticker.Stop()
for {
_, err := db.Exec("DELETE FROM jobs WHERE published_at < NOW() - INTERVAL '30 DAYS'")
if err != nil {
log.Println(fmt.Errorf("error clearing old jobs: %w", err))
}
select {
case <-ctx.Done():
log.Println("shutting down old jobs background process")
return
case <-ticker.C:
continue
}
}
}()
I figured out how to address the problem of the worker waiting an hour to run at startup that I raised here: https://github.com/devict/job-board/pull/17#discussion_r835939358
It was bothering me, so I thought about it a little bit and came up with this. I really like it. 😄