Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delay a Laravel Artisan command queued from code

I am running an Artisan command from a controller in my Laravel app. As the docs specify, you can queue like this:

Artisan::queue('email:send', [
    'user' => 1, '--queue' => 'default'
]);

This takes care the queue logic and, in my case, sends the job off to Redis where it's processed almost immediately.

I want to delay the job. You can normally do this when calling a queue command like so:

$job = (new SendReminderEmail($user))->delay(60);

$this->dispatch($job);

Is there a way to join these functions so I can delay my Artisan command for 5 minutes? I assumed there's be a simple option to delay it.

If not, I could create another Job class to stand between my controller and Artisan command, which I could queue in the normal way and delay, then have that Job call my Artisan command. But this seems like a really convoluted way to make it work. Is there a better way to delay a queued Artisan command?

Thank you

like image 452
samiles Avatar asked Nov 09 '22 22:11

samiles


1 Answers

Since the console kernel uses "push" to queue a command, this is not possible for laravel 5.3 and earlier.

However you could make a pull request to the framework to implement the "later" call on the kernel, which could just pass through to the queue´s "later" function.

Or just implement a job class, like you already stated.

But there is a better solution for your use case. Just use the Mail facade:

Mail::later(5, 'emails.welcome', $data, function ($message) {
    //
});

See https://laravel.com/docs/5.2/mail#queueing-mail for documentation.

like image 97
jackomo Avatar answered Nov 14 '22 22:11

jackomo