mirror of
https://github.com/ansible-collections/community.docker.git
synced 2026-07-29 11:55:04 +00:00
Improve health check image. (#919)
This commit is contained in:
@@ -13,7 +13,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
data, err := os.ReadFile("health.txt")
|
||||
data, err := os.ReadFile("/health.txt")
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error while reading health status: %s\n", err)
|
||||
os.Exit(1)
|
||||
|
||||
@@ -14,18 +14,38 @@ import (
|
||||
|
||||
func main() {
|
||||
os.WriteFile("health.txt", []byte("starting"), 0644)
|
||||
if len(os.Args) != 2 {
|
||||
fmt.Fprintf(os.Stderr, "%s must have 1 argument, not %d arguments\n", os.Args[0], len(os.Args))
|
||||
if len(os.Args) > 3 || len(os.Args) < 2 {
|
||||
fmt.Fprintf(os.Stderr, "%s must have 1 or 2 arguments, not %d arguments\n", os.Args[0], len(os.Args))
|
||||
os.Exit(1)
|
||||
}
|
||||
delay, err := time.ParseDuration(os.Args[1])
|
||||
runtimeDelay, err := time.ParseDuration(os.Args[1])
|
||||
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)
|
||||
}
|
||||
if delay.Microseconds() <= 0 {
|
||||
if runtimeDelay.Microseconds() <= 0 {
|
||||
fmt.Fprintf(os.Stderr, "Delay must be positive!\n")
|
||||
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")
|
||||
}
|
||||
|
||||
@@ -31,5 +31,5 @@ func main() {
|
||||
} else {
|
||||
state = []byte("unhealthy")
|
||||
}
|
||||
os.WriteFile("health.txt", state, 0644)
|
||||
os.WriteFile("/health.txt", state, 0644)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user