Search code examples
phplaravelqueuejobs

How to force laravel to wait until the end job?


my function in controller Calling parallel and i create a job for use queue in laravel Because parallel call causing the problem

I create queue as follows :

public function serve($id)
{
    $this->dispatch(new OrderServeJob($id));
    return response()->json(true);
}

and i run:

php artisan queue:work

But I have a problem with this method

I want laravel to wait until the queue ends up and then return response()->json(true)

and after redirect user to another address


Solution

  • If you want your job to be handled before the call ends you should use the syncdriver for your Job. You can do this customizing the Job connection to use in your job:

    class OrderServeJob implements ShouldQueue {
    
        public $connection = 'sync'; // <---
    
    
        //
    
    }
    

    You can take a look a this (and others) queue configuration in your config/queue.php file:

    'connections' => [
    
        'sync' => [
            'driver' => 'sync',
        ],
    
        //
    
    ]
    

    Check the Customizing The Queue & Connection section of the documentation.