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
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.
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.
Most helpful comment
I found a similar issue in laravel/tinker#28 with the solution to instead run
Bus::dispatch(new TestJob())
orQueue::push(new TestJob())
. As one of the users said, it would be nice to have a warning in the docs for queues.