Skip to content

Commit ea29561

Browse files
committed
➕ Add Cache connections test
1 parent c67865f commit ea29561

File tree

3 files changed

+58
-12
lines changed

3 files changed

+58
-12
lines changed

src/common/test_cache_connection.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
require '/laravel/vendor/autoload.php';
4+
5+
use Illuminate\Cache\CacheManager;
6+
7+
$app = require_once '/laravel/bootstrap/app.php';
8+
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
9+
$kernel->bootstrap();
10+
11+
try {
12+
$store = app(CacheManager::class)->store();
13+
$testKey = 'connection_test_' . time();
14+
15+
$store->put($testKey, 'test_value', 1);
16+
17+
if ($store->get($testKey) === 'test_value') {
18+
$store->forget($testKey);
19+
exit(0);
20+
}
21+
22+
echo 'Cache store is not working properly: Test value mismatch.';
23+
exit(1);
24+
} catch (Exception $e) {
25+
echo 'Cache connection error: ' . $e->getMessage();
26+
exit(1);
27+
}

src/entrypoint.sh

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,64 @@ set -e
88
: "${CONTAINER_WORKER_SLEEP:=5}"
99
: "${CONTAINER_WORKER_TIMEOUT:=300}"
1010
: "${CONTAINER_WORKER_TRIES:=3}"
11+
12+
: "${TEST_DB_CONNECTION:=true}"
13+
: "${TEST_CACHE_CONNECTION:=true}"
14+
: "${TEST_CONNECTION_TIMEOUT:=20}"
15+
1116
: "${APP_ENV:=production}"
17+
: "${APP_DEBUG:=false}"
1218

1319
ARTISAN="php -d variables_order=EGPCS /laravel/artisan"
1420

15-
_migrate() {
21+
_test_connection() {
1622
local count=0
17-
local timeout=20
18-
19-
while [ "$count" -lt "$timeout" ]; do
20-
php -f /common/test_db_connection.php > /dev/null 2>&1
23+
local type="${1}"
2124

25+
while [ "$count" -lt "$TEST_CONNECTION_TIMEOUT" ]; do
26+
php -f "/common/test_${type}_connection.php" > /dev/null 2>&1
2227
status=$?
2328

2429
if [ "$status" -eq 0 ]; then
25-
echo "Database connection successful."
26-
break
30+
echo "${type^} connection successful."
31+
return 0
2732
fi
2833

29-
echo "⏱ Waiting on database connection, retrying... $((timeout - count)) seconds left"
34+
echo "⏱ Waiting on $type connection, retrying... $((TEST_CONNECTION_TIMEOUT - count)) seconds left"
3035
count=$((count + 1))
3136
sleep 1
3237
done
3338

34-
if [ "$count" -eq "$timeout" ]; then
35-
echo "⛔ Database connection failed after multiple attempts."
36-
exit 1
39+
echo "${type^} connection failed after multiple attempts."
40+
exit 1
41+
}
42+
43+
_test_connections() {
44+
if [ "$TEST_DB_CONNECTION" != "true" ]; then
45+
echo "⏭ Skipping database connection test..."
46+
else
47+
_test_connection "db"
3748
fi
3849

50+
if [ "$TEST_CACHE_CONNECTION" != "true" ]; then
51+
echo "⏭ Skipping cache connection test..."
52+
else
53+
_test_connection "cache"
54+
fi
55+
}
56+
57+
_migrate() {
3958
echo "🚀 Running migrations..."
4059
${ARTISAN} migrate --force --isolated
4160
}
4261

4362
_setup() {
4463
if [ -n "$CONTAINER_MANUAL_SETUP" ]; then
4564
echo "⏭: Skipping setup..."
46-
4765
return
4866
fi
4967

68+
_test_connections
5069
_migrate
5170

5271
if [ -d "/laravel/app/public/storage" ]; then

0 commit comments

Comments
 (0)