From c554ccee839e117dbacc7de396f767c92a449085 Mon Sep 17 00:00:00 2001 From: Michal Iskierko Date: Mon, 22 Jul 2024 13:38:03 +0200 Subject: [PATCH] WIP --- api/geth_backend.go | 15 +++++++++++++-- centralizedmetrics/metrics.go | 35 ++++++++++++++++++----------------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/api/geth_backend.go b/api/geth_backend.go index 2179c5e39..5e77090bd 100644 --- a/api/geth_backend.go +++ b/api/geth_backend.go @@ -219,7 +219,19 @@ func (b *GethStatusBackend) ToggleCentralizedMetrics(isEnabled bool) error { return errors.New("centralized metrics nil") } - return b.centralizedMetrics.ToggleEnabled(isEnabled) + var err error + if isEnabled { + if !b.centralizedMetrics.IsRunning() { + // it was stopped, we need to recreate service + fmt.Println("!!! restart") + b.centralizedMetrics = centralizedmetrics.NewDefaultMetricService(b.multiaccountsDB.DB()) + } + err = b.centralizedMetrics.Start() + } else { + err = b.centralizedMetrics.Stop() + } + + return err } func (b *GethStatusBackend) AddCentralizedMetric(metric centralizedmetricscommon.Metric) error { @@ -227,7 +239,6 @@ func (b *GethStatusBackend) AddCentralizedMetric(metric centralizedmetricscommon return errors.New("centralized metrics nil") } return b.centralizedMetrics.AddMetric(metric) - } func (b *GethStatusBackend) GetAccounts() ([]multiaccounts.Account, error) { diff --git a/centralizedmetrics/metrics.go b/centralizedmetrics/metrics.go index 31a65bb34..14e9a22cf 100644 --- a/centralizedmetrics/metrics.go +++ b/centralizedmetrics/metrics.go @@ -50,9 +50,17 @@ func NewMetricService(repository MetricRepository, processor common.MetricProces } } -func (s *MetricService) Start() { +func (s *MetricService) IsRunning() bool { + return s.started +} + +func (s *MetricService) Start() error { if s.started { - return + return nil + } + err := s.repository.ToggleEnabled(true) + if err != nil { + return err } s.wg.Add(1) s.started = true @@ -67,16 +75,22 @@ func (s *MetricService) Start() { } } }() + return nil } -func (s *MetricService) Stop() { +func (s *MetricService) Stop() error { if !s.started { - return + return nil + } + err := s.repository.ToggleEnabled(false) + if err != nil { + return err } s.ticker.Stop() s.done <- true s.wg.Wait() s.started = false + return nil } func (s *MetricService) EnsureStarted() error { @@ -94,19 +108,6 @@ func (s *MetricService) Info() (*MetricsInfo, error) { return s.repository.Info() } -func (s *MetricService) ToggleEnabled(isEnabled bool) error { - err := s.repository.ToggleEnabled(isEnabled) - if err != nil { - return err - } - if isEnabled { - s.Start() - } else { - s.Stop() - } - return nil -} - func (s *MetricService) AddMetric(metric common.Metric) error { return s.repository.Add(metric) }