clockwork icon indicating copy to clipboard operation
clockwork copied to clipboard

On Demand not working with Laravel Octane

Open finalgamer opened this issue 3 years ago • 6 comments

When running with a configured on demand key no requests are captured.

My investigation shows that this is caused because the service provider is only booted on startup of Octane and the event listeners and middleware are not registered.

https://github.com/itsgoingd/clockwork/blob/bd13e765a6d77d95a6d6ecf9842a7d02382ff39d/Clockwork/Support/Laravel/ClockworkServiceProvider.php#L27-L30 Line 27 will evaluate to false.

Analysis

When the Octane server boots up and registers the service providers and a new empty request is created in the support class.

https://github.com/itsgoingd/clockwork/blob/bd13e765a6d77d95a6d6ecf9842a7d02382ff39d/Clockwork/Support/Laravel/ClockworkSupport.php#L661-L672

This empty request does not contain the on demand key and thus will fail the shouldCollect() call on Line 54

https://github.com/itsgoingd/clockwork/blob/bd13e765a6d77d95a6d6ecf9842a7d02382ff39d/Clockwork/Support/Laravel/ClockworkSupport.php#L549-L555

The end result is that the middleware and event listeners are not registered.

finalgamer avatar Aug 08 '22 04:08 finalgamer

@finalgamer have you tried adding Clockwork class to the flush list in config/octane.php file?

flexchar avatar Aug 10 '22 12:08 flexchar

Hey, thanks for the detailed report, this indeed seems to be a bug, I will have to take a closer look.

Though be aware that the Octane support currently has a known limitation, where the Clockwork profiling is always active, even for filtered requests. If you are planning to use the on-demand mode to avoid the performance overhead from running Clockwork for all requests, this unfortunately won't work atm.

itsgoingd avatar Aug 10 '22 12:08 itsgoingd

@finalgamer have you tried adding Clockwork class to the flush list in config/octane.php file?

@flexchar What class are you talking about?

finalgamer avatar Aug 11 '22 08:08 finalgamer

Flushing the resolved instances did not work for me unfortuantely, but for now I got it working by focefully reloading the ClockworkServiceProvider on every request using a Listener:

<?php

namespace App\Listeners;

use Clockwork\Support\Laravel\ClockworkServiceProvider;
use Laravel\Octane\Events\RequestReceived;

class ReloadClockworkProvider
{
    /**
     * Handle the event.
     *
     * @param  \Laravel\Octane\Events\RequestReceived  $event
     * @return void
     */
    public function handle(RequestReceived $event): void
    {
        $event->sandbox->register(ClockworkServiceProvider::class, true);
    }
}

and then register it inside config/octane.php

'listeners' => [
        ...
        RequestReceived::class => [
            ...
            ReloadClockworkProvider::class,
        ],

matthiasmetzen avatar Aug 13 '22 16:08 matthiasmetzen

@finalgamer I was talking about the main Clockwork class. Adding me helped to fix getting false warnings for duplicate queries.

image

// config/octane.php
    'flush' => [
        //
        \Clockwork\Clockwork::class,
    ],

flexchar avatar Aug 30 '22 06:08 flexchar

@flexchar That sounds like something we should actually fix in Clockwork itself. :)

itsgoingd avatar Aug 30 '22 20:08 itsgoingd