From ed3627820aee26edc68928372f9811d143836e9d Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Wed, 13 Sep 2023 22:47:11 +0200 Subject: [PATCH] removed the need for a CLI-Parser (#211) --- README.md | 6 +--- .../templates/deployments/backend-v2.yaml | 2 ++ docker-compose.yaml | 1 + server/backend/cron/cronjobs.go | 31 ++++++++++--------- server/env/environment.go | 4 +++ server/main.go | 9 ++---- 6 files changed, 26 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 7f2dd65c..2c29a84b 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ To start the server there are environment variables, as well as command line opt ```bash cd server export DB_DSN="Your gorm DB connection string for example: gorm:GORM_USER_PASSWORD@tcp(localhost:3306)/campus_backend" -go run ./main.go [-MensaCron 0] +go run ./main.go ``` #### Environment Variables @@ -72,10 +72,6 @@ There are a few environment variables available: * [REQUIRED] `DB_DSN`: The [GORM](https://gorm.io/) [DB connection string](https://gorm.io/docs/connecting_to_the_database.html#MySQL) for connecting to the MySQL DB. Example: `gorm@tcp(localhost:3306)/campus_backend` * [OPTIONAL] `SENTRY_DSN`: The Sentry [Data Source Name](https://sentry-docs-git-patch-1.sentry.dev/product/sentry-basics/dsn-explainer/) for reporting issues and crashes. -#### Command Line Arguments - -* [OPTIONAL] `-MensaCron 0`: Providing this argument deactivates the Mensa Rating cronjobs if not needed in a local setup. Be aware, this option will change in a future version ([#117](https://github.com/TUM-Dev/Campus-Backend/issues/117) and [#115](https://github.com/TUM-Dev/Campus-Backend/issues/115)). - ## Running the Server (Docker) ```bash docker compose up -d diff --git a/deployment/charts/backend/templates/deployments/backend-v2.yaml b/deployment/charts/backend/templates/deployments/backend-v2.yaml index 310387e7..f037f8f9 100644 --- a/deployment/charts/backend/templates/deployments/backend-v2.yaml +++ b/deployment/charts/backend/templates/deployments/backend-v2.yaml @@ -39,6 +39,8 @@ spec: env: - name: ENVIRONMENT value: prod + - name: MensaCronDisabled + value: "false" - name: APNS_P8_FILE_PATH value: /etc/apns_auth_key.p8 - name: SENTRY_DSN diff --git a/docker-compose.yaml b/docker-compose.yaml index 65a65470..4a4a9174 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -16,6 +16,7 @@ services: - APNS_KEY_ID=${APNS_KEY_ID} - APNS_TEAM_ID=${APNS_TEAM_ID} - APNS_P8_FILE_PATH=${APNS_P8_FILE_PATH} + - MensaCronDisabled=false volumes: - backend-storage:/Storage - ./apns_auth_key.p8:${APNS_P8_FILE_PATH} diff --git a/server/backend/cron/cronjobs.go b/server/backend/cron/cronjobs.go index fc6858b9..b09fc17a 100644 --- a/server/backend/cron/cronjobs.go +++ b/server/backend/cron/cronjobs.go @@ -2,6 +2,7 @@ package cron import ( "github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/ios_apns" + "github.com/TUM-Dev/Campus-Backend/server/env" "time" "github.com/TUM-Dev/Campus-Backend/server/model" @@ -12,10 +13,9 @@ import ( ) type CronService struct { - db *gorm.DB - gf *gofeed.Parser - useMensa bool - APNs *ios_apns.Service + db *gorm.DB + gf *gofeed.Parser + APNs *ios_apns.Service } const StorageDir = "/Storage/" // target location of files @@ -36,21 +36,22 @@ const ( AlarmType = "alarm" */ ) -func New(db *gorm.DB, mensaCronActivated bool) *CronService { +func New(db *gorm.DB) *CronService { return &CronService{ - db: db, - gf: gofeed.NewParser(), - APNs: ios_apns.NewCronService(db), - useMensa: mensaCronActivated, + db: db, + gf: gofeed.NewParser(), + APNs: ios_apns.NewCronService(db), } } func (c *CronService) Run() error { - log.WithField("MensaCronsRunning", c.useMensa).Trace("running cron service") + log.WithField("MensaCronActive", env.IsMensaCronActive()).Debug("running cron service") g := new(errgroup.Group) - g.Go(func() error { return c.dishNameDownloadCron() }) - g.Go(func() error { return c.averageRatingComputation() }) + if env.IsMensaCronActive() { + g.Go(func() error { return c.dishNameDownloadCron() }) + g.Go(func() error { return c.averageRatingComputation() }) + } for { log.Trace("Cron: checking for pending") @@ -72,7 +73,7 @@ func (c *CronService) Run() error { for _, cronjob := range res { // Persist run to DB right away var offset int32 = 0 - if c.useMensa { + if env.IsMensaCronActive() { if cronjob.Type.String == AverageRatingComputation { if time.Now().Hour() == 16 { offset = 18 * 3600 // fast-forward 18 Hours to the next day + does not need to be computed overnight @@ -95,11 +96,11 @@ func (c *CronService) Run() error { case FileDownloadType: g.Go(func() error { return c.fileDownloadCron() }) case DishNameDownload: - if c.useMensa { + if env.IsMensaCronActive() { g.Go(c.dishNameDownloadCron) } case AverageRatingComputation: //call every five minutes between 11AM and 4 PM on weekdays - if c.useMensa { + if env.IsMensaCronActive() { g.Go(c.averageRatingComputation) } /* diff --git a/server/env/environment.go b/server/env/environment.go index cea605a8..0526d478 100644 --- a/server/env/environment.go +++ b/server/env/environment.go @@ -13,3 +13,7 @@ func IsDev() bool { func IsProd() bool { return GetEnvironment() == "prod" } + +func IsMensaCronActive() bool { + return os.Getenv("MensaCronDisabled") != "true" +} diff --git a/server/main.go b/server/main.go index d3eb3997..f6d74a78 100644 --- a/server/main.go +++ b/server/main.go @@ -80,13 +80,8 @@ func main() { return } - var mensaCronActivated = true - if len(os.Args) > 2 && os.Args[1] == "-MensaCron" && os.Args[2] == "0" { - mensaCronActivated = false - log.Info("Cronjobs for the cafeteria rating are deactivated. Remove commandline argument <-MensaCron 0> or set it to 1.", len(os.Args)) - } - // Create any other background services (these shouldn't do any long running work here) - cronService := cron.New(db, mensaCronActivated) + // Create any other background services (these shouldn't do any long-running work here) + cronService := cron.New(db) campusService := backend.New(db) // Listen to our configured ports