Skip to content

Commit

Permalink
Fetch worker (#3)
Browse files Browse the repository at this point in the history
* Add fetch_worker docker file

* Expose http liveness check from fetch worker
  • Loading branch information
Flexicon authored Sep 7, 2022
1 parent a5f4c96 commit 328db49
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
21 changes: 18 additions & 3 deletions fetch_worker.go
Original file line number Diff line number Diff line change
@@ -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")
}

Expand Down
27 changes: 27 additions & 0 deletions ops/docker/fetch_worker/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]

0 comments on commit 328db49

Please sign in to comment.