-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test to make sure healthcheck works
Allow fpm status from ipv6 localhost
- Loading branch information
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import plugins.TestsPlugin.DockerComposeUp | ||
import plugins.TestsPlugin.DockerComposeUp.Companion.pool | ||
import java.lang.Thread.sleep | ||
import java.time.Duration.ofSeconds | ||
import java.util.concurrent.CompletableFuture.supplyAsync | ||
import java.io.ByteArrayOutputStream | ||
|
||
tasks.named<DockerComposeUp>("test") { | ||
doFirst { | ||
supplyAsync( | ||
{ | ||
val maxAttempts = 10 | ||
val delayBetweenAttempts = 5000L // 5 seconds in milliseconds | ||
var attempt = 0 | ||
var foundHealthyService = false | ||
|
||
while (attempt < maxAttempts) { | ||
attempt++ | ||
val outputStream = ByteArrayOutputStream() | ||
project.exec { | ||
commandLine = baseArguments + listOf("ps", "--all") | ||
standardOutput = outputStream | ||
workingDir = project.projectDir | ||
} | ||
val output = outputStream.toString() | ||
|
||
// Regex to match a healthy service in the ps output | ||
val healthyServicePattern = """(?m)^.+\s+.+\s+Up \d+ seconds \(healthy\).*$""".toRegex() | ||
foundHealthyService = output.lines().any { line -> | ||
healthyServicePattern.matches(line) | ||
} | ||
|
||
if (foundHealthyService) { | ||
println("Healthy service found on attempt $attempt.") | ||
project.exec { | ||
commandLine = baseArguments + listOf("stop") | ||
standardOutput = outputStream | ||
workingDir = project.projectDir | ||
} | ||
break | ||
} | ||
|
||
if (attempt < maxAttempts) { | ||
println("No healthy service found. Retrying in ${delayBetweenAttempts / 1000} seconds...") | ||
sleep(delayBetweenAttempts) | ||
} | ||
} | ||
|
||
// Throw an exception if no healthy service was found after all attempts | ||
if (!foundHealthyService) { | ||
throw GradleException("No service is marked as healthy in docker compose ps output after $maxAttempts attempts.") | ||
} | ||
}, pool | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
|
||
# Common to all services | ||
x-common: &common | ||
restart: "no" | ||
|
||
name: nginx-servicehealthcheck | ||
services: | ||
nginx: | ||
<<: *common | ||
image: ${NGINX:-islandora/nginx:local} | ||
healthcheck: | ||
interval: 10s | ||
retries: 3 | ||
start_period: 10s | ||
volumes: | ||
- ./test.sh:/test.sh # Test to run. | ||
command: | ||
- /test.sh # Run test and exit. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/command/with-contenv bash | ||
# shellcheck shell=bash | ||
|
||
on_terminate() { | ||
echo "Termination signal received. Exiting..." | ||
exit 0 | ||
} | ||
trap 'on_terminate' SIGTERM | ||
|
||
# Wait for Nginx to be ready. | ||
s6-svwait -U /run/service/nginx | ||
|
||
sleep 60 | ||
|
||
# Service must start for us to get to this point. | ||
exit 1 |