itsgoingd / clockwork

Clockwork - php dev tools in your browser - server-side component
https://underground.works/clockwork
MIT License
5.68k stars 321 forks source link

On Demand not working with Laravel Octane #592

Open finalgamer opened 2 years ago

finalgamer commented 2 years ago

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.

flexchar commented 2 years ago

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

itsgoingd commented 2 years ago

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.

finalgamer commented 2 years ago

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

@flexchar What class are you talking about?

matthiasmetzen commented 2 years ago

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,
        ],
flexchar commented 2 years ago

@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,
    ],
itsgoingd commented 2 years ago

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