health_check.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. use Magento\Framework\Config\ConfigOptionsListConstants;
  7. register_shutdown_function("fatalErrorHandler");
  8. try {
  9. require __DIR__ . '/../app/bootstrap.php';
  10. /** @var \Magento\Framework\App\ObjectManagerFactory $objectManagerFactory */
  11. $objectManagerFactory = \Magento\Framework\App\Bootstrap::createObjectManagerFactory(BP, []);
  12. /** @var \Magento\Framework\ObjectManagerInterface $objectManager */
  13. $objectManager = $objectManagerFactory->create([]);
  14. /** @var \Magento\Framework\App\DeploymentConfig $deploymentConfig */
  15. $deploymentConfig = $objectManager->get(\Magento\Framework\App\DeploymentConfig::class);
  16. /** @var \Psr\Log\LoggerInterface $logger */
  17. $logger = $objectManager->get(\Psr\Log\LoggerInterface::class);
  18. } catch (\Exception $e) {
  19. http_response_code(500);
  20. exit(1);
  21. }
  22. // check mysql connectivity
  23. foreach ($deploymentConfig->get(ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTIONS) as $connectionData) {
  24. try {
  25. /** @var \Magento\Framework\DB\Adapter\Pdo\Mysql $dbAdapter */
  26. $dbAdapter = $objectManager->create(
  27. \Magento\Framework\DB\Adapter\Pdo\Mysql::class,
  28. ['config' => $connectionData]
  29. );
  30. $dbAdapter->getConnection();
  31. } catch (\Exception $e) {
  32. http_response_code(500);
  33. $logger->error("MySQL connection failed: " . $e->getMessage());
  34. exit(1);
  35. }
  36. }
  37. // check cache storage availability
  38. $cacheConfigs = $deploymentConfig->get(ConfigOptionsListConstants::KEY_CACHE_FRONTEND);
  39. if ($cacheConfigs) {
  40. foreach ($cacheConfigs as $cacheConfig) {
  41. if (!isset($cacheConfig[ConfigOptionsListConstants::CONFIG_PATH_BACKEND]) ||
  42. !isset($cacheConfig[ConfigOptionsListConstants::CONFIG_PATH_BACKEND_OPTIONS])) {
  43. http_response_code(500);
  44. $logger->error("Cache configuration is invalid");
  45. exit(1);
  46. }
  47. $cacheBackendClass = $cacheConfig[ConfigOptionsListConstants::CONFIG_PATH_BACKEND];
  48. try {
  49. /** @var \Zend_Cache_Backend_Interface $backend */
  50. $backend = new $cacheBackendClass($cacheConfig[ConfigOptionsListConstants::CONFIG_PATH_BACKEND_OPTIONS]);
  51. $backend->test('test_cache_id');
  52. } catch (\Exception $e) {
  53. http_response_code(500);
  54. $logger->error("Cache storage is not accessible");
  55. exit(1);
  56. }
  57. }
  58. }
  59. /**
  60. * Handle any fatal errors
  61. *
  62. * @return void
  63. */
  64. function fatalErrorHandler()
  65. {
  66. $error = error_get_last();
  67. if ($error !== null) {
  68. http_response_code(500);
  69. }
  70. }