Ad

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

Ad

Answer

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.

Ad
source: stackoverflow.com
Ad