Skip to content

Commit

Permalink
Healthcheck (#51)
Browse files Browse the repository at this point in the history
* healthcheck

* Fix error message on exit
  • Loading branch information
ostcar committed Mar 26, 2022
1 parent bf715f9 commit ac6723d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ COPY internal internal
# Build service in seperate stage.
FROM base as builder
RUN CGO_ENABLED=0 go build ./cmd/icc
RUN CGO_ENABLED=0 go build ./cmd/healthcheck


# Development build.
Expand All @@ -34,7 +35,9 @@ LABEL org.opencontainers.image.licenses="MIT"
LABEL org.opencontainers.image.source="https://github.com/OpenSlides/openslides-icc-service"

COPY --from=builder /root/icc .
COPY --from=builder /root/healthcheck .
EXPOSE 9007
ENV MESSAGING redis
ENV AUTH ticket
ENTRYPOINT ["/icc"]
HEALTHCHECK CMD ["/healthcheck"]
45 changes: 45 additions & 0 deletions cmd/healthcheck/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"fmt"
"io"
"net/http"
"os"
"strings"
)

func main() {
if err := run(); err != nil {
fmt.Printf("Error: %v", err)
os.Exit(1)
}
}

func run() error {
port, found := os.LookupEnv("ICC_PORT")
if !found {
port = "9007"
}

resp, err := http.Get("http://localhost:" + port + "/system/icc/health")
if err != nil {
return fmt.Errorf("sending request: %w", err)
}

if resp.StatusCode != 200 {
return fmt.Errorf("health returned status %s", resp.Status)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("reading response body: %w", err)
}

expect := `{"healthy":true}`
got := strings.TrimSpace(string(body))
if got != expect {
return fmt.Errorf("got `%s`, expected `%s`", body, expect)
}

return nil
}
2 changes: 1 addition & 1 deletion internal/icchttp/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func HandleHealth(mux *http.ServeMux) {
"/system/icc/health",
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/octet-stream")
fmt.Fprintln(w, `{"healthy": true}`)
fmt.Fprintln(w, `{"healthy":true}`)
},
)
}
4 changes: 4 additions & 0 deletions internal/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ func secret(name string, getSecret func(name string) (string, error), dev bool)

func buildErrHandler() func(err error) {
return func(err error) {
if errors.Is(err, context.Canceled) {
return
}

var closing interface {
Closing()
}
Expand Down

0 comments on commit ac6723d

Please sign in to comment.