This episode is for members only

Sign up to access "Job Batching Progress with Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
10. Updating the progress of the batch

Transcript

00:00
We saw from earlier that the progress callback is going to be called for every single job that's
00:04
run within the batch. So remember when we log this to our Laravel log file with a percentage, we know that this is run three times if we have three jobs and obviously more if this changes. Let's just take a look at the steps we need to take within this progress callback and then we'll implement the code to get each of the jobs that we've created transitioned through the states
00:22
that we've defined. So the first thing that we need to do is get the current task in progress, which we know is always going to be the first one because we defined that within the before callback here. Then we need to transition that task, so the one that's in progress now means it's finished because progress will only happen when a job finishes, transition that task to
00:45
complete. Then we need to get the next task in the batch or stack, whatever we want to call it, and transition that to in progress. So we basically need to move both of these jobs from one state to another and that will nicely move along the progress that we have in the database. Okay, so sounds pretty straightforward. Let's see how we can implement this. Now I want
01:10
a really easy way to get the current task that's in progress. Let's just think about how we might want to do that. We might want to grab the server, which of course we're going to need to bring into scope just up here, and we might want to say task currently in progress. So let's just scuffle this out just so we don't end up with too many queries and too much code hanging around in this callback.
01:33
Okay, let's implement this method and see what this might look like. So let's go ahead and create out a method in here called task currently in progress and see what we need to do. If we think about it, the task that is currently in progress is just the task where the state is in progress. Now luckily for us, the package that we've pulled in has some scopes that we can take advantage of.
01:54
So let's go ahead and return this task. So this will be all of the tasks specifically for that server and we can use the where state scope and this is part of that package. So we can say state and in progress and that will return to us the current task that is in progress. Really simple. We have that scope available to us and now we know that inside of here we've got the current
02:22
task that's in progress. Now that we've got the task that's currently in progress, the one that has just been completed, because remember progress will only be run when this actually finishes, we can transition this to complete. So let's go ahead and take that task, take the state, and then let's transition the one that's just finished to a status of complete. Okay, so we've
02:46
got the current task in progress. We've transitioned that task to complete. We'll figure these out in a minute but let's just run our queue and see what happens. Okay, so let's again go ahead and clear out the database of any server tasks just so we can see what we're doing and let's head over and run our queue. So php artisan queue work and let's go ahead and create this out. Okay, so remember
03:09
we've got a few seconds for each of our jobs so we'll need to head straight over to the database to monitor this. Let's go ahead and click create server and head over and you can see that this is in progress. I'm just going to keep refreshing and you can see now that after a few seconds when that first job completed this is now being transitioned to complete. Great, now we need to
03:29
figure out how to when this is complete transition the next job into an in progress status. All of these are running in the background so your jobs are still being run and as part of that batch they're still all going through but we're just not reflecting this status at the moment. Okay, so how do we do this? Well we need to figure out the next job in the batch. Now ideally we'd do something
03:53
like task and next but Laravel doesn't have this functionality. Let's just build this out first of all and then we'll see how we do this. So let's say task next state and then let's go ahead and transition that to in progress so basically the next job in the stack. Now we know that we're storing the order of the jobs so we can just implement a really simple method here to grab
04:17
out the next one based on the order. Let's go over to our server task model and create this out now. So let's do this just down here and let's create next. Now this does introduce an additional query but it's not too much trouble just to do this for every single job that we have. So we're going to go ahead and return here this and really importantly where belongs to because we are working with the
04:41
model itself. If we don't do this it's going to fetch the next task for any of the servers that we've created so let's go ahead and say that where it belongs to the server and what we're going to do is say where order is greater than the order of the job that we're currently working in. So if for example we're working with job with an order of one this will fetch out any of these tasks that
05:06
has an order greater than one which we know is going to be two, three, four, however many we have. Then what we can do to this is grab the first one which is effectively the next one. So we're just going to say first and that is how we fetch the next record when we have an orderable thing. So if we head back over here this is now going to grab the next one and transition that to the next state.
05:29
Now because we've done this and every single job is going to have progress called this callback is going to call every time a single job in that batch works it's just going to go through the chain and update each one. Just while we're here I'm going to go ahead and increase the time out here a little bit so let's set these each to five just so we have a little bit of time
05:51
to react and test this out. Okay so we've made some changes to our job so let's cancel off the queue rerun this and let's go over and just get rid of any of the server tasks we have in here. Okay let's go over and create this let's go back over to the database and keep an eye. So this one is in progress I'm just going to keep refreshing that should transition to complete and
06:14
the next one should transition to in progress. Now it doesn't look like that's happened so let's take a look at what has gone wrong here. Okay so if we open up our Laravel log file we've got an attempt to read property state on null. Okay so if we head back over to our server observer here let's just have a look and this looks good to me so let's open up our server task and yeah where belongs
06:34
to? I just spelled the method wrong of course that's going to return null. Okay let's go ahead and close off our queue and rerun it and let's try this again. So again we're just going to clear out all of our server tasks head over and click create server back over and let's have a look. So now after five seconds that in progress should go to complete the next one now goes to in progress
06:55
and finally we end up with the last one in progress and then of course that is going to transition after about five seconds to complete. So now we're going through the entire stack of all of the tasks that we have transitioning the state over but of course at some point we're going to end up with a failure in one of these jobs. Let's take a look at how we handle that next.
18 episodes1 hr 37 mins

Overview

Let’s tackle how to batch tasks in Laravel, change their state as they complete, and display step-by-step progress in the UI.

Using job batching we’ll create, dispatch and monitor a list of sequential jobs — changing their state using model states. We’ll also take this a step further to allow batch variations based on different tasks, and the ability to easy swap around the order of tasks.

At the end of the course, we’ll set up a UI with Livewire to poll the state of our batch, displaying progress to the user (and if anything goes wrong).

Simply put, if you’re building something that requires step-by-step jobs to be run and you need to display progress to the user — this course covers absolutely everything you’ll need to know.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!