upscalesoftware / swoole-reflection

Reflection API for Swoole web-server
Apache License 2.0
1 stars 1 forks source link

UnexpectedValueException in Laravel Octane #4

Open fizerkhan opened 7 months ago

fizerkhan commented 7 months ago

I am getting this error in Laravel Octane.

2024-03-12 08:36:42 UnexpectedValueException
2024-03-12 08:36:42
2024-03-12 08:36:42 Server middleware has not been detected.
2024-03-12 08:36:42
2024-03-12 08:36:42 at vendor/upscale/swoole-reflection/src/Http/Server.php:36
2024-03-12 08:36:42 32▕ {
2024-03-12 08:36:42 33▕ $middleware = $this->getCallback($this->server, 'request')
2024-03-12 08:36:42 34▕ ?: $this->getCallback($this->getPrimaryPort(), 'request');
2024-03-12 08:36:42 35▕ if (!is_callable($middleware)) {
2024-03-12 08:36:42 ➜ 36▕ throw new \UnexpectedValueException('Server middleware has not been detected.');
2024-03-12 08:36:42 37▕ }
2024-03-12 08:36:42 38▕ return $middleware;
2024-03-12 08:36:42 39▕ }
2024-03-12 08:36:42 40▕

Any idea how to fix it?

sshymko commented 7 months ago

@fizerkhan Could you please clarify how you intend to use this library with Laravel Octane? It would really help if you could provide some code samples of what you're trying to do.

sshymko commented 7 months ago

@fizerkhan The error means that the request handling middleware hasn't been registered. Make sure to use the library after $server->on('Request', ...) call (implemented somewhere in Laravel Octane framework).

fizerkhan commented 7 months ago

@sshymko Thanks for the response.

I am trying to use this Swoole integration - https://github.com/upscalesoftware/swoole-newrelic with my Laravel Octane app (based on Swoole).

I could not find the $server->on('Request', ...) call. I don't know where to include the below steps in the Laravel Octane app

$apm = new Newrelic\Apm(new Newrelic\Apm\TransactionFactory());
$apm->instrument($server);
sshymko commented 7 months ago

@fizerkhan Researched a little bit how Laravel Octane framework is organized. Script bin/swoole-server bootstraps a Swoole server. New Relic instrumentation need to take place after $server->on('request', ...) but before $server->start().

Unfortunately, Laravel Octane doesn't seem to provide a way to customize its Swoole bootstrap before the server starts. You'd have to resort to a more elaborate process involving copying and modifying files.

The workaround:

  1. Copy vendor/laravel/octane/bin/swoole-server to a new file my-swoole-server in the project root
  2. Modify my-swoole-server script to replace __DIR__ with 'vendor/laravel/octane/bin'
  3. Add New Relic instrumentation to my-swoole-server script before $server->start():

    ...
    
    $apm = new Newrelic\Apm(new Newrelic\Apm\TransactionFactory());
    $apm->instrument($server);
    unset($apm);
    
    $server->start();
  4. Start the web-server using the modified script:
    ./my-swoole-server

    instead of:

    ./vendor/bin/swoole-server