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 { type Config struct {
DB *pg.DB DB *pg.DB
MaxConcurrentWorkers int MaxConcurrentWorkers int
RunOnStartup bool
} }
func Attach(c *cron.Cron, cfg Config) error { 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 { if _, err := c.AddFunc("30 2 * * *", updateStats); err != nil {
return err return err
} }
go func() { if cfg.RunOnStartup {
updateServerData() go func() {
vacuumDatabase() updateServerData()
updateHistory() vacuumDatabase()
updateStats() updateHistory()
}() updateStats()
}()
}
return nil return nil
} }

View File

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