samiles
samiles

Reputation: 3900

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

Upvotes: 6

Views: 2747

Answers (2)

you can solve the problem, using a scheduled task that is going to be watching for emails to send in the desired time. you can also use a table to set the email variables like the subject, template, template vars, time to send the email, etc.

Upvotes: 0

jackomo
jackomo

Reputation: 375

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.

Upvotes: 1

Related Questions