Maintenance mode using LockComponent

I have implemented a maintenance mode in my symfony 5.1.8 app. It uses the Lock Component.

The core is a request listener and a helper service:

My issue: Some subrequests get caught in my listener.

class MaintenanceModeRequestListener
{
    /** @var MaintenanceModeHelper */
    protected $lockHelper;

    /** @var UrlGeneratorInterface */
    protected $router;

    public function __construct(
        MaintenanceModeHelper $lockHelper,
        UrlGeneratorInterface $router
    )
    {
        $this->lockHelper = $lockHelper;
        $this->router = $router;
    }

    public function onKernelRequest(RequestEvent $event)
    {
        $inMMode = $this->lockHelper->isInMaintenanceMode();
        $requestIsMaintenance = $event->getRequest()->getRequestUri() === '/maintenance/';

        if($inMMode && !$requestIsMaintenance)

            $event->setResponse(new RedirectResponse($this->router->generate('show-maintenance-mode')));

        else if(!$inMMode && $requestIsMaintenance)

            $event->setResponse(new RedirectResponse($this->router->generate('index')));
    }
}

so any incoming request will check if the Lock is locked and proceed accordingly.

The helper is important:

class MaintenanceModeHelper
{
    protected $lock;

    public function __construct(LockFactory $lockFactory)
    {
        $this->lock = $lockFactory->createLock('maintenance', 5);
    }

    /**
     * @return bool true iff in maintenance mode
     */
    public function isInMaintenanceMode() {
        $obtained = $this->lock->acquire(false);
        if ($obtained) {
            $this->lock->release();
        }
        return !$obtained;
    }

    /**
     * @return bool true iff "I" am in the warmup command process
     */
    public function isAcquired() {
        return $this->lock->isAcquired();
    }

    public function acquire() {
        $acquireResult = $this->lock->acquire(false);
        return $acquireResult;
    }

    public function release() {
        $result =  $this->lock->release();
        return $result;
    }
}

Now, here’s my problem: If the page has sub-requests, e.g. an iframe or image, such that the browser will issue a second request to the server, it does happen on some machines, that the second request gets caught in the request listener and is redirected to the maintenance page. I am no sure if "sub-request" is the correct term for the second request that loads an image resource or an iframe.

The following screenshot illustrates this issue:
enter image description here

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *