Skip to content
This repository was archived by the owner on May 30, 2022. It is now read-only.

Commit

Permalink
Moved duplicated repeat function to internal/utils.go
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonkopliku committed Nov 22, 2021
1 parent e855ec1 commit 6760a39
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions internal/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"context"
"crypto/md5"
"encoding/hex"
"fmt"
Expand All @@ -9,6 +10,7 @@ import (
"os"
"regexp"
"strings"
"time"

log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -95,3 +97,24 @@ func CRC32hash(input []byte) int {
return int(crc32.Checksum(input, crc32Table))

}

// Repeat executes a function at a given interval.
// the first tick runs immediately
func Repeat(operation string, tick func(), interval time.Duration, ctx context.Context) {
tick()

ticker := time.NewTicker(interval)
msg := fmt.Sprintf("Next execution for operation %s in %s", operation, interval)
log.Debugf(msg)

defer ticker.Stop()
for {
select {
case <-ticker.C:
tick()
log.Debugf(msg)
case <-ctx.Done():
return
}
}
}

0 comments on commit 6760a39

Please sign in to comment.