laravel-actions icon indicating copy to clipboard operation
laravel-actions copied to clipboard

[Octane] Event use handle instead of asListener

Open exodusanto opened this issue 3 years ago • 13 comments

Using Laravel Octane seems that actions are run as object instead of listener.

The problem is similar to #87, the workaround on EventServiceProvider event/listener registration works.

UserUpdated::class => NotifyUserUpdated::class.'@asListener'

exodusanto avatar Oct 05 '21 10:10 exodusanto

Hi there 👋

Thanks for raising this! The issue with #87 was that Laravel Actions was not able to recognise the action as being a listener due to a lack of recognised frames.

Since I've not used Octane yet, would you be able to tell me how Laravel dispatches listeners when used with Octane? It could be using a different method or perhaps even a different class than the ones listed above which would cause Laravel Actions to default to returning an object.

lorisleiva avatar Oct 05 '21 14:10 lorisleiva

This is a trace of the exception.

#0 /.../vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(424): App\\Actions\\User\\SetupCompleted->handle(Object(App\\Events\\UserUpdated))
#1 /.../vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(249): Illuminate\\Events\\Dispatcher->Illuminate\\Events\\{closure}('App\\\\Events\\\\User...', Array)
#2 /.../vendor/nuwave/lighthouse/src/Schema/Directives/EventDirective.php(48): Illuminate\\Events\\Dispatcher->dispatch('App\\\\Events\\\\User...')

Note: I'm using lighthouse graphql plugin, but this problem is only with octane serve

exodusanto avatar Oct 05 '21 21:10 exodusanto

According to your stacktrace, Laravel Actions should recognise it as a Listener (see code below).

https://github.com/lorisleiva/laravel-actions/blob/6e0aa6ed7cf379c3dd0e8d842a1ce754394152f6/src/DesignPatterns/ListenerDesignPattern.php#L20

Is there anything unusual about how you dispatch these events?

lorisleiva avatar Oct 07 '21 15:10 lorisleiva

I made some tests and I think that this part

https://github.com/lorisleiva/laravel-actions/blob/6e0aa6ed7cf379c3dd0e8d842a1ce754394152f6/src/ActionServiceProvider.php#L41-L51

extends only some classes, maybe octane create a new application instance like there

Edit: I added an var_dump before the $manager->extend($abstract); and only Actions registred as command are logged

exodusanto avatar Oct 10 '21 14:10 exodusanto

Hey 👋 Sorry for the late reply. I finally got around to checking it out. Could you try to use dev-main and let me know if that fixes your issue?

lorisleiva avatar Nov 10 '21 17:11 lorisleiva

Hi, thank you for your fix, but it doesn't work.

I have the same exception:

App\Actions\User\SetupCompleted::handle(): Argument #1 ($user) must be of type App\Models\User, App\Events\UserUpdated given, called in /.../vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php on line 424

I also made some tests but I'm worried that beforeResolving is not supported from Octane, or for integrate it this package need to register some logic into octane.php file

 RequestReceived::class => [
            ...Octane::prepareApplicationForNextOperation(),
            ...Octane::prepareApplicationForNextRequest(),
            //
        ],

exodusanto avatar Nov 11 '21 13:11 exodusanto

No worries, thanks for trying!

If anyone using Octane would like to open a PR for this, that would be most appreciated. ❤️

lorisleiva avatar Nov 11 '21 17:11 lorisleiva

Maybe @nunomaduro can give us a hit if nowadays Octane support beforeResolving in some way

exodusanto avatar Nov 16 '21 11:11 exodusanto

Are you able to make a pull request in Octane with a failing test? Or create an issue, with a repository example, so I can reproduce the issue locally?

nunomaduro avatar Jun 14 '22 14:06 nunomaduro

Hi Nuno!

This is the repo.

To setup the reproduction DB

php artisan migrate:fresh
php artisan db:seed # this will create an user

There are two commits:

  1. Base reproduction
  2. Octane installed

Base reproduction

Route / send an event UserUpdated and the listener action UserUpdateTask writes on logs a message.

Octane installed

Starting the server with Octane the same flow throws the error

exodusanto avatar Jun 14 '22 19:06 exodusanto

@exodusanto I think he meant that you should create an issue in the Octane repo itself.

Wulfheart avatar Jun 14 '22 20:06 Wulfheart

Hi all

Has anyone found a solution for this? I’m keen to try a fully actions-based architecture for a new project but we’re using Octane.

Nuno mentioned Octane supports beforeResolving so that doesn’t seem to be the problem: https://github.com/laravel/octane/issues/542

binaryfire avatar Oct 02 '22 17:10 binaryfire

Not yet 😔, I'll find some time next weeks

exodusanto avatar Oct 04 '22 08:10 exodusanto