From fa1fe1a4f5b051f3c197510dec86c95e197cc5e2 Mon Sep 17 00:00:00 2001 From: Abhimanyu Babbar Date: Thu, 27 Oct 2022 11:18:40 +0530 Subject: [PATCH] Added support to read the interval between loops from env --- regulation-worker/internal/service/looper.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/regulation-worker/internal/service/looper.go b/regulation-worker/internal/service/looper.go index 2dcaf867a36..ad37e80fbd4 100644 --- a/regulation-worker/internal/service/looper.go +++ b/regulation-worker/internal/service/looper.go @@ -2,6 +2,9 @@ package service import ( "context" + "fmt" + "os" + "strconv" "time" backoff "github.com/cenkalti/backoff/v4" @@ -19,13 +22,19 @@ type Looper struct { func (l *Looper) Loop(ctx context.Context) error { pkgLogger.Infof("running regulation worker in infinite loop") + + interval, err := getenvInt("INTERVAL_IN_MINUTES", 10) + if err != nil { + return fmt.Errorf("reading value: %s from env: %s", "INTERVAL_IN_MINUTES", err.Error()) + } + for { err := l.Svc.JobSvc(ctx) if err == model.ErrNoRunnableJob { pkgLogger.Debugf("no runnable job found... sleeping") - if err := misc.SleepCtx(ctx, 1*time.Minute); err != nil { + if err := misc.SleepCtx(ctx, time.Duration(interval)*time.Minute); err != nil { pkgLogger.Debugf("context cancelled... exiting infinite loop %v", err) return nil } @@ -38,3 +47,11 @@ func (l *Looper) Loop(ctx context.Context) error { } } } + +func getenvInt(key string, fallback int) (int, error) { + k := os.Getenv(key) + if len(k) == 0 { + return fallback, nil + } + return strconv.Atoi(k) +}