-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bbe318c
commit 2593009
Showing
2 changed files
with
113 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package services | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/logger" | ||
"github.com/smartcontractkit/chainlink-common/pkg/timeutil" | ||
) | ||
|
||
// Heartbeat is a usage of Engine for application specific heartbeats, | ||
// used in the core node and for loops. It accepts a named logger, | ||
// a beat, a setup func to initialize resources used on each beat, | ||
// a beat function to define the behavior on each beat, and a close func | ||
// for resource teardown | ||
type Heartbeat struct { | ||
Service | ||
eng *Engine | ||
|
||
beat time.Duration | ||
lggr logger.Logger | ||
} | ||
|
||
func NewHeartbeat( | ||
lggr logger.Logger, | ||
beat time.Duration, | ||
setupFn func(ctx context.Context) error, | ||
beatFn func(bCtx context.Context), | ||
closeFn func() error, | ||
) Heartbeat { | ||
h := Heartbeat{ | ||
beat: beat, | ||
lggr: lggr, | ||
} | ||
startFn := func(ctx context.Context) error { | ||
err := setupFn(ctx) | ||
if err != nil { | ||
return fmt.Errorf("setting up heartbeat: %w", err) | ||
} | ||
|
||
// consistent tick period | ||
constantTickFn := func() time.Duration { | ||
return h.beat | ||
} | ||
|
||
// TODO allow for override of tracer provider in engine | ||
// TODO wrap beatFn in engine trace | ||
h.eng.GoTick(timeutil.NewTicker(constantTickFn), beatFn) | ||
return nil | ||
} | ||
|
||
h.Service, h.eng = Config{ | ||
Name: fmt.Sprintf("%s.%s", lggr.Name(), "heartbeat"), | ||
Start: startFn, | ||
Close: closeFn, | ||
}.NewServiceEngine(lggr) | ||
return h | ||
} |