add a new directory - cmd
This commit is contained in:
parent
5e1041503c
commit
7a67a9c048
|
@ -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")
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
3
main.go
3
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 {
|
||||
|
|
|
@ -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,
|
||||
|
|
Reference in New Issue