zerodahero / laravel-workflow

Use the Symfony Workflow component in Laravel
MIT License
197 stars 37 forks source link

Event listeners defined in EventServiceProvider failing #20

Closed klimenttoshkov closed 4 years ago

klimenttoshkov commented 4 years ago

I have the following in EventServiceProvider:

class EventServiceProvider extends ServiceProvider { protected $listen = [ 'workflow.smsNotificationIncoming.transition.toAccepted' => [IncomingListener::class], ];

When transition is applied, fails with:

{ "message": "Serialization of 'Closure' is not allowed", "exception": "Exception",

klimenttoshkov commented 4 years ago

The main idea of defining separate listener is that I want to process it in Queue and I want to avoid making Job and dispatching in from function that is defined in subscriber.

zerodahero commented 4 years ago

Can you send over the stack trace? I believe that's coming from the event dispatcher, but it's possible there may be some non-serializeable things (i.e. a closure) being appended to your model as well.

klimenttoshkov commented 4 years ago

Please review attached file with the model, controller, listener and service provider. And the trace. all debug info.txt

klimenttoshkov commented 4 years ago

I did some testing and isolated the problem to interface ShouldQueue. If it is used on a listener then those errors appear. It doesn't matter where the listener is defined and etc. Removing this interface fixes the error but what is important to me is to process using Queue.

zerodahero commented 4 years ago

It looks like the issue is in the serialization, so that would make sense if you remove the listener from queuing. Out of curiosity, in tinker or a test, try serializing a similar model ($model->jsonSerialize()) and see if that throws the same error.

klimenttoshkov commented 4 years ago

Seems fine to me:

$model=Modules\SmsNotification\Entities\Queue\Incoming::find(100); => Modules\SmsNotification\Entities\Queue\Incoming {#3354 id: 100, uuid: null, service_id: null, marking: null, created_at: null, updated_at: null, } $model->jsonSerialize(); => [ "id" => 100, "uuid" => null, "service_id" => null, "marking" => null, "created_at" => null, "updated_at" => null, ]

klimenttoshkov commented 4 years ago

and then going further:

$model->workflow_apply('toAccepted'); Exception with message 'Serialization of 'Closure' is not allowed'

klimenttoshkov commented 4 years ago

So basically it seems to me that if you try to apply workflow transition from Tinker the output would require serialisation and it will fail. The same applies for queued execution which leads to throwing the exception when trying to queue.

zerodahero commented 4 years ago

Ok, I found the issue. Deep in symfony, it looks like it's possible to have a guard that exposes an expression/closure. I'll work on a fix, and keep you posted.

zerodahero commented 4 years ago

Ok, just released 3.1.0 which should address that! Let me know if you run into anything else!

klimenttoshkov commented 4 years ago

Thank you for quick and kind response

klimenttoshkov commented 4 years ago

Still not able to queue event listeners:

`[2020-05-18 19:32:19] local.ERROR: Serialization of 'Closure' is not allowed {"exception":"[object] (Exception(code: 0): Serialization of 'Closure' is not allowed at /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Queue/Queue.php:147) [stacktrace]

0 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Queue/Queue.php(147): serialize(Object(Illuminate\Events\CallQueuedListener))

1 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Queue/Queue.php(116): Illuminate\Queue\Queue->createObjectPayload(Object(Illuminate\Events\CallQueuedListener), 'queues:default')

2 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Queue/RedisQueue.php(153): Illuminate\Queue\Queue->createPayloadArray(Object(Illuminate\Events\CallQueuedListener), 'queues:default', '')

3 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Queue/Queue.php(94): Illuminate\Queue\RedisQueue->createPayloadArray(Object(Illuminate\Events\CallQueuedListener), 'queues:default', '')

4 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Queue/RedisQueue.php(91): Illuminate\Queue\Queue->createPayload(Object(Illuminate\Events\CallQueuedListener), 'queues:default', '')

5 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Queue/Queue.php(46): Illuminate\Queue\RedisQueue->push(Object(Illuminate\Events\CallQueuedListener), '', NULL)

6 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(496): Illuminate\Queue\Queue->pushOn(NULL, Object(Illuminate\Events\CallQueuedListener))

7 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(453): Illuminate\Events\Dispatcher->queueHandler('Modules\\SmsNoti...', 'transitionToRou...', Array)

8 [internal function]: Illuminate\Events\Dispatcher->Illuminate\Events\{closure}(Object(Symfony\Component\Workflow\Event\TransitionEvent))

9 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(388): call_user_func_array(Object(Closure), Array)

10 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(218): Illuminate\Events\Dispatcher->Illuminate\Events\{closure}('workflow.smsNot...', Array)

11 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(476): Illuminate\Events\Dispatcher->dispatch('workflow.smsNot...', Array)

12 /Users/sag/Documents/api/vendor/zerodahero/laravel-workflow/src/Events/WorkflowSubscriber.php(47): event('workflow.smsNot...', Object(Symfony\Component\Workflow\Event\TransitionEvent))

13 /Users/sag/Documents/api/vendor/symfony/event-dispatcher/EventDispatcher.php(230): ZeroDaHero\LaravelWorkflow\Events\WorkflowSubscriber->transitionEvent(Object(Symfony\Component\Workflow\Event\TransitionEvent), 'workflow.transi...', Object(Symfony\Component\EventDispatcher\EventDispatcher))

14 /Users/sag/Documents/api/vendor/symfony/event-dispatcher/EventDispatcher.php(59): Symfony\Component\EventDispatcher\EventDispatcher->callListeners(Array, 'workflow.transi...', Object(Symfony\Component\Workflow\Event\TransitionEvent))

15 /Users/sag/Documents/api/vendor/symfony/workflow/Workflow.php(333): Symfony\Component\EventDispatcher\EventDispatcher->dispatch(Object(Symfony\Component\Workflow\Event\TransitionEvent), 'workflow.transi...')

16 /Users/sag/Documents/api/vendor/symfony/workflow/Workflow.php(200): Symfony\Component\Workflow\Workflow->transition(Object(Modules\SmsNotification\Entities\Cdr\In), Object(Symfony\Component\Workflow\Transition), Object(Symfony\Component\Workflow\Marking), Array)

17 /Users/sag/Documents/api/vendor/zerodahero/laravel-workflow/src/Traits/WorkflowTrait.php(14): Symfony\Component\Workflow\Workflow->apply(Object(Modules\SmsNotification\Entities\Cdr\In), 'toRoute')

18 /Users/sag/Documents/api/Modules/SmsNotification/Listeners/IncomingSubscriber.php(150): Modules\SmsNotification\Entities\Cdr\In->workflow_apply('toRoute')

19 [internal function]: Modules\SmsNotification\Listeners\IncomingSubscriber->transitionToQueued202(Object(Symfony\Component\Workflow\Event\TransitionEvent))

20 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(388): call_user_func_array(Array, Array)

21 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(218): Illuminate\Events\Dispatcher->Illuminate\Events\{closure}('workflow.smsNot...', Array)

22 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(476): Illuminate\Events\Dispatcher->dispatch('workflow.smsNot...', Array)

23 /Users/sag/Documents/api/vendor/zerodahero/laravel-workflow/src/Events/WorkflowSubscriber.php(47): event('workflow.smsNot...', Object(Symfony\Component\Workflow\Event\TransitionEvent))

24 /Users/sag/Documents/api/vendor/symfony/event-dispatcher/EventDispatcher.php(230): ZeroDaHero\LaravelWorkflow\Events\WorkflowSubscriber->transitionEvent(Object(Symfony\Component\Workflow\Event\TransitionEvent), 'workflow.transi...', Object(Symfony\Component\EventDispatcher\EventDispatcher))

25 /Users/sag/Documents/api/vendor/symfony/event-dispatcher/EventDispatcher.php(59): Symfony\Component\EventDispatcher\EventDispatcher->callListeners(Array, 'workflow.transi...', Object(Symfony\Component\Workflow\Event\TransitionEvent))

26 /Users/sag/Documents/api/vendor/symfony/workflow/Workflow.php(333): Symfony\Component\EventDispatcher\EventDispatcher->dispatch(Object(Symfony\Component\Workflow\Event\TransitionEvent), 'workflow.transi...')

27 /Users/sag/Documents/api/vendor/symfony/workflow/Workflow.php(200): Symfony\Component\Workflow\Workflow->transition(Object(Modules\SmsNotification\Entities\Queue\Incoming), Object(Symfony\Component\Workflow\Transition), Object(Symfony\Component\Workflow\Marking), Array)

28 /Users/sag/Documents/api/vendor/zerodahero/laravel-workflow/src/Traits/WorkflowTrait.php(14): Symfony\Component\Workflow\Workflow->apply(Object(Modules\SmsNotification\Entities\Queue\Incoming), 'toQueued202')

29 /Users/sag/Documents/api/Modules/SmsNotification/Jobs/Queue/processSupply.php(50): Modules\SmsNotification\Entities\Queue\Incoming->workflow_apply('toQueued202')

30 [internal function]: Modules\SmsNotification\Jobs\Queue\processSupply->handle()

31 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(33): call_user_func_array(Array, Array)

32 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Container/Util.php(36): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()

33 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(91): Illuminate\Container\Util::unwrapIfClosure(Object(Closure))

34 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))

35 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Container/Container.php(592): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)

36 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(94): Illuminate\Container\Container->call(Array)

37 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\Bus\Dispatcher->Illuminate\Bus\{closure}(Object(Modules\SmsNotification\Jobs\Queue\processSupply))

38 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Modules\SmsNotification\Jobs\Queue\processSupply))

39 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(98): Illuminate\Pipeline\Pipeline->then(Object(Closure))

40 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(83): Illuminate\Bus\Dispatcher->dispatchNow(Object(Modules\SmsNotification\Jobs\Queue\processSupply), false)

41 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\Queue\CallQueuedHandler->Illuminate\Queue\{closure}(Object(Modules\SmsNotification\Jobs\Queue\processSupply))

42 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Modules\SmsNotification\Jobs\Queue\processSupply))

43 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(85): Illuminate\Pipeline\Pipeline->then(Object(Closure))

44 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(59): Illuminate\Queue\CallQueuedHandler->dispatchThroughMiddleware(Object(Illuminate\Queue\Jobs\RedisJob), Object(Modules\SmsNotification\Jobs\Queue\processSupply))

45 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(98): Illuminate\Queue\CallQueuedHandler->call(Object(Illuminate\Queue\Jobs\RedisJob), Array)

46 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(356): Illuminate\Queue\Jobs\Job->fire()

47 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(306): Illuminate\Queue\Worker->process('redis', Object(Illuminate\Queue\Jobs\RedisJob), Object(Illuminate\Queue\WorkerOptions))

48 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(132): Illuminate\Queue\Worker->runJob(Object(Illuminate\Queue\Jobs\RedisJob), 'redis', Object(Illuminate\Queue\WorkerOptions))

49 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(112): Illuminate\Queue\Worker->daemon('redis', 'SmsNotification', Object(Illuminate\Queue\WorkerOptions))

50 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(96): Illuminate\Queue\Console\WorkCommand->runWorker('redis', 'SmsNotification')

51 [internal function]: Illuminate\Queue\Console\WorkCommand->handle()

52 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(33): call_user_func_array(Array, Array)

53 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Container/Util.php(36): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()

54 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(91): Illuminate\Container\Util::unwrapIfClosure(Object(Closure))

55 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))

56 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Container/Container.php(592): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)

57 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Console/Command.php(134): Illuminate\Container\Container->call(Array)

58 /Users/sag/Documents/api/vendor/symfony/console/Command/Command.php(255): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))

59 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Console/Command.php(121): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))

60 /Users/sag/Documents/api/vendor/symfony/console/Application.php(912): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

61 /Users/sag/Documents/api/vendor/symfony/console/Application.php(264): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Queue\Console\WorkCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

62 /Users/sag/Documents/api/vendor/symfony/console/Application.php(140): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

63 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Console/Application.php(93): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

64 /Users/sag/Documents/api/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(129): Illuminate\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

65 /Users/sag/Documents/api/artisan(37): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

66 {main}

"} `

zerodahero commented 4 years ago

I'll try and take another look at that soon. In the meantime, can you confirm that you're using v3.1.0 of this package? composer info | grep laravel-workflow

klimenttoshkov commented 4 years ago

sag@Sags-MacBook-Pro-A1398 api % composer info | grep laravel-workflow zerodahero/laravel-workflow v3.1.0 Integerate Symfony Workflow component into Laravel.

— Kliment Toshkov mail@klimenttoshkov.com

On 19 May 2020, at 22:04, zerodahero notifications@github.com wrote:

I'll try and take another look at that soon. In the meantime, can you confirm that you're using v3.1.0 of this package? composer info | grep laravel-workflow

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/zerodahero/laravel-workflow/issues/20#issuecomment-631021167, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOIQGVPGCRVG2BEPWQDLE73RSLJ2FANCNFSM4M2UWJPQ.

zerodahero commented 4 years ago

It's been a busy week, but this is still on my radar. I should have time this weekend to take a closer look at it. Thanks for your patience.

On Wed, May 20, 2020, 12:09 klimenttoshkov notifications@github.com wrote:

sag@Sags-MacBook-Pro-A1398 api % composer info | grep laravel-workflow zerodahero/laravel-workflow v3.1.0 Integerate Symfony Workflow component into Laravel.

— Kliment Toshkov mail@klimenttoshkov.com

On 19 May 2020, at 22:04, zerodahero notifications@github.com wrote:

I'll try and take another look at that soon. In the meantime, can you confirm that you're using v3.1.0 of this package? composer info | grep laravel-workflow

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub < https://github.com/zerodahero/laravel-workflow/issues/20#issuecomment-631021167>, or unsubscribe < https://github.com/notifications/unsubscribe-auth/AOIQGVPGCRVG2BEPWQDLE73RSLJ2FANCNFSM4M2UWJPQ .

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/zerodahero/laravel-workflow/issues/20#issuecomment-631606615, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABRAFADIQ2CRWGFIY36CQTLRSQFFJANCNFSM4M2UWJPQ .

zerodahero commented 4 years ago

Ok, looks like not all the events dispatched were the ones in this package, so I'll update those and push a new patch for it, soon.

zerodahero commented 4 years ago

Here's the patch! I was able to recreate this locally with a similar setup that you posted, and this patch fixes that. https://github.com/zerodahero/laravel-workflow/releases/tag/v3.1.1

Let me know how it works for you!

klimenttoshkov commented 4 years ago

Thank you, I need some time to think how to adjust the setup and test it. 3.1.1? :-)

zerodahero commented 4 years ago

Yep! v3.1.1

klimenttoshkov commented 4 years ago

Previous is 1.3.2, are you sure not a mistake? — Kliment Toshkov mail@klimenttoshkov.com

On 03 Jun 2020, at 17:04, zerodahero notifications@github.com wrote:

Yep! v3.1.1

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/zerodahero/laravel-workflow/issues/20#issuecomment-638219825, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOIQGVIDFILEF67JXJ3VIATRUZJ75ANCNFSM4M2UWJPQ.

zerodahero commented 4 years ago

Previous version was 3.1.0

klimenttoshkov commented 3 years ago

Hi, I'm happy to report that serializing is fine if event listener implements ShouldQueue interface then execution is queued properly.