skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

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

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

  2. For Laravel > 7.x We can dispatch anonymosly

    use AppMailWelcomeMessage;
    use IlluminateSupportFacadesMail;
     
    dispatch(function () {
        Mail::to('[email protected]')->send(new WelcomeMessage);
    })->afterResponse();
    

    The WelcomeMessage should implement/use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    For Laravel 6.x
    Instead of dispatching a job class to the queue, you may also dispatch a Closure. This is great for quick, simple tasks that need to be executed outside of the current request cycle:

    $podcast = AppPodcast::find(1);
     
    dispatch(function () use ($podcast) {
        $podcast->publish();
    });
    

    for more you can read laravel docs https://laravel.com/docs/7.x/queues

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search