From ea29561698cbbe94149b3d7264c956324ecea4c4 Mon Sep 17 00:00:00 2001 From: Dov Benyomin Sohacheski Date: Thu, 16 Jan 2025 09:35:27 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9E=95=20Add=20Cache=20connections=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/test_cache_connection.php | 27 ++++++++++++ ...ction.php => test_database_connection.php} | 0 src/entrypoint.sh | 43 +++++++++++++------ 3 files changed, 58 insertions(+), 12 deletions(-) create mode 100644 src/common/test_cache_connection.php rename src/common/{test_db_connection.php => test_database_connection.php} (100%) diff --git a/src/common/test_cache_connection.php b/src/common/test_cache_connection.php new file mode 100644 index 0000000..5168d89 --- /dev/null +++ b/src/common/test_cache_connection.php @@ -0,0 +1,27 @@ +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); +} diff --git a/src/common/test_db_connection.php b/src/common/test_database_connection.php similarity index 100% rename from src/common/test_db_connection.php rename to src/common/test_database_connection.php diff --git a/src/entrypoint.sh b/src/entrypoint.sh index 200508b..56e17df 100644 --- a/src/entrypoint.sh +++ b/src/entrypoint.sh @@ -8,34 +8,53 @@ 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 } @@ -43,10 +62,10 @@ _migrate() { _setup() { if [ -n "$CONTAINER_MANUAL_SETUP" ]; then echo "⏭: Skipping setup..." - return fi + _test_connections _migrate if [ -d "/laravel/app/public/storage" ]; then