make running cron jobs on startup optional

This commit is contained in:
Dawid Wysokiński 2020-12-23 17:52:04 +01:00
parent a95c40399c
commit 9d0bd7831d
2 changed files with 10 additions and 6 deletions

View File

@ -15,6 +15,7 @@ var log = logrus.WithField("package", "cron")
type Config struct {
DB *pg.DB
MaxConcurrentWorkers int
RunOnStartup bool
}
func Attach(c *cron.Cron, cfg Config) error {
@ -43,12 +44,14 @@ func Attach(c *cron.Cron, cfg Config) error {
if _, err := c.AddFunc("30 2 * * *", updateStats); err != nil {
return err
}
go func() {
updateServerData()
vacuumDatabase()
updateHistory()
updateStats()
}()
if cfg.RunOnStartup {
go func() {
updateServerData()
vacuumDatabase()
updateHistory()
updateStats()
}()
}
return nil
}

View File

@ -57,6 +57,7 @@ func main() {
if err := _cron.Attach(c, _cron.Config{
DB: db,
MaxConcurrentWorkers: mustParseEnvToInt("MAX_CONCURRENT_WORKERS"),
RunOnStartup: os.Getenv("RUN_ON_STARTUP") == "true",
}); err != nil {
logrus.Fatal(err)
}