cic-chain-events/cmd/service/main.go

143 lines
3.5 KiB
Go
Raw Normal View History

2023-01-05 12:45:09 +01:00
package main
import (
"context"
"flag"
"strings"
2023-01-05 12:45:09 +01:00
"sync"
"time"
"github.com/grassrootseconomics/cic-chain-events/internal/filter"
2023-01-05 12:45:09 +01:00
"github.com/grassrootseconomics/cic-chain-events/internal/pipeline"
"github.com/grassrootseconomics/cic-chain-events/internal/pub"
2023-01-05 12:45:09 +01:00
"github.com/grassrootseconomics/cic-chain-events/internal/syncer"
"github.com/knadh/goyesql/v2"
"github.com/knadh/koanf/v2"
"github.com/labstack/echo/v4"
2023-01-05 12:45:09 +01:00
"github.com/zerodha/logf"
)
type (
internalServiceContainer struct {
apiService *echo.Echo
pub *pub.Pub
}
)
2023-01-05 12:45:09 +01:00
var (
build string
confFlag string
debugFlag bool
migrationsFolderFlag string
queriesFlag string
2023-01-05 12:45:09 +01:00
ko *koanf.Koanf
lo logf.Logger
q goyesql.Queries
)
func init() {
flag.StringVar(&confFlag, "config", "config.toml", "Config file location")
flag.BoolVar(&debugFlag, "debug", false, "Enable debug logging")
flag.StringVar(&migrationsFolderFlag, "migrations", "migrations/", "Migrations folder location")
2023-01-05 12:45:09 +01:00
flag.StringVar(&queriesFlag, "queries", "queries.sql", "Queries file location")
flag.Parse()
lo = initLogger()
ko = initConfig()
2023-01-05 12:45:09 +01:00
}
func main() {
lo.Info("main: starting cic-chain-events", "build", build)
parsedQueries := initQueries(queriesFlag)
2023-01-05 12:45:09 +01:00
graphqlFetcher := initFetcher()
pgStore := initPgStore(migrationsFolderFlag, parsedQueries)
natsConn, jsCtx := initJetStream()
jsPub := initPub(natsConn, jsCtx)
2023-01-05 12:45:09 +01:00
pipeline := pipeline.NewPipeline(pipeline.PipelineOpts{
BlockFetcher: graphqlFetcher,
Filters: []filter.Filter{
initAddressFilter(),
initGasGiftFilter(jsPub),
initTransferFilter(jsPub),
initRegisterFilter(jsPub),
2023-01-05 12:45:09 +01:00
},
Logg: lo,
Store: pgStore,
})
internalServices := &internalServiceContainer{
pub: jsPub,
}
syncerStats := &syncer.Stats{}
wg := &sync.WaitGroup{}
signalCh, closeCh := createSigChannel()
defer closeCh()
ctx, cancel := context.WithCancel(context.Background())
2023-01-05 12:45:09 +01:00
headSyncer, err := syncer.NewHeadSyncer(syncer.HeadSyncerOpts{
Logg: lo,
Pipeline: pipeline,
Pool: initHeadSyncerWorkerPool(ctx),
2023-01-05 12:45:09 +01:00
Stats: syncerStats,
WsEndpoint: ko.MustString("chain.ws_endpoint"),
})
if err != nil {
lo.Fatal("main: crticial error loading head syncer", "error", err)
2023-01-05 12:45:09 +01:00
}
janitor := syncer.NewJanitor(syncer.JanitorOpts{
BatchSize: uint64(ko.MustInt64("syncer.janitor_queue_size")),
2023-01-05 12:45:09 +01:00
Logg: lo,
Pipeline: pipeline,
Pool: initJanitorWorkerPool(ctx),
2023-01-05 12:45:09 +01:00
Stats: syncerStats,
Store: pgStore,
SweepInterval: time.Second * time.Duration(ko.MustInt64("syncer.janitor_sweep_interval")),
2023-01-05 12:45:09 +01:00
})
wg.Add(1)
go func() {
defer wg.Done()
if err := headSyncer.Start(ctx); err != nil {
lo.Info("main: starting head syncer")
lo.Fatal("main: critical error starting head syncer", "error", err)
2023-01-05 12:45:09 +01:00
}
}()
wg.Add(1)
go func() {
defer wg.Done()
lo.Info("main: starting janitor")
2023-01-05 12:45:09 +01:00
if err := janitor.Start(ctx); err != nil {
lo.Fatal("main: critical error starting janitor", "error", err)
2023-01-05 12:45:09 +01:00
}
}()
internalServices.apiService = initApiServer()
wg.Add(1)
go func() {
defer wg.Done()
host := ko.MustString("service.address")
lo.Info("main: starting API server", "host", host)
if err := internalServices.apiService.Start(host); err != nil {
if strings.Contains(err.Error(), "Server closed") {
lo.Info("main: shutting down server")
} else {
lo.Fatal("main: critical error shutting down server", "err", err)
2023-01-05 12:45:09 +01:00
}
}
}()
2023-01-05 12:45:09 +01:00
lo.Info("main: graceful shutdown triggered", "signal", <-signalCh)
cancel()
startGracefulShutdown(context.Background(), internalServices)
wg.Wait()
2023-01-05 12:45:09 +01:00
}