Skip to content

Commit

Permalink
refactor: Update project structure and graceful exit
Browse files Browse the repository at this point in the history
  • Loading branch information
adriantpaez committed Oct 8, 2024
1 parent 4918f45 commit 0589b51
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ RUN go mod download
COPY . .

# Build the application
RUN CGO_ENABLED=0 GOOS=linux go build -o eoe .
RUN CGO_ENABLED=0 GOOS=linux go build -o eoe cmd/eoe/main.go

# Final stage
FROM alpine:latest
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ DEFAULT_GOAL := help
.PHONY: env

run:
go run main.go run
go run cmd/eoe/main.go run

build:
go build -o ./bin/eoe
go build -o ./bin/eoe cmd/eoe/main.go

build-docker:
docker build -t eoe .
Expand Down
27 changes: 27 additions & 0 deletions cmd/eoe/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"context"
"log/slog"
"os"
"os/signal"

"github.com/NethermindEth/eigenlayer-onchain-exporter/internal/cli"
)

func main() {
rootCmd := cli.RootCmd()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
go func() {
<-signalChan
cancel()
}()
err := rootCmd.ExecuteContext(ctx)
if err != nil {
slog.Error("error executing root command", "error", err)
os.Exit(1)
}
}
4 changes: 2 additions & 2 deletions internal/avs/eigenda/eigenda.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ func (e *eigenDAOnChainExporter) Run(ctx context.Context, c *config.Config) erro
for {
select {
case <-ctx.Done():
slog.Info("exporter context done |", "avsEnv", e.avsEnv)
return ctx.Err()
slog.Info("exiting exporter |", "avsEnv", e.avsEnv)
return nil
case <-ticker:
// Get the next block range
fromBlock, toBlock, err := e.nextBlockRange(latestBlock, tickerTime)
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go → internal/cli/root.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package cli

import (
"github.com/spf13/cobra"
Expand Down
29 changes: 12 additions & 17 deletions cmd/run.go → internal/cli/run.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package cmd
package cli

import (
"context"
"fmt"
"log/slog"
"os"
"os/signal"
"sync"

"github.com/NethermindEth/eigenlayer-onchain-exporter/internal/avs/eigenda"
Expand Down Expand Up @@ -50,42 +48,41 @@ func runCommand() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
var (
wg sync.WaitGroup
ctx, cancel = context.WithCancel(cmd.Context())
ctx = cmd.Context()
avsEnvs = make(map[string]bool)
exporterErrorCh = make(chan exporterError, len(avsEnvs))
)

// Add all AVS environments from operators
for _, operator := range c.Operators {
for _, env := range operator.AVSEnvs {
avsEnvs[env] = true
}
}

// Run exporters for each AVS environment
for env := range avsEnvs {
switch env {
case config.AVSEnvEigenDAMainnet, config.AVSEnvEigenDAHolesky:
// Initialize and run the AVS environment exporter
exporter, err := eigenda.NewEigenDAOnChainExporter(env, c)
if err != nil {
gracefulExit(cancel, &wg)
return err
}
runExporter(ctx, exporter, &wg, exporterErrorCh, c)
default:
gracefulExit(cancel, &wg)
return fmt.Errorf("invalid AVS environment: %s", env)
}
}
// Wait for all exporters to finish
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)

for {
select {
case exporterError := <-exporterErrorCh:
slog.Debug("exporter error", "exporter", exporterError.exporter.Name(), "error", exporterError.err)
runExporter(ctx, exporterError.exporter, &wg, exporterErrorCh, c)
case <-signals:
gracefulExit(cancel, &wg)
return nil
case <-ctx.Done():
slog.Debug("context done", "error", ctx.Err())
return gracefulExit(&wg, nil)
}
}
},
Expand All @@ -106,19 +103,17 @@ func runExporter(
defer wg.Done()
err := exporter.Run(ctx, c)
if err != nil {
if ctx.Err() != nil {
slog.Info("exporter context done", "exporter", exporter.Name())
} else {
if ctx.Err() == nil {
slog.Error("exporter error", "exporter", exporter.Name(), "error", err)
exporterErrorCh <- exporterError{exporter, err}
}
}
}()
}

func gracefulExit(cancel context.CancelFunc, wg *sync.WaitGroup) {
func gracefulExit(wg *sync.WaitGroup, err error) error {
slog.Debug("Shutting down exporters...")
cancel()
wg.Wait()
slog.Debug("Exporters shutdown complete")
return err
}
17 changes: 0 additions & 17 deletions main.go

This file was deleted.

0 comments on commit 0589b51

Please sign in to comment.