-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '143-crawler-refactoring' into raw-module-with-neutron
- Loading branch information
Showing
6 changed files
with
164 additions
and
13 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,11 @@ | ||
package healthchecker | ||
|
||
import "time" | ||
|
||
type Config struct { | ||
MaxBlockLag time.Duration `env:"HEALTHCHECK_MAX_LAST_BLOCK_LAG" envDefault:"5m"` | ||
Interval time.Duration `env:"HEALTHCHECK_INTERVAL" envDefault:"1m"` | ||
StartDelay time.Duration `env:"HEALTHCHECK_START_DELAY"` | ||
Enabled bool `env:"HEALTHCHECK_ENABLED" envDefault:"false"` | ||
FatalOnCheck bool `env:"HEALTHCHECK_FATAL_ON_CHECK" envDefault:"true"` | ||
} |
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,91 @@ | ||
package healthchecker | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/rs/zerolog" | ||
) | ||
|
||
const defaultInterval = time.Minute | ||
|
||
type ( | ||
CheckFn func(ctx context.Context, log *zerolog.Logger) bool | ||
|
||
Checker struct { | ||
log *zerolog.Logger | ||
cancel context.CancelFunc | ||
isHealth CheckFn | ||
cfg Config | ||
} | ||
) | ||
|
||
func New(l zerolog.Logger, checkFn CheckFn, cfg Config) *Checker { | ||
l = l.With().Str("cmp", "healthchecker").Logger() | ||
|
||
if cfg.Interval == 0 { | ||
cfg.Interval = defaultInterval | ||
} | ||
|
||
return &Checker{ | ||
log: &l, | ||
isHealth: checkFn, | ||
cfg: cfg, | ||
} | ||
} | ||
|
||
func (c *Checker) Start(_ context.Context) error { | ||
if !c.cfg.Enabled { | ||
return nil | ||
} | ||
|
||
go c.run() | ||
return nil | ||
} | ||
|
||
func (c *Checker) Stop(_ context.Context) error { | ||
if !c.cfg.Enabled { | ||
return nil | ||
} | ||
|
||
c.cancel() | ||
return nil | ||
} | ||
|
||
func (c *Checker) run() { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
c.cancel = cancel | ||
|
||
if c.cfg.StartDelay > 0 { | ||
<-time.Tick(c.cfg.StartDelay) //nolint:staticcheck | ||
} | ||
|
||
ticker := time.NewTicker(c.cfg.Interval) | ||
defer ticker.Stop() | ||
|
||
c.log.Debug().Msg("checker started") | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
c.log.Info().Msg("checker stopped") | ||
return | ||
case <-ticker.C: | ||
c.log.Info().Msg("checking health") | ||
|
||
func() { | ||
ctx2, cancel2 := context.WithTimeout(ctx, c.cfg.Interval/2) | ||
defer cancel2() | ||
|
||
if !c.isHealth(ctx2, c.log) { | ||
if c.cfg.FatalOnCheck { | ||
c.log.Fatal().Msg("service is not healthy") | ||
return | ||
} | ||
|
||
c.log.Warn().Msg("service is not healthy") | ||
} | ||
}() | ||
} | ||
} | ||
} |