prysm-pulse/shared/runutil/every.go
Raul Jordan 546196a6fa
Other Package Godocs for Prysm (#5681)
* e2e docs
* slasher docs
* Merge branch 'other-package-godocs' of github.com:prysmaticlabs/prysm into other-package-godocs
* all validator package comments
* Merge branch 'master' into other-package-godocs
* completed all other packages
* Merge branch 'master' into other-package-godocs
* Merge refs/heads/master into other-package-godocs
2020-04-29 21:32:39 +00:00

32 lines
753 B
Go

// Package runutil includes helpers for scheduling runnable, periodic functions.
package runutil
import (
"context"
"reflect"
"runtime"
"time"
log "github.com/sirupsen/logrus"
)
// RunEvery runs the provided command periodically.
// It runs in a goroutine, and can be cancelled by finishing the supplied context.
func RunEvery(ctx context.Context, period time.Duration, f func()) {
funcName := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
ticker := time.NewTicker(period)
go func() {
for {
select {
case <-ticker.C:
log.WithField("function", funcName).Trace("running")
f()
case <-ctx.Done():
log.WithField("function", funcName).Debug("context is closed, exiting")
ticker.Stop()
return
}
}
}()
}