From 70132509749419851f31f7f55949261d04c30e14 Mon Sep 17 00:00:00 2001 From: Jonas Bostoen Date: Sun, 12 Jun 2022 19:28:59 -0400 Subject: [PATCH] feat: ctrl+c handling --- chainservice/chainservice_test.go | 6 +++--- main.go | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/chainservice/chainservice_test.go b/chainservice/chainservice_test.go index 121c050..1f5eb76 100644 --- a/chainservice/chainservice_test.go +++ b/chainservice/chainservice_test.go @@ -9,9 +9,9 @@ import ( "github.com/chainbound/apollo/types" ) -const ( - rpcUrl = "wss://arb-mainnet.g.alchemy.com/v2/5_JWUuiS1cewWFpLzRxdjgZM0yLA4Uqp" -) +// const ( +// rpcUrl = "wss://arb-mainnet.g.alchemy.com/v2/5_JWUuiS1cewWFpLzRxdjgZM0yLA4Uqp" +// ) func newChainService() *ChainService { // ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) diff --git a/main.go b/main.go index 44d4517..05c6855 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,9 @@ import ( "context" "fmt" "os" + "os/signal" "path" + "syscall" "time" _ "embed" @@ -136,6 +138,7 @@ func Run(opts types.ApolloOpts) error { defaultTimeout := time.Second * 30 service := chainservice.NewChainService(defaultTimeout, opts.RateLimit, cfg.Rpc) + setupCloseHandler(service) out := output.NewOutputHandler() @@ -193,3 +196,14 @@ func Run(opts types.ApolloOpts) error { return nil } + +func setupCloseHandler(svc *chainservice.ChainService) { + c := make(chan os.Signal) + signal.Notify(c, os.Interrupt, syscall.SIGTERM) + go func() { + <-c + logger.Warn().Msg("ctrl+c pressed, exiting...") + svc.DumpMetrics() + os.Exit(0) + }() +}