From b3bdd9b79e75e224d3dc68b89dc7efb92c754a9e Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 17 Mar 2018 23:07:38 +0800 Subject: [PATCH] feat(docker): test a container to check that it is still working (#346) --- Dockerfile | 4 ++++ Dockerfile.alpine | 3 +++ Dockerfile.arm | 3 +++ Dockerfile.arm64 | 3 +++ Dockerfile.i386 | 3 +++ Dockerfile.windows | 3 +++ main.go | 25 +++++++++++++++++++++++++ 7 files changed, 44 insertions(+) diff --git a/Dockerfile b/Dockerfile index 89ebd59..765d3c3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,4 +10,8 @@ LABEL maintainer="Bo-Yi Wu " ADD release/linux/amd64/gorush /bin/ EXPOSE 8088 9000 + +HEALTHCHECK --start-period=2s --interval=10s --timeout=5s \ + CMD ["/bin/gorush", "--ping"] + ENTRYPOINT ["/bin/gorush"] diff --git a/Dockerfile.alpine b/Dockerfile.alpine index ab56596..1f839f2 100644 --- a/Dockerfile.alpine +++ b/Dockerfile.alpine @@ -13,4 +13,7 @@ RUN apk add --no-cache ca-certificates && \ ADD release/linux/amd64/gorush /bin/ EXPOSE 8088 9000 +HEALTHCHECK --start-period=2s --interval=10s --timeout=5s \ + CMD ["/bin/gorush", "--ping"] + ENTRYPOINT ["/bin/gorush"] diff --git a/Dockerfile.arm b/Dockerfile.arm index 50b0ae1..fb0a81b 100644 --- a/Dockerfile.arm +++ b/Dockerfile.arm @@ -10,4 +10,7 @@ LABEL maintainer="Bo-Yi Wu " ADD release/linux/arm/gorush /bin/ EXPOSE 8088 9000 +HEALTHCHECK --start-period=2s --interval=10s --timeout=5s \ + CMD ["/bin/gorush", "--ping"] + ENTRYPOINT ["/bin/gorush"] diff --git a/Dockerfile.arm64 b/Dockerfile.arm64 index c50ea45..bd7794f 100644 --- a/Dockerfile.arm64 +++ b/Dockerfile.arm64 @@ -10,4 +10,7 @@ LABEL maintainer="Bo-Yi Wu " ADD release/linux/arm64/gorush /bin/ EXPOSE 8088 9000 +HEALTHCHECK --start-period=2s --interval=10s --timeout=5s \ + CMD ["/bin/gorush", "--ping"] + ENTRYPOINT ["/bin/gorush"] diff --git a/Dockerfile.i386 b/Dockerfile.i386 index 93d2162..e7e84ec 100644 --- a/Dockerfile.i386 +++ b/Dockerfile.i386 @@ -10,4 +10,7 @@ LABEL maintainer="Bo-Yi Wu " ADD release/linux/i386/gorush /bin/ EXPOSE 8088 9000 +HEALTHCHECK --start-period=2s --interval=10s --timeout=5s \ + CMD ["/bin/gorush", "--ping"] + ENTRYPOINT ["/bin/gorush"] diff --git a/Dockerfile.windows b/Dockerfile.windows index 452ecb8..06c318f 100644 --- a/Dockerfile.windows +++ b/Dockerfile.windows @@ -6,4 +6,7 @@ LABEL maintainer="Bo-Yi Wu " \ org.label-schema.schema-version="1.0" ADD release/gorush.exe /gorush.exe +HEALTHCHECK --start-period=2s --interval=10s --timeout=5s \ + CMD ["\\gorush.exe", "--ping"] + ENTRYPOINT [ "\\gorush.exe" ] diff --git a/main.go b/main.go index 9893a2c..09e4cb3 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "log" + "net/http" "os" "path/filepath" "strconv" @@ -19,6 +20,7 @@ func main() { opts := config.ConfYaml{} var ( + ping bool showVersion bool configFile string topic string @@ -56,6 +58,7 @@ func main() { flag.BoolVar(&opts.Ios.Production, "production", false, "production mode in iOS") flag.StringVar(&topic, "topic", "", "apns topic in iOS") flag.StringVar(&proxy, "proxy", "", "http proxy url") + flag.BoolVar(&ping, "ping", false, "ping server") flag.Usage = usage flag.Parse() @@ -126,6 +129,13 @@ func main() { } } + if ping { + if err := pinger(); err != nil { + gorush.LogError.Warnf("ping server error: %v", err) + } + return + } + // send android notification if opts.Android.Enabled { gorush.PushConf.Android.Enabled = opts.Android.Enabled @@ -270,6 +280,7 @@ Server Options: --proxy Proxy URL (only for GCM) --pid Process identifier path --redis-addr Redis addr (default: localhost:6379) + --ping healthy check command for container iOS Options: -i, --key certificate key file path -P, --password certificate key password @@ -290,6 +301,20 @@ func usage() { os.Exit(0) } +// handles pinging the endpoint and returns an error if the +// agent is in an unhealthy state. +func pinger() error { + resp, err := http.Get("http://localhost:" + gorush.PushConf.Core.Port + gorush.PushConf.API.HealthURI) + if err != nil { + return err + } + defer resp.Body.Close() + if resp.StatusCode != 200 { + return fmt.Errorf("server returned non-200 status code") + } + return nil +} + func createPIDFile() error { if !gorush.PushConf.Core.PID.Enabled { return nil