cic-dw/cmd/main.go

82 lines
1.8 KiB
Go
Raw Normal View History

package main
import (
"cic-dw/pkg/cicnet"
"github.com/hibiken/asynq"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/knadh/koanf"
"github.com/lmittmann/w3"
"github.com/nleof/goyesql"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"golang.org/x/sys/unix"
"os"
"os/signal"
)
var (
k = koanf.New(".")
rClient asynq.RedisClientOpt
queries goyesql.Queries
conf config
db *pgxpool.Pool
cicnetClient *cicnet.CicNet
)
func init() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
if err := loadConfig("config.toml", k); err != nil {
log.Fatal().Err(err).Msg("failed to load config")
}
if err := loadQueries("queries.sql"); err != nil {
log.Fatal().Err(err).Msg("failed to load sql file")
}
if err := connectDb(conf.Db.Postgres); err != nil {
log.Fatal().Err(err).Msg("failed to connect to postgres")
}
// TODO: Not core, should be handled by job processor
if err := connectCicNet(conf.Chain.RpcProvider, w3.A(conf.Chain.TokenRegistry)); err != nil {
log.Fatal().Err(err).Msg("failed to connect to postgres")
}
}
func main() {
scheduler, err := bootstrapScheduler(rClient)
if err != nil {
log.Fatal().Err(err).Msg("could not bootstrap scheduler")
}
go func() {
if err := scheduler.Run(); err != nil {
log.Fatal().Err(err).Msg("could not start scheduler")
}
}()
processor, mux := bootstrapProcessor(rClient)
go func() {
if err := processor.Run(mux); err != nil {
log.Fatal().Err(err).Msg("failed to start job processor")
}
}()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, unix.SIGTERM, unix.SIGINT, unix.SIGTSTP)
for {
s := <-sigs
if s == unix.SIGTSTP {
processor.Stop()
scheduler.Shutdown()
continue
}
break
}
processor.Shutdown()
log.Info().Msg("gracefully shutdown processor and scheduler")
}