Laravel Queues 101: Example with Sending Emails

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys in this video you will see the first free lesson from my upcoming new course about cues in laravel i wanted to publish this for free so you would understand just the basic concepts what is a queue what is a job how to send an email in a queue with database driver for example and if you want full course i will announce when it's actually released here on this channel so subscribe to the channel and follow the news or you can go to laraveldaily.teachable.com and subscribe for a yearly membership where you get all current 19 courses and the q course which is coming in probably july realistically or end of june at best case scenario but you can as i say subscribe to yearly membership and get current courses q cores and everything i will release in 2021. now hear me talking about q basics what is q why do you need that and how to use it let's go welcome to the course and we begin talking about laravel cues from classical simple example sending an email why would you need queues because sending the email takes time and if for example after registration you need to send emails to administrators this would fire several email notifications and that would take around five seconds and five seconds is too long to wait for the user so imagine the scenario this is a typical laravel breeze project and in the registration form in the store method after creating the user we have these two lines so we get all the administrator users and then send notifications to them pretty typical scenario registered users notification is just a laravel notification class where we pass the user and inform the administrators about new user that has been registered so if we don't use any queues it will wait for emails to be sent for five seconds or so and only then redirect user to their dashboard let's take a look if we fill that form i use fake filler chrome extension and we register i won't pause this video just calculate the time how much it will take i click one two three four five around five seconds because of what because we need to send three emails i'm using mail trap service to catch those emails so to send these three emails to notify the admins it takes five seconds wouldn't it be cool that email would be sent somewhere separately in the background while the user would be redirected immediately so that's why we need cues q mechanism is the system that takes all the jobs into a queue like in a shop people standing in the queue it would be the same in laravel's system jobs standing in the queue so sending an email with notification could be considered a job where that queue is depends on the driver that you use there are a few drivers that laravel supports and we will start our example with simple database driver so all the jobs will become table rows in our database and then we'll process them one by one in the queue's official documentation there's a section about drivers and database driver requires these two commands so we will launch them queue table it would create the migration for queues table so we'll launch that one migration created successfully and then we'll run php artisan migrate and let's take a look at the database here's our database where we have a table called jobs for now it's empty but now let's fill it in with the job of those email sending so we will change this one from sync from immediate launch to the queue all we need to do here is change the notification class so laravel has a feature to add some classes into the queue really easily like email notifications you can already see that there is a shoot queue added by default when i create a notification class with artisan command php autism make notification and also use queueable here so all we need to do to enable that to be put into queue is implements shoot q that's it so nothing more to change in the class itself now we need to enable the driver of database and for that we go to config queue file and here we have default queue connection and by default it is sync which means no queue and then there are several connections available database being stocked and sqs and redis and we will talk about those later in the course but for now let's enable the database and to enable that we could change that here or in dot env file i just copy here and in dot env i will add a new variable q connection equals database or as i said you could change it here for example and now let's try to register i use fake filler again click register and see it is redirected immediately without any delay and now let's take a look at our database table database table jobs it was empty i refresh and here we have three jobs one for each email sending we will talk about what's inside of the job later in the course for now you can just take a look on the right hand side you can see all the value it's quite a big structure called payload with all the details about that job that it should process registered users notification class and more parameters here also there are parameters for timestamps where the job is available or created or reserved or how many attempts it will be and now how to process that job from the server you need to have so-called workers so there are basically three concepts trinity of concepts that you need to know about queues there's a job that goes into queue which is processed by worker so three things job queue and worker so for now we created a job in the form of laravel notification we have put that into queue when we did shoot q and now we need to start a worker which should process that job or in fact it would process all the jobs that are in the database table at the moment to do that from the terminal we launch php artisan q work we launch that and as you can see processing processed processing processed so start unfinished and now if we refresh our table refresh it's empty again so all the jobs are processed and if we look at our mail we have three new emails that was from another testing in the background so three new emails and this is how we processed the queue and that q work would work in the background for unlimited amount of time until we suspend that from terminal or just restart the server or whatever it would be listening for jobs to appear more and more jobs and it would be processed by q worker and there will be a separate section and a few lessons around configuring the workers because that's also the core concept of queues you need to have enough workers if you have enough jobs especially for bigger systems to process that quicker but in this example you basically saw everything around cues the core concepts again job queue and worker and finally in this lesson i will mention two more benefits of using queues one benefit is that you can scale it on a separate server so you can launch the jobs on totally separate queue server or even a few servers so scale it totally separately without loading your main server main application server and also queue mechanism is really good for retrying the jobs so what if that email notification sending is failing for external service failure or network connection or some parameter missing or just 500 error in your application whatever it is really easy to configure the amount of retries or retries until some certain period or process the failures and i will show all of that later in the course but basically queue mechanism the whole system is really convenient to process separate background jobs while your main application takes care of the web interface and the main functionality in the next lessons we will take a look at a few more examples
Info
Channel: Laravel Daily
Views: 59,983
Rating: undefined out of 5
Keywords:
Id: rVx8xKisbr8
Channel Id: undefined
Length: 8min 43sec (523 seconds)
Published: Sun Jun 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.