Reputation: 229
Hy! I have an application where I have to send some emails at certain actions (such as user creation, etc.). Problem is they are not running in the background, instead I have to wait until the process is done, and then it redirects me to another page.
I use database
driver with queues
, Laravel 5.2
.
My code for email, for exp, after user creation:
$this->dispatch(new WelcomeEmail($user));
Artisan::call('queue:work');
where WelcomeEmail
is the job that is pushed on queue. This type of code is placed in all the places where I want an email to be send. What is wrong?
Upvotes: 3
Views: 12921
Reputation: 11
Please remove Artisan::call on 'queue' commands within your dispatcher. and use this method for dispatching - dispatchAfterResponse
for eg : $this->dispatchAfterResponse(new WelcomeEmail($user));
Upvotes: 0
Reputation: 3755
I don't know why changing .env
is not enough to fix the issue but after changing this line from
'default' => env('QUEUE_CONNECTION', 'sync'),
to
'default' => env('QUEUE_CONNECTION', 'database'),
in config/queue.php file
Everything works fine.
Upvotes: 5
Reputation: 1
I had a similar problem, but because was a single job I didn't want a daemon to always run, also there is the update code problem,.... So I solved running the command directly from PHP, like:
exec('nohup php /my_folder/artisan queue:work --once > /dev/null 2>&1 &');
This will launch one job and then turn off, without waiting the result. But be careful on the Laravel log file permissions, the o.s. user can change if you are running under Linux depending on context and configuration.
Hope that can help someone.
Upvotes: 0
Reputation: 433
First, you do not want to use Artisan::call
on 'queue' commands within your dispatcher.
You should open your terminal and execute: php artisan queue:listen --timeout=0 --tries=1
and you should let it be.
Then you can visit your page where $this->dispatch or even better dispatch method will be called. Code on that page should be:
dispatch(new WelcomeEmail($user));
On your production server, you should use supervisord to monitor your php artisan queue:listen
command, to make sure that it's up and running all the time.
For further reading please visit: https://laravel.com/docs/5.2/queues
Upvotes: 6