2022-04-25 17:29:38 +02:00
|
|
|
package main
|
2022-05-03 17:54:51 +02:00
|
|
|
|
|
|
|
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"
|
2022-05-05 14:01:34 +02:00
|
|
|
"golang.org/x/sys/unix"
|
2022-05-03 17:54:51 +02:00
|
|
|
"os"
|
2022-05-05 14:01:34 +02:00
|
|
|
"os/signal"
|
2022-05-03 17:54:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-05-05 14:01:34 +02:00
|
|
|
k = koanf.New(".")
|
|
|
|
|
|
|
|
rClient asynq.RedisClientOpt
|
2022-05-03 17:54:51 +02:00
|
|
|
queries goyesql.Queries
|
2022-05-05 14:01:34 +02:00
|
|
|
conf config
|
|
|
|
db *pgxpool.Pool
|
2022-05-03 17:54:51 +02:00
|
|
|
cicnetClient *cicnet.CicNet
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
|
|
|
|
2022-05-05 14:01:34 +02:00
|
|
|
if err := loadConfig("config.toml", k); err != nil {
|
2022-05-03 17:54:51 +02:00
|
|
|
log.Fatal().Err(err).Msg("failed to load config")
|
|
|
|
}
|
|
|
|
|
2022-05-05 14:01:34 +02:00
|
|
|
if err := loadQueries("queries.sql"); err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("failed to load sql file")
|
|
|
|
}
|
|
|
|
|
2022-05-05 18:39:25 +02:00
|
|
|
if err := connectDbs(conf.Db.Postgres); err != nil {
|
2022-05-05 14:01:34 +02:00
|
|
|
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")
|
|
|
|
}
|
2022-05-03 17:54:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2022-05-05 14:01:34 +02:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}()
|
2022-05-03 17:54:51 +02:00
|
|
|
|
2022-05-05 14:01:34 +02:00
|
|
|
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
|
2022-05-03 17:54:51 +02:00
|
|
|
}
|
|
|
|
|
2022-05-05 14:01:34 +02:00
|
|
|
processor.Shutdown()
|
|
|
|
log.Info().Msg("gracefully shutdown processor and scheduler")
|
2022-05-03 17:54:51 +02:00
|
|
|
}
|