Commit 6fe9bbe8 authored by Hamdi Allam's avatar Hamdi Allam

catch panics and log config failures

parent 0f52d3d4
...@@ -23,15 +23,16 @@ type Cli struct { ...@@ -23,15 +23,16 @@ type Cli struct {
} }
func runIndexer(ctx *cli.Context) error { func runIndexer(ctx *cli.Context) error {
logger := log.NewLogger(log.ReadCLIConfig(ctx))
configPath := ctx.String(ConfigFlag.Name) configPath := ctx.String(ConfigFlag.Name)
cfg, err := config.LoadConfig(configPath) cfg, err := config.LoadConfig(configPath)
if err != nil { if err != nil {
logger.Error("failed to load config", "err", err)
return err return err
} }
// setup logger cfg.Logger = logger
cfg.Logger = log.NewLogger(log.ReadCLIConfig(ctx))
indexer, err := indexer.NewIndexer(cfg) indexer, err := indexer.NewIndexer(cfg)
if err != nil { if err != nil {
return err return err
...@@ -47,17 +48,20 @@ func runIndexer(ctx *cli.Context) error { ...@@ -47,17 +48,20 @@ func runIndexer(ctx *cli.Context) error {
} }
func runApi(ctx *cli.Context) error { func runApi(ctx *cli.Context) error {
configPath := ctx.String(ConfigFlag.Name) logger := log.NewLogger(log.ReadCLIConfig(ctx))
conf, err := config.LoadConfig(configPath)
fmt.Println(conf)
configPath := ctx.String(ConfigFlag.Name)
cfg, err := config.LoadConfig(configPath)
if err != nil { if err != nil {
panic(err) logger.Error("failed to load config", "err", err)
return err
} }
cfg.Logger = logger
fmt.Println(cfg)
// finish me // finish me
return nil return err
} }
var ( var (
......
...@@ -2,6 +2,7 @@ package indexer ...@@ -2,6 +2,7 @@ package indexer
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"sync" "sync"
...@@ -72,13 +73,20 @@ func NewIndexer(cfg config.Config) (*Indexer, error) { ...@@ -72,13 +73,20 @@ func NewIndexer(cfg config.Config) (*Indexer, error) {
// Start starts the indexing service on L1 and L2 chains // Start starts the indexing service on L1 and L2 chains
func (i *Indexer) Run(ctx context.Context) error { func (i *Indexer) Run(ctx context.Context) error {
var wg sync.WaitGroup var wg sync.WaitGroup
errCh := make(chan error) errCh := make(chan error, 1)
// If either processor errors out, we stop // If either processor errors out, we stop
processorCtx, cancel := context.WithCancel(ctx) processorCtx, cancel := context.WithCancel(ctx)
run := func(start func(ctx context.Context) error) { run := func(start func(ctx context.Context) error) {
wg.Add(1) wg.Add(1)
defer wg.Done() defer func() {
if err := recover(); err != nil {
i.log.Error("halting indexer on panic", "err", err)
errCh <- fmt.Errorf("panic: %v", err)
}
wg.Done()
}()
err := start(processorCtx) err := start(processorCtx)
if err != nil { if err != nil {
...@@ -86,6 +94,8 @@ func (i *Indexer) Run(ctx context.Context) error { ...@@ -86,6 +94,8 @@ func (i *Indexer) Run(ctx context.Context) error {
cancel() cancel()
errCh <- err errCh <- err
} else {
errCh <- errors.New("processor stopped")
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment