This repository has been archived on 2022-09-04. You can view files and clone it, but cannot push or open issues or pull requests.
dataupdater/main.go

111 lines
2.6 KiB
Go
Raw Normal View History

2020-06-01 19:05:33 +00:00
package main
import (
2021-04-25 15:02:58 +00:00
"context"
"github.com/go-redis/redis/v8"
"github.com/pkg/errors"
2020-06-01 19:05:33 +00:00
"os"
"os/signal"
"syscall"
2021-04-25 15:02:58 +00:00
"time"
2020-06-02 06:02:04 +00:00
"github.com/sirupsen/logrus"
"github.com/tribalwarshelp/shared/mode"
2021-04-25 15:02:58 +00:00
twhelpcron "github.com/tribalwarshelp/cron/cron"
"github.com/tribalwarshelp/cron/db"
envutils "github.com/tribalwarshelp/cron/utils/env"
2020-06-01 19:05:33 +00:00
"github.com/joho/godotenv"
"github.com/robfig/cron/v3"
)
func init() {
os.Setenv("TZ", "UTC")
if mode.Get() == mode.DevelopmentMode {
godotenv.Load(".env.local")
2020-06-01 19:05:33 +00:00
}
setupLogger()
2020-06-01 19:05:33 +00:00
}
func main() {
redisClient, err := initializeRedis()
2021-04-25 15:02:58 +00:00
if err != nil {
logrus.Fatal(errors.Wrap(err, "Couldn't connect to Redis"))
2021-04-25 15:02:58 +00:00
}
defer redisClient.Close()
2021-04-25 15:02:58 +00:00
dbConn, err := db.New(&db.Config{LogQueries: envutils.GetenvBool("LOG_DB_QUERIES")})
2021-04-25 15:02:58 +00:00
if err != nil {
logrus.Fatal(errors.Wrap(err, "Couldn't connect to the db"))
}
defer dbConn.Close()
2021-04-25 15:02:58 +00:00
logrus.Info("Connection with the database has been established")
c, err := twhelpcron.New(&twhelpcron.Config{
DB: dbConn,
RunOnInit: envutils.GetenvBool("RUN_ON_INIT"),
Redis: redisClient,
WorkerLimit: envutils.GetenvInt("WORKER_LIMIT"),
Opts: []cron.Option{
cron.WithChain(
cron.SkipIfStillRunning(
cron.PrintfLogger(logrus.WithField("package", "cron")),
),
),
},
2021-04-25 15:02:58 +00:00
})
if err != nil {
logrus.Fatal(err)
}
if err := c.Start(context.Background()); err != nil {
logrus.Fatal(err)
2020-06-01 19:05:33 +00:00
}
logrus.Info("Cron is running!")
2020-06-01 19:05:33 +00:00
channel := make(chan os.Signal, 1)
signal.Notify(channel, os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGINT)
<-channel
logrus.Info("shutting down")
if err := c.Stop(); err != nil {
2021-04-25 15:02:58 +00:00
logrus.Fatal(err)
}
}
func setupLogger() {
if mode.Get() == mode.DevelopmentMode {
logrus.SetLevel(logrus.DebugLevel)
}
timestampFormat := "2006-01-02 15:04:05"
if mode.Get() == mode.ProductionMode {
customFormatter := new(logrus.JSONFormatter)
customFormatter.TimestampFormat = timestampFormat
logrus.SetFormatter(customFormatter)
} else {
customFormatter := new(logrus.TextFormatter)
customFormatter.TimestampFormat = timestampFormat
customFormatter.FullTimestamp = true
logrus.SetFormatter(customFormatter)
}
}
2021-04-25 15:02:58 +00:00
func initializeRedis() (redis.UniversalClient, error) {
client := redis.NewClient(&redis.Options{
Addr: envutils.GetenvString("REDIS_ADDR"),
Username: envutils.GetenvString("REDIS_USERNAME"),
Password: envutils.GetenvString("REDIS_PASSWORD"),
DB: envutils.GetenvInt("REDIS_DB"),
})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := client.Ping(ctx).Err(); err != nil {
return nil, errors.Wrap(err, "initializeRedis")
}
return client, nil
}