-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add scheduler for periodic checks
- Loading branch information
1 parent
8f41bb3
commit efad4e6
Showing
7 changed files
with
143 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package scheduler | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/ethersphere/beekeeper/pkg/logging" | ||
) | ||
|
||
type PeriodicExecutor struct { | ||
ticker *time.Ticker | ||
interval time.Duration | ||
log logging.Logger | ||
stopChan chan struct{} | ||
} | ||
|
||
func NewPeriodicExecutor(interval time.Duration, log logging.Logger) *PeriodicExecutor { | ||
return &PeriodicExecutor{ | ||
ticker: time.NewTicker(interval), | ||
interval: interval, | ||
log: log, | ||
stopChan: make(chan struct{}), | ||
} | ||
} | ||
|
||
func (pe *PeriodicExecutor) Start(ctx context.Context, task func(ctx context.Context) error) { | ||
go func() { | ||
for { | ||
select { | ||
case <-pe.ticker.C: | ||
pe.log.Tracef("Executing task") | ||
if err := task(ctx); err != nil { | ||
pe.log.Errorf("Task execution failed: %v", err) | ||
} | ||
case <-pe.stopChan: | ||
return | ||
case <-ctx.Done(): | ||
return | ||
} | ||
} | ||
}() | ||
} | ||
|
||
func (pe *PeriodicExecutor) Stop() { | ||
pe.ticker.Stop() | ||
close(pe.stopChan) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.