From 9d0bd7831d4285d935934e367c9a719ce65dd5cb Mon Sep 17 00:00:00 2001 From: Kichiyaki Date: Wed, 23 Dec 2020 17:52:04 +0100 Subject: [PATCH] make running cron jobs on startup optional --- cron/cron.go | 15 +++++++++------ main.go | 1 + 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/cron/cron.go b/cron/cron.go index d0c62d3..65cd2c2 100644 --- a/cron/cron.go +++ b/cron/cron.go @@ -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 } diff --git a/main.go b/main.go index a3ebac9..58412a5 100644 --- a/main.go +++ b/main.go @@ -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) }