Skip to content

Commit

Permalink
Added support to read the interval between loops from env
Browse files Browse the repository at this point in the history
  • Loading branch information
abhimanyubabbar committed Oct 27, 2022
1 parent b4da4cf commit fa1fe1a
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion regulation-worker/internal/service/looper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package service

import (
"context"
"fmt"
"os"
"strconv"
"time"

backoff "github.com/cenkalti/backoff/v4"
Expand All @@ -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
}
Expand All @@ -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)
}

0 comments on commit fa1fe1a

Please sign in to comment.