Laravel Job Batch: Show Queue Progress

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys now what you're about to see is another free lesson from my new course cues in larval it will be probably the last free lesson that i will release on youtube and the topic is progress of batch of jobs so imagine you have a set of jobs like 500 messages to send to someone and you need to show the progress like here on the screen in this short video i will touch only the back end part like how to get the progress off the badge the implementation on the front end could be really really different from vue.js live wire sockets and whatever so that is outside of this video but in this video you will understand at least how to get the data about the badge and how to show it on the screen if you want full course you go to laravel daily.teachable.com here it is it costs 29 plus vat if you are from european union or you can opt in for yearly membership and get all the 19 cores for 999 per year plus everything upcoming in a year ahead i have a lot of plans for new courses like graphql like react native or flutter not decided yet like api stuff so if you want to get it all go to yearly membership now let's get to the video about progress of the jobs in this lesson let's try to display progress of a lot of jobs so for example we have a batch of jobs which are like 500 jobs doing something and you need to display the progress refreshing every second or every whatever time period you want and there are multiple ways to do that i will show you the most straightforward one with batches because batch object contains all that information you just need to display that and then on top of that you can come up with your own variant of doing that you can use vue.js live wire or sockets or whatever you want i will just show you the back end part and the front end visual part it's up to your imagination in this case i've made an array of jobs of sending welcome message to everyone in the system except for the user themselves so send welcome message is a job which just creates a message record and intentionally i added a micro sleep here for 100 000 microseconds which is 0.1 of a second actually let's make it a lit a little bit smaller so 0.05 of a second for each job so it's kind of like fake delay so in total we have around 500 users in the database in my testing project and we need to process 500 jobs which is one batch let's try and see what happens and how to view the progress so when i register with a new user with fake file chrome extension we register and i redirect back to the dashboard with batch id and where do i get that batch id from when i launch the batch dispatch that batch it's not started yet because the queue isn't started yet i get the badge object in return and then i redirect to dashboard with batch id and what is batch id in the database if we refresh the job batches table we have one record with 547 jobs pending for now nothing failed and all of those are inside of the jobs table so 547 rows as it should be now if we start the queue remember one job is delayed for 0.5 or 0.05 of a second we do php artisan q work and we have processing processed and in the job batches table that number is being refreshed so refresh refresh and on the page i did the refresh as well so every two second the refresh is happening with all the page with the same batch id and i get the batch id progress here and show the percentage again it's a very simple implementation with just refreshing full page with plain javascript you can do that with ujs livewire sockets or whatever but on the back end you have access to all the batch object and let's take a look at the code so as you can see hundred percent and if i reach 100 then the refresh doesn't happen anymore in the code dashboard is referring to dashboard controller with index method and here it is if we have batch id as a request parameter in the url we try to find that batch and bus facade helps us with that so facades bus here and find badge and here we have full object with a lot of things inside of it and in dashboard blade i have if statements so if the batch is not null then we show those properties and here are many properties so process jobs this is a method total jobs is a property progress is a method all of that is listed in the laravel documentation of cues here inspecting batches and there are a lot of things that you can use like pending jobs failed jobs finished cancel or cancelled so use your imagination at the bottom i've added a really really simple javascript again no vue.js or livewire or anything like that just simple plain javascript i'm using laravel breeze as a starter kit that's why i had to add a slot of scripts in the app blade in the main app blade and this is resolved by x slot and here i check if there is a batch and if it's not finished then i just refresh the full page every two seconds the parameter is the same the id parameter of batch so it's doing the same thing again getting all the batch passing into the dashboard and showing the progress so this is probably the most simple way how you can do that again you can do that with no refresh off the page depending on your situation is there a button is there a progress bar or something so use your imagination but on the back end you can have access to all the batch as object and use these methods and properties
Info
Channel: Laravel Daily
Views: 30,378
Rating: undefined out of 5
Keywords:
Id: HErI5i-a0NI
Channel Id: undefined
Length: 5min 50sec (350 seconds)
Published: Fri Jun 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.