diff --git a/cmd/cron/main.go b/cmd/cron/main.go new file mode 100644 index 0000000..8116715 --- /dev/null +++ b/cmd/cron/main.go @@ -0,0 +1,67 @@ +package main + +import ( + "github.com/Kichiyaki/goutil/envutil" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" + "os" + "os/signal" + "syscall" + + "github.com/tribalwarshelp/cron/cmd/internal" + twhelpcron "github.com/tribalwarshelp/cron/pkg/cron" + "github.com/tribalwarshelp/cron/pkg/postgres" + "github.com/tribalwarshelp/cron/pkg/queue" +) + +func main() { + redisClient, err := internal.NewRedisClient() + if err != nil { + logrus.Fatal(errors.Wrap(err, "couldn't connect to Redis")) + } + defer func() { + if err := redisClient.Close(); err != nil { + logrus.Warn(errors.Wrap(err, "couldn't close the Redis connection")) + } + }() + + dbConn, err := postgres.Connect(nil) + if err != nil { + logrus.Fatal(errors.Wrap(err, "couldn't connect to the db")) + } + defer func() { + if err := dbConn.Close(); err != nil { + logrus.Warn(errors.Wrap(err, "couldn't close the db connection")) + } + }() + + q, err := queue.New(&queue.Config{ + DB: dbConn, + Redis: redisClient, + WorkerLimit: envutil.GetenvInt("WORKER_LIMIT"), + }) + if err != nil { + logrus.Fatal(errors.Wrap(err, "couldn't initialize a queue")) + } + + c, err := twhelpcron.New(&twhelpcron.Config{ + DB: dbConn, + RunOnInit: envutil.GetenvBool("RUN_ON_INIT"), + Queue: q, + }) + if err != nil { + logrus.Fatal(errors.Wrap(err, "couldn't initialize a cron instance")) + } + if err := c.Start(); err != nil { + logrus.Fatal(errors.Wrap(err, "couldn't start the cron")) + } + defer c.Stop() + + logrus.Info("Cron is up and running!") + + channel := make(chan os.Signal, 1) + signal.Notify(channel, os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGINT) + <-channel + + logrus.Info("shutting down") +} diff --git a/cmd/dataupdater/main.go b/cmd/dataupdater/main.go new file mode 100644 index 0000000..48837f0 --- /dev/null +++ b/cmd/dataupdater/main.go @@ -0,0 +1,60 @@ +package main + +import ( + "context" + "github.com/Kichiyaki/goutil/envutil" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" + "os" + "os/signal" + "syscall" + + "github.com/tribalwarshelp/cron/cmd/internal" + "github.com/tribalwarshelp/cron/pkg/postgres" + "github.com/tribalwarshelp/cron/pkg/queue" +) + +func main() { + redisClient, err := internal.NewRedisClient() + if err != nil { + logrus.Fatal(errors.Wrap(err, "Couldn't connect to Redis")) + } + defer func() { + if err := redisClient.Close(); err != nil { + logrus.Warn(errors.Wrap(err, "Couldn't close the Redis connection")) + } + }() + + dbConn, err := postgres.Connect(&postgres.Config{SkipDBInitialization: true}) + if err != nil { + logrus.Fatal(errors.Wrap(err, "Couldn't connect to the db")) + } + defer func() { + if err := dbConn.Close(); err != nil { + logrus.Warn(errors.Wrap(err, "Couldn't close the db connection")) + } + }() + + q, err := queue.New(&queue.Config{ + DB: dbConn, + Redis: redisClient, + WorkerLimit: envutil.GetenvInt("WORKER_LIMIT"), + }) + if err != nil { + logrus.Fatal(errors.Wrap(err, "Couldn't initialize a queue")) + } + if err := q.Start(context.Background()); err != nil { + logrus.Fatal(errors.Wrap(err, "Couldn't start the queue")) + } + + logrus.Info("Data updater is up and running!") + + channel := make(chan os.Signal, 1) + signal.Notify(channel, os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGINT) + <-channel + + logrus.Info("shutting down") + if err := q.Close(); err != nil { + logrus.Fatal(err) + } +} diff --git a/cmd/internal/init.go b/cmd/internal/init.go new file mode 100644 index 0000000..91d373e --- /dev/null +++ b/cmd/internal/init.go @@ -0,0 +1,50 @@ +package internal + +import ( + "github.com/Kichiyaki/appmode" + "github.com/joho/godotenv" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" + "os" +) + +func init() { + if err := setENVs(); err != nil { + logrus.Fatal(err) + } + prepareLogger() +} + +func setENVs() error { + err := os.Setenv("TZ", "UTC") + if err != nil { + return errors.Wrap(err, "setENVs") + } + + if appmode.Equals(appmode.DevelopmentMode) { + err := godotenv.Load(".env.local") + if err != nil { + return errors.Wrap(err, "setENVs") + } + } + + return nil +} + +func prepareLogger() { + if appmode.Equals(appmode.DevelopmentMode) { + logrus.SetLevel(logrus.DebugLevel) + } + + timestampFormat := "2006-01-02 15:04:05" + if appmode.Equals(appmode.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) + } +} diff --git a/cmd/internal/redis.go b/cmd/internal/redis.go new file mode 100644 index 0000000..443ea2c --- /dev/null +++ b/cmd/internal/redis.go @@ -0,0 +1,24 @@ +package internal + +import ( + "context" + "github.com/Kichiyaki/goutil/envutil" + "github.com/go-redis/redis/v8" + "github.com/pkg/errors" + "time" +) + +func NewRedisClient() (redis.UniversalClient, error) { + client := redis.NewClient(&redis.Options{ + Addr: envutil.GetenvString("REDIS_ADDR"), + Username: envutil.GetenvString("REDIS_USERNAME"), + Password: envutil.GetenvString("REDIS_PASSWORD"), + DB: envutil.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, "NewRedisClient") + } + return client, nil +} diff --git a/main.go b/main.go index 32ab641..93cfacd 100644 --- a/main.go +++ b/main.go @@ -38,7 +38,7 @@ func main() { } }() - dbConn, err := postgres.Connect(&postgres.Config{LogQueries: envutil.GetenvBool("LOG_DB_QUERIES")}) + dbConn, err := postgres.Connect(nil) if err != nil { logrus.Fatal(errors.Wrap(err, "Couldn't connect to the db")) } @@ -62,6 +62,7 @@ func main() { c, err := twhelpcron.New(&twhelpcron.Config{ DB: dbConn, + Queue: q, RunOnInit: envutil.GetenvBool("RUN_ON_INIT"), }) if err != nil { diff --git a/pkg/postgres/postgres.go b/pkg/postgres/postgres.go index e0b7e89..87e3a30 100644 --- a/pkg/postgres/postgres.go +++ b/pkg/postgres/postgres.go @@ -14,14 +14,13 @@ import ( var log = logrus.WithField("package", "pkg/postgres") type Config struct { - LogQueries bool SkipDBInitialization bool } func Connect(cfg *Config) (*pg.DB, error) { db := pg.Connect(prepareOptions()) - if cfg != nil && cfg.LogQueries { + if envutil.GetenvBool("LOG_DB_QUERIES") { db.AddQueryHook(querylogger.Logger{ Log: log, MaxQueryLength: 2000,