Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore:no CLI-Parser #211

Merged
merged 4 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
31 changes: 16 additions & 15 deletions server/backend/cron/cronjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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
Expand All @@ -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)
}
/*
Expand Down
4 changes: 4 additions & 0 deletions server/env/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ func IsDev() bool {
func IsProd() bool {
return GetEnvironment() == "prod"
}

func IsMensaCronActive() bool {
return os.Getenv("MensaCronDisabled") != "true"
}
9 changes: 2 additions & 7 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down