Last active
October 19, 2020 16:29
-
-
Save Renrhaf/94785c4bf71e2f736d90f000a36725d6 to your computer and use it in GitHub Desktop.
Health check script for Drupal 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Health check controller for the API Indexer. | |
*/ | |
use Drupal\Core\Database\Database; | |
use Drupal\Core\DrupalKernel; | |
use Drupal\Core\Installer\InstallerKernel; | |
use Symfony\Component\HttpFoundation\JsonResponse; | |
use Symfony\Component\HttpFoundation\Request; | |
$autoloader = require 'autoload.php'; | |
$request = Request::createFromGlobals(); | |
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod'); | |
$result = ['status' => TRUE, 'message' => 'APIs are up and ready !']; | |
try { | |
// Initialize the DI container. | |
$kernel->boot(); | |
// Check if database is ready. | |
if (!Database::getConnectionInfo() && !InstallerKernel::installationAttempted() && PHP_SAPI !== 'cli') { | |
throw new \Exception('Deployment in progress... Thank you for your patience.'); | |
} | |
// Check if site is installed like the core do. | |
// This means checking for the session table. | |
// @see Drupal\Core\Installer\InstallerRedirectTrait::shouldRedirectToInstaller(). | |
$connection = $kernel->getContainer()->get('database'); | |
if (!$connection->schema()->tableExists('sessions')) { | |
throw new \Exception('Deployment in progress... Thank you for your patience.'); | |
} | |
// Get maintenance mode. | |
$maintenance = $kernel->getContainer()->get('state')->get('system.maintenance_mode'); | |
if ($maintenance) { | |
throw new \Exception('Website is currently under maintenance. We should be back shortly. Thank you for your patience.'); | |
} | |
} catch (\Exception $e) { | |
$result = ['status' => FALSE, 'message' => $e->getMessage()]; | |
} | |
// Return the JSON data. | |
$response = new JsonResponse($result); | |
$response->send(); | |
$kernel->terminate($request, $response); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment