I'm trying to replicate a solution I had working on Drupal 7 into Drupal 10. I was able to change the http response code to 504 such that Cloudflare would return a cached site copy in the instance of the database server being upgraded. I've got this far - using an Exception event subscriber - but my code never gets called. I've added the Request debug method - and that fires - I see those logs. However, when I turn the DB off - I just get the core FinalExceptionSubscriber firing. Could it be that my module never gets loaded during the bootstrap process?
<?php
namespace Drupal\neway_platform\EventSubscriber;
use Drupal\Core\Database\DatabaseConnectionRefusedException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class NewayExceptionSubscriber implements EventSubscriberInterface {
/**
* Handles exceptions for this subscriber.
*
* @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
* The event to process.
*/
public function onException(ExceptionEvent $event) {
//
error_log('Error here: ');
$exception = $event->getThrowable();
//$this->logger->get('php')->error('Custom logging message' . $exception->getMessage());
if ($exception instanceof DatabaseConnectionRefusedException) {
//$this->logger->get('php')->error('Custom logging message');
$response = new Response('Site is being upgraded - please check back soon', 504);
$event->setResponse($response);
}
}
public function onRequest() {
error_log('Your message here2: ');
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
// Listen on 4xx exceptions late, but before the final exception handler.
$events[KernelEvents::EXCEPTION][] = ['onException', 1000];
$events[KernelEvents::REQUEST][] = ['onRequest', 1000];
return $events;
}
mymodule.services.yml
services:
cron_fire_forget:
class: '\Drupal\neway_platform\Service\CronFireForget'
neway_exception:
class: Drupal\neway_platform\EventSubscriber\NewayExceptionSubscriber
tags:
- { name: 'event_subscriber' }
logs:
[02-Oct-2024 14:08:01 Europe/Berlin] Your message here2:
[02-Oct-2024 14:08:01 Europe/Berlin] Your message here2:
[02-Oct-2024 14:08:01 Europe/Berlin] Your message here2:
[02-Oct-2024 14:08:20 Europe/Berlin] Drupal\Core\Database\DatabaseConnectionRefusedException: SQLST (edited)
AuthenticationSubscriber
. If the event handler is not called, there must be an issue somewhere else.