Framework: Jobs not processed for first time in artisan tinker

Created on 15 Feb 2018  路  3Comments  路  Source: laravel/framework

  • Laravel Version: 5.5.34
  • PHP Version: 7.1.7

Description:

When using artisan, the job doesn't dispatch the first time I call TestJob::dispatch() but does so in subsequent calls. This is the case for both the queue worker php artisan queue:worker and horizon php artisan horizon

Steps To Reproduce:

Run artisan tinker and dispatch a job. The queue worker doesn't process it. If you try to dispatch the job again, it will be processed.

Most helpful comment

I found a similar issue in laravel/tinker#28 with the solution to instead run Bus::dispatch(new TestJob()) or Queue::push(new TestJob()). As one of the users said, it would be nice to have a warning in the docs for queues.

All 3 comments

I found a similar issue in laravel/tinker#28 with the solution to instead run Bus::dispatch(new TestJob()) or Queue::push(new TestJob()). As one of the users said, it would be nice to have a warning in the docs for queues.

The issue is that tinker will keep the generated PendingDispatch object in memory, to supposedly allow the user to use it in subsequent commands. The Job will be truly dispatched when that object is destroyed. https://github.com/laravel/tinker/issues/28#issuecomment-530473307 suggests to use ->__destruct() on the returned object to force destroying the object and thus dispatch the job, however I noticed that would make the job dispatch twice for some reason.

My simple "solution" is to simply add another statement after the job dispatch, to force tinker destroy the PendingDispatch object:
App\Jobs\MyJob::dispatch()->onConnection('redis');1; , notice the ;1; at the end

Oh my goodness... I lost a few hours because of this issue. I finally realized it was a Tinker issue. @DragosMocrii 's solution didn't seem to work. I finally solved it by putting the ::dispatch in a Laravel command and just ran the command, ie not using Tinker.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

klimentLambevski picture klimentLambevski  路  3Comments

Anahkiasen picture Anahkiasen  路  3Comments

progmars picture progmars  路  3Comments

PhiloNL picture PhiloNL  路  3Comments

gabriellimo picture gabriellimo  路  3Comments