devict / job-board

the devICT job board!
https://jobs.devict.org
0 stars 13 forks source link

Run the worker immediately at startup #22

Closed kevinfalting closed 2 years ago

kevinfalting commented 2 years ago

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. 😄

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
        }
    }
}()
sethetter commented 2 years ago

@kevinfalting I dig it! Going to submit a PR?

kevinfalting commented 2 years ago

Can do, it's going to be a bit though.