From 7b23e21ddfc6fc51673a06cee4e909d924eccbbf Mon Sep 17 00:00:00 2001 From: Kichiyaki Date: Sun, 11 Jul 2021 11:04:51 +0200 Subject: [PATCH] update README.md, add dockerfiles --- .dockerignore | 1 + README.md | 10 ++- Dockerfile => cmd/cron/Dockerfile | 6 +- cmd/dataupdater/Dockerfile | 32 +++++++ main.go | 135 ------------------------------ 5 files changed, 42 insertions(+), 142 deletions(-) create mode 100644 .dockerignore rename Dockerfile => cmd/cron/Dockerfile (80%) create mode 100644 cmd/dataupdater/Dockerfile delete mode 100644 main.go diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..11ee758 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +.env.local diff --git a/README.md b/README.md index 324994e..60e0f7d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# tribalwarshelp.com cron +# datafetcher - Adds automatically new servers. - Fetches and updates server data (players, tribes, ODA, ODD, ODS, OD, conquers, configs). @@ -40,9 +40,13 @@ git clone git@github.com:tribalwarshelp/cron.git ``` 2. Open the folder with this project in a terminal. 3. Set the required env variables directly in your system or create .env.local file. -4. Run the app. +4. Run the cron. ``` -go run main.go +go run ./cmd/cron/main.go +``` +5. Run the data updater in a new tab. +``` +go run ./cmd/dataupdater/main.go ``` ## License diff --git a/Dockerfile b/cmd/cron/Dockerfile similarity index 80% rename from Dockerfile rename to cmd/cron/Dockerfile index 82d02a3..be47ad2 100644 --- a/Dockerfile +++ b/cmd/cron/Dockerfile @@ -12,7 +12,7 @@ RUN go mod download # Copy the source from the current directory to the Working Directory inside the container COPY . . -RUN go build -o twcron . +RUN go build -o twcron ./cmd/cron ######## Start a new stage from scratch ####### FROM alpine:latest @@ -28,7 +28,5 @@ ENV APP_MODE=production EXPOSE 8080 RUN apk add --no-cache tzdata -ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.2.1/wait ./wait -RUN chmod +x ./wait -CMD ./wait && ./twcron +CMD ./twcron diff --git a/cmd/dataupdater/Dockerfile b/cmd/dataupdater/Dockerfile new file mode 100644 index 0000000..fde64d0 --- /dev/null +++ b/cmd/dataupdater/Dockerfile @@ -0,0 +1,32 @@ +FROM golang:alpine as builder + +# Set the Current Working Directory inside the container +WORKDIR /app + +# Copy go mod and sum files +COPY go.mod go.sum ./ + +# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed +RUN go mod download + +# Copy the source from the current directory to the Working Directory inside the container +COPY . . + +RUN go build -o twdataupdater ./cmd/dataupdater + +######## Start a new stage from scratch ####### +FROM alpine:latest + +RUN apk --no-cache add ca-certificates + +WORKDIR /root/ + +# Copy the Pre-built binary file from the previous stage +COPY --from=builder /app/twdataupdater . + +ENV APP_MODE=production +EXPOSE 8080 + +RUN apk add --no-cache tzdata + +CMD ./twdataupdater diff --git a/main.go b/main.go deleted file mode 100644 index 93cfacd..0000000 --- a/main.go +++ /dev/null @@ -1,135 +0,0 @@ -package main - -import ( - "context" - "github.com/Kichiyaki/appmode" - "github.com/Kichiyaki/goutil/envutil" - "github.com/go-redis/redis/v8" - "github.com/pkg/errors" - "os" - "os/signal" - "syscall" - "time" - - "github.com/sirupsen/logrus" - - twhelpcron "github.com/tribalwarshelp/cron/pkg/cron" - "github.com/tribalwarshelp/cron/pkg/postgres" - "github.com/tribalwarshelp/cron/pkg/queue" - - "github.com/joho/godotenv" -) - -func init() { - if err := setupENVs(); err != nil { - logrus.Fatal(err) - } - setupLogger() -} - -func main() { - redisClient, err := initializeRedis() - 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")) - } - if err := q.Start(context.Background()); err != nil { - logrus.Fatal(errors.Wrap(err, "Couldn't start the queue")) - } - - c, err := twhelpcron.New(&twhelpcron.Config{ - DB: dbConn, - Queue: q, - RunOnInit: envutil.GetenvBool("RUN_ON_INIT"), - }) - 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") - if err := q.Close(); err != nil { - logrus.Fatal(err) - } -} - -func setupENVs() error { - err := os.Setenv("TZ", "UTC") - if err != nil { - return errors.Wrap(err, "setupENVs") - } - - if appmode.Equals(appmode.DevelopmentMode) { - err := godotenv.Load(".env.local") - if err != nil { - return errors.Wrap(err, "setupENVs") - } - } - - return nil -} - -func setupLogger() { - 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) - } -} - -func initializeRedis() (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, "initializeRedis") - } - return client, nil -}