From 328db49da4c103e5c010f3147b37cdf0e5ea2a1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Repe=C4=87?= Date: Wed, 7 Sep 2022 18:32:33 +0200 Subject: [PATCH] Fetch worker (#3) * Add fetch_worker docker file * Expose http liveness check from fetch worker --- fetch_worker.go | 21 ++++++++++++++++++--- ops/docker/fetch_worker/Dockerfile | 27 +++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 ops/docker/fetch_worker/Dockerfile diff --git a/fetch_worker.go b/fetch_worker.go index 224c968..146951c 100644 --- a/fetch_worker.go +++ b/fetch_worker.go @@ -1,23 +1,38 @@ package main import ( + "fmt" "log" + "net/http" + "time" "github.com/pkg/errors" "github.com/robfig/cron/v3" + "github.com/spf13/viper" "gorm.io/gorm" ) func runFetchWorker(db *gorm.DB, api *GitHubAPI) error { log.Println("Running fetch_worker") + start := time.Now() + + // Expose simple http liveness check + go func() { + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Late Night Commits fetch worker running since: %v", start) + }) + + log.Fatalln(http.ListenAndServe(":"+viper.GetString("port"), nil)) + }() + + // Run fetch job on a schedule scheduler := cron.New() - _, err := scheduler.AddFunc("*/10 * * * *", func() { + if _, err := scheduler.AddFunc("*/10 * * * *", func() { if err := runFetchJob(db, api); err != nil { log.Printf("Fetch job error: %v", err) } - }) - if err != nil { + }); err != nil { return errors.Wrap(err, "failed to schedule fetch job worker") } diff --git a/ops/docker/fetch_worker/Dockerfile b/ops/docker/fetch_worker/Dockerfile new file mode 100644 index 0000000..bb9bf96 --- /dev/null +++ b/ops/docker/fetch_worker/Dockerfile @@ -0,0 +1,27 @@ +### Builder +FROM golang:1.17 as builder + +WORKDIR /app + +COPY go.mod . +COPY go.sum . + +RUN go mod download + +COPY . . + +RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main . + +### Runner +FROM alpine:latest + +RUN apk add --no-cache ca-certificates + +WORKDIR /app + +COPY --from=builder /app . + +ENV PORT=3000 +EXPOSE 3000 + +CMD ["./main", "-mode=fetch_worker"] \ No newline at end of file