1

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?

aref razavi
  • 413
  • 2
  • 12

2 Answers2

1

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');
     }
}
aref razavi
  • 413
  • 2
  • 12
0

For Laravel > 7.x We can dispatch anonymosly

use App\Mail\WelcomeMessage;
use Illuminate\Support\Facades\Mail;
 
dispatch(function () {
    Mail::to('taylor@laravel.com')->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 = App\Podcast::find(1);
 
dispatch(function () use ($podcast) {
    $podcast->publish();
});

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