phplaravellaravel-queuelaravel-jobs

how to run the jobs queue immediately after a job added to queue in laravel?


I currently registered php artisan schedule:run in cronjob in cpanel and the schedule method is:

protected function schedule(Schedule $schedule)
{
    $schedule->command('queue:work --stop-when-empty')
    ->cron('* * * * *')
    ->withoutOverlapping(5);
}

But for my purpose it is necessary to run the jobs immediately,

How can I run php artisan queue:work immediately after a job added to queue(jobs table) and not after one minute?


Solution

  • the solution is to call queue:work on destruct() method of each class that I want to run it's job immediately.

    use Illuminate\Support\Facades\Artisan;
    
    class ProductShopObserver implements ShouldQueue
    {
         public function __destruct()
         {
             Artisan::call('queue:work --stop-when-empty');
         }
    }