Search code examples
phplaravelqueuejobs

How to avoid waiting for the Laravel queue and continue with functionality


This is probably a very trivial/novice question. I have started using Laravel Queue in one of my controller functions; however, when I dispatch the job, I have to wait for the queue to finish before it lets me continue with the function. I thought the whole concept behind queues is that it lets you process time-intensive tasks in the background so you can continue with rest of functionality.

This is what my controller looks like:

public function postCreate(Request $request)
{
    // Validate Request
    $this->validate($request, $this->rules());

    // I have to wait for this to finish before it lets me continue
    // I want this to be processed in the background instead
    $this->dispatchFrom('App\Jobs\GenerateReport', $request);

    return json_encode([
        'html' => '<p>some html</p>'
    ]);
}

Solution

  • If indeed the script is processing the job online, instead of offline, my best guess is that you're using the synchronous driver for your queue, which processes jobs immediately and is blocking.

    Check the config/queue.php file to see what the "default" config value is set to, it should read by default: env('QUEUE_DRIVER', 'sync'). If that's the case then that is correct, so then take a look at your .env file and make sure you have an environmental variable set for QUEUE_DRIVER.

    If the QUEUE_DRIVER variable is set to "sync" in your .env file, then you're using the synchronous driver. If it's not set at all, then that line in the queue.php file says it will fall back to the synchronous driver.

    Should all of this be true, just update the environmental variable with the driver you wish to use, and make sure you update the configuration options for that driver in the config/queue.php file.

    Hope that helps!