update README.md, add dockerfiles
This commit is contained in:
parent
7a67a9c048
commit
7b23e21ddf
|
@ -0,0 +1 @@
|
||||||
|
.env.local
|
10
README.md
10
README.md
|
@ -1,4 +1,4 @@
|
||||||
# tribalwarshelp.com cron
|
# datafetcher
|
||||||
|
|
||||||
- Adds automatically new servers.
|
- Adds automatically new servers.
|
||||||
- Fetches and updates server data (players, tribes, ODA, ODD, ODS, OD, conquers, configs).
|
- 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.
|
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.
|
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
|
## License
|
||||||
|
|
|
@ -12,7 +12,7 @@ RUN go mod download
|
||||||
# Copy the source from the current directory to the Working Directory inside the container
|
# Copy the source from the current directory to the Working Directory inside the container
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
RUN go build -o twcron .
|
RUN go build -o twcron ./cmd/cron
|
||||||
|
|
||||||
######## Start a new stage from scratch #######
|
######## Start a new stage from scratch #######
|
||||||
FROM alpine:latest
|
FROM alpine:latest
|
||||||
|
@ -28,7 +28,5 @@ ENV APP_MODE=production
|
||||||
EXPOSE 8080
|
EXPOSE 8080
|
||||||
|
|
||||||
RUN apk add --no-cache tzdata
|
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
|
|
@ -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
|
135
main.go
135
main.go
|
@ -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
|
|
||||||
}
|
|
Reference in New Issue