mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-22 16:28:30 +02:00
refactor: code refactor and improved context and error handling
This commit is contained in:
@@ -65,7 +65,7 @@ func NewPoller[T any, AggregateT json.Marshaler](
|
||||
}
|
||||
|
||||
func (p *Poller[T, AggregateT]) savePath() string {
|
||||
return filepath.Join(saveBaseDir, fmt.Sprintf("%s.json", p.name))
|
||||
return filepath.Join(saveBaseDir, p.name+".json")
|
||||
}
|
||||
|
||||
func (p *Poller[T, AggregateT]) load() error {
|
||||
@@ -135,13 +135,14 @@ func (p *Poller[T, AggregateT]) pollWithTimeout(ctx context.Context) {
|
||||
|
||||
func (p *Poller[T, AggregateT]) Start() {
|
||||
t := task.RootTask("poller." + p.name)
|
||||
l := log.With().Str("name", p.name).Logger()
|
||||
err := p.load()
|
||||
if err != nil {
|
||||
if !os.IsNotExist(err) {
|
||||
log.Error().Err(err).Msgf("failed to load last metrics data for %s", p.name)
|
||||
l.Err(err).Msg("failed to load last metrics data")
|
||||
}
|
||||
} else {
|
||||
log.Debug().Msgf("Loaded last metrics data for %s, %d entries", p.name, p.period.Total())
|
||||
l.Debug().Int("entries", p.period.Total()).Msgf("Loaded last metrics data")
|
||||
}
|
||||
|
||||
go func() {
|
||||
@@ -154,11 +155,13 @@ func (p *Poller[T, AggregateT]) Start() {
|
||||
gatherErrsTicker.Stop()
|
||||
saveTicker.Stop()
|
||||
|
||||
p.save()
|
||||
if err := p.save(); err != nil {
|
||||
l.Err(err).Msg("failed to save metrics data")
|
||||
}
|
||||
t.Finish(nil)
|
||||
}()
|
||||
|
||||
log.Debug().Msgf("Starting poller %s with interval %s", p.name, pollInterval)
|
||||
l.Debug().Dur("interval", pollInterval).Msg("Starting poller")
|
||||
|
||||
p.pollWithTimeout(t.Context())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user