Queue in Laravel and use supervisor to monitor queue processing on ubuntu

Tram Ho

What is Laravel Queue?

Queue is a list of queue jobs that need to be processed in order. For example, when 1000 people click on their mail button, all will be in order and the mail will be processed in turn. And the jobs will be saved in different places (database, redis), depending on what you set up in env . For example, if you want to save a job to the database:

And remember not to run

Create a job

First you need to create yourself a job to handle your work. For example, here I need to send a password reset mail

And in app/Jobs his will file SendEmailResetPassword.php as follows:

Here I have transferred a little data to handle, but by default there is no such thing ?

Create email template

Please run the following command

And now go to app/Mail and you should see the newly created file. Basically, this will be a file that helps you bind data into mail and send it to users

Next, create a blade file. As above, I will create the file reset-password.blade.php in views/mail

Dispatch Job

And now, in the Controller, please disptach your job

Run jobs in the queue

To run jobs, use the command:

--tries=3 will be the number of times you --tries=3 the job if it fails. There are a few other parameters about waiting time, rest time, .. then you should take the time to read the docs of Laravel and read it, here I will introduce it just like that. ?

How to run the queue on the server

Oh, but maybe we always have to run queue: work on the server huh. Of course not, you can use the supervisor to monitor the queue handling on the server. To install the table, use the command

The configuration files will be located in the etc/supervisor/conf.d . Here, create the file reset-password.conf and configure the following

Then save and run the following commands:

My article is only here, hope to be of some help to everyone ? . If anything is wrong, everyone should let me know

Share the news now

Source : Viblo