Skip to content

Commit

Permalink
Start metrics service before migrations (#1638)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshklop authored Jan 9, 2024
1 parent 112c178 commit bd53567
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ type Node struct {
db db.DB
blockchain *blockchain.Blockchain

services []service.Service
log utils.Logger
metricsService service.Service // Start the metrics service earlier than other services.
services []service.Service
log utils.Logger

version string
}
Expand Down Expand Up @@ -172,6 +173,7 @@ func New(cfg *Config, version string) (*Node, error) { //nolint:gocyclo,funlen
if cfg.Websocket {
services = append(services, makeRPCOverWebsocket(cfg.WebsocketHost, cfg.WebsocketPort, rpcServers, log, cfg.Metrics))
}
var metricsService service.Service
if cfg.Metrics {
chain.WithListener(makeBlockchainMetrics())
makeJunoMetrics(version)
Expand All @@ -182,7 +184,7 @@ func New(cfg *Config, version string) (*Node, error) { //nolint:gocyclo,funlen
synchronizer.WithListener(makeSyncMetrics(synchronizer, chain))
client.WithListener(makeFeederMetrics())
gatewayClient.WithListener(makeGatewayMetrics())
services = append(services, makeMetrics(cfg.MetricsHost, cfg.MetricsPort))
metricsService = makeMetrics(cfg.MetricsHost, cfg.MetricsPort)
}
if cfg.GRPC {
services = append(services, makeGRPC(cfg.GRPCHost, cfg.GRPCPort, database, version))
Expand All @@ -192,12 +194,13 @@ func New(cfg *Config, version string) (*Node, error) { //nolint:gocyclo,funlen
}

n := &Node{
cfg: cfg,
log: log,
version: version,
db: database,
blockchain: chain,
services: services,
cfg: cfg,
log: log,
version: version,
db: database,
blockchain: chain,
services: services,
metricsService: metricsService,
}

if n.cfg.EthNode == "" {
Expand Down Expand Up @@ -285,6 +288,20 @@ func (n *Node) Run(ctx context.Context) {
}
n.log.Debugw(fmt.Sprintf("Running Juno with config:\n%s", string(yamlConfig)))

wg := conc.NewWaitGroup()
defer wg.Wait()
ctx, cancel := context.WithCancel(ctx)
defer cancel()

if n.metricsService != nil {
wg.Go(func() {
defer cancel()
if err := n.metricsService.Run(ctx); err != nil {
n.log.Errorw("Metrics error", "err", err)
}
})
}

if err := migration.MigrateIfNeeded(ctx, n.db, n.cfg.Network, n.log); err != nil {
if errors.Is(err, context.Canceled) {
n.log.Infow("DB Migration cancelled")
Expand All @@ -294,8 +311,6 @@ func (n *Node) Run(ctx context.Context) {
return
}

ctx, cancel := context.WithCancel(ctx)
wg := conc.NewWaitGroup()
for _, s := range n.services {
s := s
wg.Go(func() {
Expand All @@ -307,10 +322,8 @@ func (n *Node) Run(ctx context.Context) {
}
})
}
defer wg.Wait()

<-ctx.Done()
cancel()
n.log.Infow("Shutting down Juno...")
}

Expand Down

0 comments on commit bd53567

Please sign in to comment.