2
0

HealthCheckController.php 3.4 KB

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