3

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?

4 Answers 4

6

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

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, but what if I don't want to use supervisors? Can i put them into a cron?
I decided to work with supervisors, and it works! Many thanks!
Well, you do not need to use supervisord, you could use daemon option, but it has some code implications. Honestly, using supervisord is a good call, and it'll save you some headaches. More info available on link I've provided. No problem, it was pleasure.
5

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.

Comments

0

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.

1 Comment

You can mark code/script with ctrl-k (for full lines) or with the backtick (```). Hopefully someone can follow up with me by telling me how to escape a backtick on this site :)
0

Please remove Artisan::call on 'queue' commands within your dispatcher. and use this method for dispatching - dispatchAfterResponse

for eg : $this->dispatchAfterResponse(new WelcomeEmail($user));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.