Skip to content

Commit

Permalink
Add Dockerfile healthcheck
Browse files Browse the repository at this point in the history
WIP
  • Loading branch information
sbreker committed Apr 23, 2024
1 parent d4a731f commit eabf8a3
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 0 deletions.
22 changes: 22 additions & 0 deletions 1.1.21.2/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
FROM golang:1.22-alpine3.19 as builder

WORKDIR /go/src/app

# Copy the local package files to the container's workspace.
COPY ./healthcheck .

# Install Go dependencies and compile the static binary.
RUN go mod init healthcheck && \
go get -d -v github.com/mikespook/gearman-go && \
go get -d -v github.com/mikespook/golib/signal && \
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o ping_client client/ping.go && \
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o pong_worker worker/pong.go

FROM alpine:3.19.1

ENV GEARMAND_VERSION 1.1.21
Expand Down Expand Up @@ -51,6 +65,14 @@ RUN set -x \
&& apk del .build-deps \
&& /usr/local/sbin/gearmand --version

COPY --from=builder /go/src/app/ping_client /usr/local/bin/ping_client
COPY --from=builder /go/src/app/pong_worker /usr/local/bin/pong_worker

RUN chmod +x /usr/local/bin/ping_client /usr/local/bin/pong_worker

HEALTHCHECK --interval=30s --timeout=3s --retries=2 \
CMD test $(/usr/local/bin/ping_client | grep -c 'pong') -eq 1 || exit 1

COPY docker-entrypoint.sh /usr/local/bin/
RUN apk add --no-cache bash \
&& touch /etc/gearmand.conf && chown gearman:gearman /etc/gearmand.conf \
Expand Down
3 changes: 3 additions & 0 deletions 1.1.21.2/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,7 @@ function generate_config() {
if ! [ -s "${CONFIG_FILE}" ]; then # dont genarate config if current config file is not empty
generate_config
fi

/usr/local/bin/pong_worker &

exec "$@"
5 changes: 5 additions & 0 deletions 1.1.21.2/healthcheck/client/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module healthcheck

go 1.22.1

require github.com/mikespook/gearman-go v0.0.0-20220520031403-2a518e866145 // indirect
2 changes: 2 additions & 0 deletions 1.1.21.2/healthcheck/client/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/mikespook/gearman-go v0.0.0-20220520031403-2a518e866145 h1:6kTCi6p3Hd6JYROnq+1UOdewoXj90zKKDQPlsHYTSEs=
github.com/mikespook/gearman-go v0.0.0-20220520031403-2a518e866145/go.mod h1:77Th6O6AZfMU6i5hLJnjN5xxUBoio7LN0aOyxGhqV1U=
50 changes: 50 additions & 0 deletions 1.1.21.2/healthcheck/client/ping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"log"
"sync"

"github.com/mikespook/gearman-go/client"
)

func main() {
var wg sync.WaitGroup

c, err := client.New(client.Network, "127.0.0.1:4730")
if err != nil {
log.Fatalln(err)
}
defer c.Close()
c.ErrorHandler = func(e error) {
log.Println(e)
}
echo := []byte("ping")
wg.Add(1)

_, err = c.Echo(echo)
if err != nil {
log.Fatalln(err)
}
wg.Done()
jobHandler := func(resp *client.Response) {
if string(resp.Data) == "pong" {
log.Println("Health check passed")
} else {
log.Println("Health check failed")
}
log.Printf("%s", resp.Data)
wg.Done()
}
handle, err := c.Do("ping", echo, client.JobNormal, jobHandler)
if err != nil {
log.Fatalln(err)
}
wg.Add(1)
status, err := c.Status(handle)
if err != nil {
log.Fatalln(err)
}
log.Printf("%v", *status)

wg.Wait()
}
9 changes: 9 additions & 0 deletions 1.1.21.2/healthcheck/worker/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module healthcheck

go 1.22.1

require (
github.com/mikespook/gearman-go v0.0.0-20220520031403-2a518e866145 // indirect
github.com/mikespook/golib v0.0.0-20151119134446-38fe6917d34b // indirect
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect
)
6 changes: 6 additions & 0 deletions 1.1.21.2/healthcheck/worker/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
github.com/mikespook/gearman-go v0.0.0-20220520031403-2a518e866145 h1:6kTCi6p3Hd6JYROnq+1UOdewoXj90zKKDQPlsHYTSEs=
github.com/mikespook/gearman-go v0.0.0-20220520031403-2a518e866145/go.mod h1:77Th6O6AZfMU6i5hLJnjN5xxUBoio7LN0aOyxGhqV1U=
github.com/mikespook/golib v0.0.0-20151119134446-38fe6917d34b h1:v99/2GZfv/W/HuD17/QcHucnDtUsY9strTTwG82EuZY=
github.com/mikespook/golib v0.0.0-20151119134446-38fe6917d34b/go.mod h1:3lBg8mdFpiF0C4ibbSFsxr+rgPWWkFlEVD/s11OG5k4=
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 h1:VpOs+IwYnYBaFnrNAeB8UUWtL3vEUnzSCL1nVjPhqrw=
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
44 changes: 44 additions & 0 deletions 1.1.21.2/healthcheck/worker/pong.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"log"
"os"
"time"

"github.com/mikespook/gearman-go/worker"
"github.com/mikespook/golib/signal"
)

func ping(job worker.Job) ([]byte, error) {
log.Printf("Received job: %s", job.Data())
return []byte("pong"), nil
}

func main() {
w := worker.New(worker.Unlimited)
defer w.Close()

connect := func() error {
return w.AddServer("tcp4", "localhost:4730")
}
for i := 0; i < 5; i++ {
err := connect()
if err == nil {
break
}
if i == 4 {
log.Fatalf("Failed to connect to gearman server after several retries: %v", err)
}
log.Printf("Could not connect to server: %v. Retrying in 5 seconds...", err)
time.Sleep(5 * time.Second)
}
w.AddFunc("ping", ping, worker.Unlimited)

if err := w.Ready(); err != nil {
log.Fatal(err)
return
}
go w.Work()
signal.Bind(os.Interrupt, func() uint { return signal.BreakExit })
signal.Wait()
}

0 comments on commit eabf8a3

Please sign in to comment.