Laravel 5.5 Queue Dispatch Not Working
Asked Answered
C

2

5

Maybe I'm not understanding on Laravel queue works, or maybe it itself is not working, my expected behaviour for Laravel Queue/Dispatch is that if a dispatch is initiated from the Controller, the code dispatched to queue should be executed silently and in the background. The end-user browser should not have to wait for the code to execute.

This is however what happens with my code, the dispatched code to queue leaves the browsers "Spinning..." whilst is executes.

Is this expected behavior? The code:

    **Controller:**

    public function make_eps_certs($tbl_eps)
    {
        //dd(Carbon::now()->addMinutes(10))
        Log::info('Dispatching maeEPSCert to Queue');
        $var_result=makeEPSCerts::dispatch($tbl_eps)->onQueue('eventadmin')
            ->delay(10);  
return redirect()->back();
}


    **Job:**

    namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

use App\partSubs;
use Log;

use Image;

class makeEPSCerts implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */

    protected $passdata;
    public $timeout = 120;

    public function __construct($passdata)
    {
        Log::info('Constructing makeEPSCert');
        $this->passdata = $passdata;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {

    try
        {
        Log::info('Beginning makeEPSCert');
        $tbl_eps=$this->passdata;
    .....
Charlottetown answered 9/1, 2018 at 11:45 Comment(0)
E
4

Change your LOG_DRIVERin your .env to database and create the needed migration files with php artisan queue:table, after that do a php artisan migrate.

After that you just need to run php artisan queue:work --queue="eventadmin"

and then you will recognize the expected behavior

A more detailed documentation can be found here: https://laravel.com/docs/5.5/queues

Excursion answered 9/1, 2018 at 12:14 Comment(4)
Thanks, yes that worked perfectly. So my misunderstanding was that you need to run php artisan queue:work --queue="eventadmin" as a listener in order for it to work.Charlottetown
What if you don't want to use the comand line? For instance, process videos in the background as user uploads them ?Cameron
@codiiv If you don't want to use the commandline, it won't work, thats it. The commandline is just used to create an instance of the queue worker, its up to you when you are going to fire a new jobExcursion
@Excursion It looks there is a way. I will update you once I have tested because you can run Artisan commands programmatically actually laravel.com/docs/5.7/…Cameron
B
2

You can try again in the following way (I assume that you did instructions in Laravel docs but someday it's not working):

Step 1: drop table 'jobs' in your database.

Step 2: run command 'php artisan migrate' in console to create table 'jobs' again.

Step 3: run command 'php artisan queue:work' in console

Step 4: retry your app

Note that in .env file, you set up:

QUEUE_CONNECTION=database

QUEUE_DRIVER=database

P/s: It works for me!

Burgess answered 19/5, 2021 at 5:33 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.