2022-12-01 06:13:48 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/certusone/wormhole/node/pkg/supervisor"
|
2023-10-18 07:18:32 -07:00
|
|
|
"github.com/go-redis/redis/v8"
|
|
|
|
"github.com/wormhole-foundation/wormhole-explorer/common/health"
|
2023-03-23 11:36:50 -07:00
|
|
|
"github.com/wormhole-foundation/wormhole-explorer/common/logger"
|
2022-12-01 06:13:48 -08:00
|
|
|
"github.com/wormhole-foundation/wormhole-explorer/spy/config"
|
|
|
|
"github.com/wormhole-foundation/wormhole-explorer/spy/grpc"
|
2022-12-14 12:07:55 -08:00
|
|
|
"github.com/wormhole-foundation/wormhole-explorer/spy/http/infraestructure"
|
2023-10-18 07:18:32 -07:00
|
|
|
"github.com/wormhole-foundation/wormhole-explorer/spy/source"
|
2022-12-01 06:13:48 -08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type exitCode int
|
|
|
|
|
|
|
|
func handleExit() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
if e, ok := r.(exitCode); ok {
|
|
|
|
os.Exit(int(e))
|
|
|
|
}
|
|
|
|
panic(r) // not an Exit, bubble up
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-18 07:18:32 -07:00
|
|
|
func newHealthChecks(
|
|
|
|
ctx context.Context,
|
|
|
|
client *redis.Client,
|
|
|
|
) ([]health.Check, error) {
|
|
|
|
|
|
|
|
healthChecks := []health.Check{
|
|
|
|
health.Redis(client),
|
|
|
|
}
|
|
|
|
return healthChecks, nil
|
|
|
|
}
|
|
|
|
|
2022-12-01 06:13:48 -08:00
|
|
|
func main() {
|
|
|
|
|
|
|
|
defer handleExit()
|
|
|
|
|
|
|
|
rootCtx, rootCtxCancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
config, err := config.New(rootCtx)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error creating config", err)
|
|
|
|
}
|
|
|
|
|
2023-03-23 11:36:50 -07:00
|
|
|
logger := logger.New("wormhole-explorer-spy", logger.WithLevel(config.LogLevel))
|
2022-12-01 06:13:48 -08:00
|
|
|
|
|
|
|
logger.Info("Starting wormhole-explorer-spy ...")
|
|
|
|
|
2023-02-01 04:31:15 -08:00
|
|
|
svs := grpc.NewSignedVaaSubscribers(logger)
|
2022-12-01 06:13:48 -08:00
|
|
|
avs := grpc.NewAllVaaSubscribers(logger)
|
2023-02-01 04:31:15 -08:00
|
|
|
go svs.Start(rootCtx)
|
|
|
|
go avs.Start(rootCtx)
|
2022-12-01 06:13:48 -08:00
|
|
|
|
2023-02-01 04:31:15 -08:00
|
|
|
handler := grpc.NewHandler(svs, avs, logger)
|
2022-12-01 06:13:48 -08:00
|
|
|
|
|
|
|
grpcServer, err := grpc.NewServer(handler, logger, config.GrpcAddress)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal("failed to start RPC server", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
supervisor.New(rootCtx, logger, func(ctx context.Context) error {
|
|
|
|
if err := supervisor.Run(ctx, "spyrpc", grpcServer.Runnable); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
<-ctx.Done()
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
// It's safer to crash and restart the process in case we encounter a panic,
|
|
|
|
// rather than attempting to reschedule the runnable.
|
|
|
|
supervisor.WithPropagatePanic)
|
|
|
|
|
|
|
|
publisher := grpc.NewPublisher(svs, avs, logger)
|
|
|
|
|
2023-10-18 07:18:32 -07:00
|
|
|
client := redis.NewClient(&redis.Options{Addr: config.RedisURI})
|
|
|
|
|
|
|
|
watcher, err := source.NewRedisSubscriber(rootCtx, client, config.RedisPrefix, config.RedisChannel, publisher.Publish, logger)
|
2022-12-01 06:13:48 -08:00
|
|
|
if err != nil {
|
2023-10-18 07:18:32 -07:00
|
|
|
logger.Fatal("failed to create redis subscriber", zap.Error(err))
|
2022-12-01 06:13:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
err = watcher.Start(rootCtx)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal("failed to watch MongoDB", zap.Error(err))
|
|
|
|
}
|
2023-10-18 07:18:32 -07:00
|
|
|
// get health check functions.
|
|
|
|
logger.Info("creating health check functions...")
|
|
|
|
healthChecks, err := newHealthChecks(rootCtx, client)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal("failed to create health checks", zap.Error(err))
|
|
|
|
}
|
2022-12-01 06:13:48 -08:00
|
|
|
|
2023-10-18 07:18:32 -07:00
|
|
|
server := infraestructure.NewServer(logger, config.Port, config.PprofEnabled, healthChecks...)
|
2022-12-14 12:07:55 -08:00
|
|
|
server.Start()
|
|
|
|
|
2022-12-01 06:13:48 -08:00
|
|
|
logger.Info("Started wormhole-explorer-spy")
|
|
|
|
|
|
|
|
// Waiting for signal
|
|
|
|
sigterm := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigterm, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
select {
|
|
|
|
case <-rootCtx.Done():
|
|
|
|
logger.Warn("Terminating with root context cancelled.")
|
|
|
|
case signal := <-sigterm:
|
|
|
|
logger.Info("Terminating with signal.", zap.String("signal", signal.String()))
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Info("root context cancelled, exiting...")
|
|
|
|
rootCtxCancel()
|
|
|
|
|
|
|
|
logger.Info("Closing GRPC server ...")
|
|
|
|
grpcServer.Stop()
|
2023-08-07 12:05:08 -07:00
|
|
|
|
2023-10-18 07:18:32 -07:00
|
|
|
logger.Info("Closing Redis connection...")
|
|
|
|
if err := watcher.Close(rootCtx); err != nil {
|
|
|
|
logger.Error("Error closing watcher", zap.Error(err))
|
|
|
|
}
|
|
|
|
if err := client.Close(); err != nil {
|
|
|
|
logger.Error("Error closing redis client", zap.Error(err))
|
|
|
|
}
|
2023-08-07 12:05:08 -07:00
|
|
|
|
2022-12-14 12:07:55 -08:00
|
|
|
logger.Info("Closing Http server ...")
|
|
|
|
server.Stop()
|
2022-12-01 06:13:48 -08:00
|
|
|
logger.Info("Finished wormhole-explorer-spy")
|
|
|
|
}
|