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
|
|
|
|
2020-10-24 10:24:05 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-06-02 06:26:51 +00:00
|
|
|
"github.com/tribalwarshelp/shared/mode"
|
|
|
|
|
2021-05-01 05:24:02 +00:00
|
|
|
twhelpcron "github.com/tribalwarshelp/cron/internal/cron"
|
|
|
|
"github.com/tribalwarshelp/cron/internal/db"
|
|
|
|
envutils "github.com/tribalwarshelp/cron/internal/utils/env"
|
2020-06-01 19:05:33 +00:00
|
|
|
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
"github.com/robfig/cron/v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-04-27 18:06:38 +00:00
|
|
|
if err := setupENVs(); err != nil {
|
|
|
|
logrus.Fatal(err)
|
2020-06-01 19:05:33 +00:00
|
|
|
}
|
2021-04-23 19:20:01 +00:00
|
|
|
setupLogger()
|
2020-06-01 19:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2021-04-27 16:30:12 +00:00
|
|
|
redisClient, err := initializeRedis()
|
2021-04-25 15:02:58 +00:00
|
|
|
if err != nil {
|
2021-04-25 16:37:55 +00:00
|
|
|
logrus.Fatal(errors.Wrap(err, "Couldn't connect to Redis"))
|
2021-04-25 15:02:58 +00:00
|
|
|
}
|
2021-04-27 18:06:38 +00:00
|
|
|
defer func() {
|
|
|
|
if err := redisClient.Close(); err != nil {
|
|
|
|
logrus.Warn(errors.Wrap(err, "Couldn't close the Redis connection"))
|
|
|
|
}
|
|
|
|
}()
|
2021-04-25 15:02:58 +00:00
|
|
|
|
2021-04-27 16:30:12 +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 {
|
2021-04-25 16:37:55 +00:00
|
|
|
logrus.Fatal(errors.Wrap(err, "Couldn't connect to the db"))
|
2020-10-24 10:24:05 +00:00
|
|
|
}
|
2021-04-27 18:06:38 +00:00
|
|
|
defer func() {
|
|
|
|
if err := dbConn.Close(); err != nil {
|
|
|
|
logrus.Warn(errors.Wrap(err, "Couldn't close the db connection"))
|
|
|
|
}
|
|
|
|
}()
|
2021-04-25 15:02:58 +00:00
|
|
|
|
2021-04-27 16:30:12 +00:00
|
|
|
c, err := twhelpcron.New(&twhelpcron.Config{
|
|
|
|
DB: dbConn,
|
|
|
|
RunOnInit: envutils.GetenvBool("RUN_ON_INIT"),
|
|
|
|
Redis: redisClient,
|
2021-04-25 18:24:26 +00:00
|
|
|
WorkerLimit: envutils.GetenvInt("WORKER_LIMIT"),
|
2021-04-27 16:30:12 +00:00
|
|
|
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)
|
2020-12-30 22:57:23 +00:00
|
|
|
}
|
2021-04-27 16:30:12 +00:00
|
|
|
if err := c.Start(context.Background()); err != nil {
|
2020-10-24 10:24:05 +00:00
|
|
|
logrus.Fatal(err)
|
2020-06-01 19:05:33 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 10:24:05 +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
|
|
|
|
|
2020-10-24 10:24:05 +00:00
|
|
|
logrus.Info("shutting down")
|
2021-04-27 16:30:12 +00:00
|
|
|
if err := c.Stop(); err != nil {
|
2021-04-25 15:02:58 +00:00
|
|
|
logrus.Fatal(err)
|
2020-10-10 09:45:52 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-23 19:20:01 +00:00
|
|
|
|
2021-05-01 05:24:02 +00:00
|
|
|
func setupENVs() error {
|
|
|
|
err := os.Setenv("TZ", "UTC")
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "setupENVs")
|
|
|
|
}
|
|
|
|
|
|
|
|
if mode.Get() == mode.DevelopmentMode {
|
|
|
|
err := godotenv.Load(".env.local")
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "setupENVs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-23 19:20:01 +00:00
|
|
|
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
|
|
|
|
}
|