laravel / tinker

Powerful REPL for the Laravel framework.
https://laravel.com/docs/artisan#tinker
MIT License
7.34k stars 130 forks source link

artisan tinker artifact - Laravel queue processes a job only after the next is queued #59

Closed mkarnicki closed 6 years ago

mkarnicki commented 6 years ago

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.

driesvints commented 6 years ago

Please see https://github.com/laravel/tinker/issues/28 and https://github.com/laravel/tinker/issues/30

mkarnicki commented 6 years ago

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.

robclancy commented 5 years ago

For people coming here you can just destruct the object you get.

App\Jobs\SendTasksDigest::dispatch(App\User::find(5))->__destruct();