Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On Demand not working with Laravel Octane #592

Open
finalgamer opened this issue Aug 8, 2022 · 6 comments
Open

On Demand not working with Laravel Octane #592

finalgamer opened this issue Aug 8, 2022 · 6 comments
Milestone

Comments

@finalgamer
Copy link

finalgamer commented Aug 8, 2022

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.

if ($this->app['clockwork.support']->isCollectingData()) {
$this->registerEventListeners();
$this->registerMiddleware();
}

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.

// Make an incoming request instance
protected function incomingRequest()
{
if ($this->incomingRequest) return $this->incomingRequest;
return $this->incomingRequest = new IncomingRequest([
'method' => $this->app['request']->getMethod(),
'uri' => $this->app['request']->getRequestUri(),
'input' => $this->app['request']->input(),
'cookies' => $this->app['request']->cookie()
]);
}

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

// Check whether we are collecting http requests
public function isCollectingRequests()
{
return ($this->isEnabled() || $this->getConfig('collect_data_always', false))
&& ! $this->app->runningInConsole()
&& $this->app['clockwork']->shouldCollect()->filter($this->incomingRequest());
}

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

@flexchar
Copy link

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

@itsgoingd
Copy link
Owner

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
Copy link
Author

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

@flexchar What class are you talking about?

@matthiasmetzen
Copy link

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
Copy link

@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
Copy link
Owner

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

@itsgoingd itsgoingd added this to the Clockwork 6 milestone Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants