Improve health check image. (#919)

This commit is contained in:
Felix Fontein 2024-07-07 21:19:26 +02:00 committed by GitHub
parent ff412f475e
commit 569880486f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 29 additions and 8 deletions

View File

@ -13,7 +13,7 @@ import (
) )
func main() { func main() {
data, err := os.ReadFile("health.txt") data, err := os.ReadFile("/health.txt")
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Error while reading health status: %s\n", err) fmt.Fprintf(os.Stderr, "Error while reading health status: %s\n", err)
os.Exit(1) os.Exit(1)

View File

@ -14,18 +14,38 @@ import (
func main() { func main() {
os.WriteFile("health.txt", []byte("starting"), 0644) os.WriteFile("health.txt", []byte("starting"), 0644)
if len(os.Args) != 2 { if len(os.Args) > 3 || len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "%s must have 1 argument, not %d arguments\n", os.Args[0], len(os.Args)) fmt.Fprintf(os.Stderr, "%s must have 1 or 2 arguments, not %d arguments\n", os.Args[0], len(os.Args))
os.Exit(1) os.Exit(1)
} }
delay, err := time.ParseDuration(os.Args[1]) runtimeDelay, err := time.ParseDuration(os.Args[1])
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Cannot parse delay duration: %s\n", err) fmt.Fprintf(os.Stderr, "Cannot parse runtime duration: %s\n", err)
os.Exit(1) os.Exit(1)
} }
if delay.Microseconds() <= 0 { if runtimeDelay.Microseconds() <= 0 {
fmt.Fprintf(os.Stderr, "Delay must be positive!\n") fmt.Fprintf(os.Stderr, "Delay must be positive!\n")
os.Exit(1) os.Exit(1)
} }
time.Sleep(delay) var healthyDelay time.Duration
if len(os.Args) == 3 {
healthyDelay, err = time.ParseDuration(os.Args[2])
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot parse healthy delay: %s\n", err)
os.Exit(1)
}
if healthyDelay.Microseconds() <= 0 {
fmt.Fprintf(os.Stderr, "Healthy delay must not be negative!\n")
os.Exit(1)
}
}
if healthyDelay.Microseconds() > 0 {
fmt.Fprintf(os.Stderr, "Waiting %s until setting to healthy...\n", healthyDelay)
time.Sleep(healthyDelay)
os.WriteFile("/health.txt", []byte("healthy"), 0644)
fmt.Fprintf(os.Stderr, "Set state to healthy.\n")
}
fmt.Fprintf(os.Stderr, "Waiting %s until quitting...\n", runtimeDelay)
time.Sleep(runtimeDelay)
fmt.Fprintf(os.Stderr, "Goodbye.\n")
} }

View File

@ -31,5 +31,5 @@ func main() {
} else { } else {
state = []byte("unhealthy") state = []byte("unhealthy")
} }
os.WriteFile("health.txt", state, 0644) os.WriteFile("/health.txt", state, 0644)
} }

View File

@ -19,3 +19,4 @@ docker_test_image_registry_nginx: quay.io/ansible/docker-test-containers:nginx-a
docker_test_image_registry: registry:2.6.1 docker_test_image_registry: registry:2.6.1
docker_test_image_simple_1: ghcr.io/ansible-collections/simple-1:tag docker_test_image_simple_1: ghcr.io/ansible-collections/simple-1:tag
docker_test_image_simple_2: ghcr.io/ansible-collections/simple-2:tag docker_test_image_simple_2: ghcr.io/ansible-collections/simple-2:tag
docker_test_image_healthcheck: ghcr.io/ansible-collections/healthcheck:check