artisan tinker artifact - Laravel queue processes a job only after the next is queued
mkarnicki opened this issue ยท 3 comments
- Laravel Version: 5.7.12
- PHP Version: 7.2.10
- Database Driver & Version: confirmed with redis, database, sync
Please advise if this is a PsyShell bug and not laravel/framework bug, in which case I can file this issue in a more appropriate place.
Description:
Dispatching jobs from artisan tinker session doesn't work correctly with the queue. Any queued job PREV waits for another job NEXT, at which point PREV is handled (but not NEXT, which waits for yet another job). I fully described the issue on SO and got someone to confirm it.
Steps To Reproduce:
$ laravel new test
$ cd test
$ php artisan make:job TestQueue
Paste the following into the TestQueue class. Nothing fancy, really:
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Support\Facades\Log;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class TestQueue implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $id;
public function __construct($id)
{
Log::info('Creating ' . $id);
$this->id = $id;
}
public function handle()
{
Log::info('Running ' . $this->id);
}
}
Now, regardless of the QUEUE_CONNECTION env var (redis
, beanstalkd
, even sync
), I get the following behavior. Please note I have php artisan queue:work
running in a separate terminal, restarted after any .env changes and config is not cached.
$ php artisan tinker
>>> App\Jobs\TestQueue::dispatch(1)
logs:
[2018-10-30 22:38:01] local.INFO: Creating 1
>>> App\Jobs\TestQueue::dispatch(2)
logs:
[2018-10-30 22:38:04] local.INFO: Creating 2
[2018-10-30 22:38:06] local.INFO: Running 1
>>> App\Jobs\TestQueue::dispatch(3)
logs:
[2018-10-30 22:38:22] local.INFO: Creating 3
[2018-10-30 22:38:24] local.INFO: Running 2
If you're testing with sync queue, ensure that sync driver is set to sync
and not null
in config/queue.php
EDIT: Note this only happens from artisan tinker session. The jobs are handled correctly via web routes.
facepalm
Thank you for the links Dries. I see everyone's complaining and nobody's trying to document it, so I might as well do it myself. Cheers.
For people coming here you can just destruct the object you get.
App\Jobs\SendTasksDigest::dispatch(App\User::find(5))->__destruct();