Procházet zdrojové kódy

添加 redis , opensearch mq, db检测

visuddhinanda před 2 dny
rodič
revize
203a8e47a5

+ 66 - 9
api-v13/app/Http/Controllers/HealthCheckController.php

@@ -3,6 +3,11 @@
 namespace App\Http\Controllers;
 namespace App\Http\Controllers;
 
 
 use Illuminate\Http\Request;
 use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\DB;
+use App\Services\RabbitMQService;
+use App\Services\OpenSearchService;
 
 
 class HealthCheckController extends Controller
 class HealthCheckController extends Controller
 {
 {
@@ -14,16 +19,68 @@ class HealthCheckController extends Controller
     public function index()
     public function index()
     {
     {
         //
         //
-        if(file_exists(base_path('.stop'))){
-            $status = 503;
-        }else{
-            $status = 200;
+        if (file_exists(base_path('.stop'))) {
+            return response()->json(
+                ['createdAt' => now(), 'checks' => []],
+                503,
+                ['Content-Type' => 'application/json;charset=UTF-8', 'Charset' => 'utf-8'],
+                JSON_UNESCAPED_UNICODE
+            );
         }
         }
-        return response()->json(['createdAt' => now()],
-            $status,
-            ['Content-Type' => 'application/json;charset=UTF-8',
-            'Charset' => 'utf-8'],
-            JSON_UNESCAPED_UNICODE);
+
+        $checks = [];
+        $healthy = true;
+
+        // Cache
+        try {
+            Cache::put('health_check', true, 5);
+            Cache::get('health_check');
+            $checks['cache'] = 'true';
+        } catch (\Throwable $e) {
+            $checks['cache'] = 'failed';
+            $healthy = false;
+            Log::error('Health check failed: cache', ['error' => $e->getMessage()]);
+        }
+
+        // Database
+        try {
+            DB::connection()->getPdo();
+            $checks['db'] = 'true';
+        } catch (\Throwable $e) {
+            $checks['db'] = 'failed';
+            $healthy = false;
+            Log::error('Health check failed: db', ['error' => $e->getMessage()]);
+        }
+
+        // OpenSearch
+        try {
+            $service = app(OpenSearchService::class);
+            [$ok, $msg] = $service->testConnection();
+            if (!$ok) throw new \Exception($msg);
+            $checks['opensearch'] = 'true';
+        } catch (\Throwable $e) {
+            $checks['opensearch'] = 'fail';
+            $healthy = false;
+            Log::error('Health check failed: opensearch', ['error' => $e->getMessage()]);
+        }
+
+        // RabbitMQ
+        try {
+            $service = app(RabbitMQService::class);
+            $service->isConnected() ? null : throw new \Exception('Not connected');
+            $checks['rabbitmq'] = 'true';
+        } catch (\Throwable $e) {
+            $checks['rabbitmq'] = 'failed';
+            $healthy = false;
+            Log::error('Health check failed: rabbitmq', ['error' => $e->getMessage()]);
+        }
+
+        return response()->json(
+            ['createdAt' => now(), 'checks' => $checks],
+            $healthy ? 200 : 500,
+            ['Content-Type' => 'application/json;charset=UTF-8', 'Charset' => 'utf-8'],
+            JSON_UNESCAPED_UNICODE
+        );
     }
     }
 
 
     /**
     /**