Skip to content

Commit

Permalink
chore(cli): add runSentry to cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
namn-grg committed Jun 11, 2024
1 parent 629a51a commit 7b78b00
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 64 deletions.
57 changes: 55 additions & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package cmd

import (
"context"
"os"
"os/signal"
"syscall"

"github.com/chainbound/valtrack/clickhouse"
"github.com/chainbound/valtrack/consumer"
"github.com/chainbound/valtrack/discovery"
"github.com/google/uuid"

"github.com/rs/zerolog"
Expand All @@ -12,7 +18,7 @@ import (
var ConsumerCommand = &cli.Command{
Name: "consumer",
Usage: "run the consumer",
Action: LaunchConsumer,
Action: runConsumer,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "log-level",
Expand Down Expand Up @@ -59,7 +65,27 @@ var ConsumerCommand = &cli.Command{
},
}

func LaunchConsumer(c *cli.Context) error {
var SentryCommand = &cli.Command{
Name: "sentry",
Usage: "run the sentry node",
Action: runSentry,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "log-level",
Usage: "log level",
Aliases: []string{"l"},
Value: "info",
},
&cli.StringFlag{
Name: "nats-url",
Usage: "NATS server URL (needs JetStream)",
Aliases: []string{"n"},
Value: "", // If empty URL, run the sentry without NATS
},
},
}

func runConsumer(c *cli.Context) error {
cfg := consumer.ConsumerConfig{
LogLevel: c.String("log-level"),
NatsURL: c.String("nats-url"),
Expand All @@ -79,3 +105,30 @@ func LaunchConsumer(c *cli.Context) error {
consumer.RunConsumer(&cfg)
return nil
}

func runSentry(c *cli.Context) error {
level, _ := zerolog.ParseLevel(c.String("log-level"))
zerolog.SetGlobalLevel(level)

disc, err := discovery.NewDiscovery(c.String("nats-url"))
if err != nil {
panic(err)
}

ctx, cancel := context.WithCancel(context.Background())

defer cancel()

go func() {
if err := disc.Start(ctx); err != nil {
panic(err)
}
}()

quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)

<-quit

return nil
}
63 changes: 1 addition & 62 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,86 +1,25 @@
package main

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

"github.com/rs/zerolog"
"github.com/urfave/cli/v2"

"github.com/chainbound/valtrack/cmd"
"github.com/chainbound/valtrack/discovery"
)

type Config struct {
logLevel string
natsURL string
}

func main() {
cfg := new(Config)

app := &cli.App{
Name: "valtrack",
Usage: "Ethereum consensus validator tracking tool",
Commands: []*cli.Command{
{
Name: "sentry",
Usage: "run the sentry node",
Action: func(*cli.Context) error {
level, _ := zerolog.ParseLevel(cfg.logLevel)
zerolog.SetGlobalLevel(level)

runSentry(cfg.natsURL)
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "log-level",
Usage: "log level",
Aliases: []string{"l"},
Value: "info",
Destination: &cfg.logLevel,
},
&cli.StringFlag{
Name: "nats-url",
Usage: "NATS server URL (needs JetStream)",
Aliases: []string{"n"},
Value: "", // If empty URL, run the sentry without NATS
Destination: &cfg.natsURL,
},
},
},
cmd.SentryCommand,
cmd.ConsumerCommand,
},
}

if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}

}

func runSentry(natsURL string) {
disc, err := discovery.NewDiscovery(natsURL)
if err != nil {
panic(err)
}

ctx, cancel := context.WithCancel(context.Background())

defer cancel()

go func() {
if err := disc.Start(ctx); err != nil {
panic(err)
}
}()

quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)

<-quit
}

0 comments on commit 7b78b00

Please sign in to comment.