2
0

HealthCheckController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Services\OpenSearchService;
  4. use App\Services\RabbitMQService;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Http\Response;
  7. use Illuminate\Support\Facades\Cache;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Log;
  10. class HealthCheckController extends Controller
  11. {
  12. /**
  13. * Display a listing of the resource.
  14. *
  15. * @return Response
  16. */
  17. public function index()
  18. {
  19. //
  20. if (file_exists(base_path('.stop'))) {
  21. return response()->json(
  22. ['createdAt' => now(), 'checks' => []],
  23. 503,
  24. ['Content-Type' => 'application/json;charset=UTF-8', 'Charset' => 'utf-8'],
  25. JSON_UNESCAPED_UNICODE
  26. );
  27. }
  28. $checks = [];
  29. $healthy = true;
  30. // Cache
  31. try {
  32. Cache::put('health_check', true, 5);
  33. Cache::get('health_check');
  34. $checks['cache'] = true;
  35. } catch (\Throwable $e) {
  36. $checks['cache'] = false;
  37. $healthy = false;
  38. Log::error('Health check failed: cache', ['error' => $e->getMessage()]);
  39. }
  40. // Database
  41. try {
  42. DB::connection()->getPdo();
  43. $checks['db'] = true;
  44. } catch (\Throwable $e) {
  45. $checks['db'] = false;
  46. $healthy = false;
  47. Log::error('Health check failed: db', ['error' => $e->getMessage()]);
  48. }
  49. // OpenSearch
  50. try {
  51. $service = app(OpenSearchService::class);
  52. [$ok, $msg] = $service->testConnection();
  53. if (! $ok) {
  54. throw new \Exception($msg);
  55. }
  56. $checks['opensearch'] = true;
  57. } catch (\Throwable $e) {
  58. $checks['opensearch'] = false;
  59. $healthy = false;
  60. Log::error('Health check failed: opensearch', ['error' => $e->getMessage()]);
  61. }
  62. // RabbitMQ
  63. try {
  64. $service = app(RabbitMQService::class);
  65. $service->isConnected() ? null : throw new \Exception('Not connected');
  66. $checks['rabbitmq'] = true;
  67. } catch (\Throwable $e) {
  68. $checks['rabbitmq'] = false;
  69. $healthy = false;
  70. Log::error('Health check failed: rabbitmq', ['error' => $e->getMessage()]);
  71. }
  72. return response()->json(
  73. [
  74. 'createdAt' => now(),
  75. 'services' => $checks,
  76. 'host' => $_SERVER['HTTP_HOST'],
  77. 'document-root' => $_SERVER['DOCUMENT_ROOT'],
  78. ],
  79. $healthy ? 200 : 500,
  80. ['Content-Type' => 'application/json;charset=UTF-8', 'Charset' => 'utf-8'],
  81. JSON_UNESCAPED_UNICODE
  82. );
  83. }
  84. /**
  85. * Store a newly created resource in storage.
  86. *
  87. * @return Response
  88. */
  89. public function store(Request $request)
  90. {
  91. //
  92. }
  93. /**
  94. * Display the specified resource.
  95. *
  96. * @param int $id
  97. * @return Response
  98. */
  99. public function show($id)
  100. {
  101. //
  102. }
  103. /**
  104. * Update the specified resource in storage.
  105. *
  106. * @param int $id
  107. * @return Response
  108. */
  109. public function update(Request $request, $id)
  110. {
  111. //
  112. }
  113. /**
  114. * Remove the specified resource from storage.
  115. *
  116. * @param int $id
  117. * @return Response
  118. */
  119. public function destroy($id)
  120. {
  121. //
  122. }
  123. }