Skip to content

Commit

Permalink
βž• Add Cache connections test
Browse files Browse the repository at this point in the history
  • Loading branch information
bdsoha committed Jan 16, 2025
1 parent c67865f commit ea29561
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
27 changes: 27 additions & 0 deletions src/common/test_cache_connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

require '/laravel/vendor/autoload.php';

use Illuminate\Cache\CacheManager;

$app = require_once '/laravel/bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();

try {
$store = app(CacheManager::class)->store();
$testKey = 'connection_test_' . time();

$store->put($testKey, 'test_value', 1);

if ($store->get($testKey) === 'test_value') {
$store->forget($testKey);
exit(0);
}

echo 'Cache store is not working properly: Test value mismatch.';
exit(1);
} catch (Exception $e) {
echo 'Cache connection error: ' . $e->getMessage();
exit(1);
}
File renamed without changes.
43 changes: 31 additions & 12 deletions src/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,64 @@ set -e
: "${CONTAINER_WORKER_SLEEP:=5}"
: "${CONTAINER_WORKER_TIMEOUT:=300}"
: "${CONTAINER_WORKER_TRIES:=3}"

: "${TEST_DB_CONNECTION:=true}"
: "${TEST_CACHE_CONNECTION:=true}"
: "${TEST_CONNECTION_TIMEOUT:=20}"

: "${APP_ENV:=production}"
: "${APP_DEBUG:=false}"

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

_migrate() {
_test_connection() {
local count=0
local timeout=20

while [ "$count" -lt "$timeout" ]; do
php -f /common/test_db_connection.php > /dev/null 2>&1
local type="${1}"

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

if [ "$status" -eq 0 ]; then
echo "βœ… Database connection successful."
break
echo "βœ… ${type^} connection successful."
return 0
fi

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

if [ "$count" -eq "$timeout" ]; then
echo "β›” Database connection failed after multiple attempts."
exit 1
echo "β›” ${type^} connection failed after multiple attempts."
exit 1
}

_test_connections() {
if [ "$TEST_DB_CONNECTION" != "true" ]; then
echo "⏭ Skipping database connection test..."
else
_test_connection "db"
fi

if [ "$TEST_CACHE_CONNECTION" != "true" ]; then
echo "⏭ Skipping cache connection test..."
else
_test_connection "cache"
fi
}

_migrate() {
echo "πŸš€ Running migrations..."
${ARTISAN} migrate --force --isolated
}

_setup() {
if [ -n "$CONTAINER_MANUAL_SETUP" ]; then
echo "⏭: Skipping setup..."

return
fi

_test_connections
_migrate

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

0 comments on commit ea29561

Please sign in to comment.