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
06. Inspecting the batch status

Transcript

00:00
There are two main ways that we can inspect the status of our batch.
00:04
The first way is to chain on any methods from bus batch, like the progress, when something failed, and there's a few others that we'll cover as well. But what we can also do is we can take the batch ID, go ahead and fetch it out, and then query it. So let's look at, first of all, implementing the progress callback from our batch.
00:27
So when we go ahead and do this, what we'll get is the batch model itself, or the batch instance itself, directly into here. What we can do then is anything in here from that batch class that we looked at earlier, remember with that list of methods that give us the percentage,
00:44
if it has failures, we can just do anything with that. So in here, let's just go ahead and log out something from here, just so we can see this working behind the scenes. So let's go ahead and use log info, and let's go back over to the batch,
00:57
and look at this progress method. So this is just going to return the percentage progress of all of the jobs that we have. So let's log that out. So batch and progress, and we'll see that in our log file.
01:08
Let's go ahead and open up our log file, and let's get rid of this, and we're going to go over to the database and just clear everything out, so we can start fresh, and we'll get rid of this server as well. Okay, so it's at this point, we want to go ahead and start our queue.
01:21
So let's go php artisan queue work, and let's run that to start our queue. Okay, great. So we're going to go back over and hit create server. We're going to come over to our Laravel log file,
01:31
and let's just wait for the percentage progress for this to roll in. We might need to go ahead and close this off, and just reopen it. And there we go. So we know that we have got three jobs in there, so we get a 33% progress,
01:45
67, and then finally, when this is finished, we get 100. Now, what we're going to be doing in the next couple of episodes is using the progress callback just here to work out how far along each of the jobs are, and we're going to log a model state for each of our jobs in the database.
02:02
What this means is that we'll have a list of all of the jobs within this batch, so the jobs that we get through from our server type. We're going to store these per system in the database, and then we're going to update the state of each one.
02:13
But we're also going to use batch on the UI to work out whether this has been cancelled, or any of the other states that we have for this. So now that we know how to actually get when something progresses, we can use that to start to log the state for each of the jobs that we have.

Episode summary

In this episode, we dive into inspecting the status of your Laravel batches. There are two main approaches: you can chain callback methods like progress or failed directly onto your batch, or you can fetch the batch using its ID and inspect it manually. We focus on the first method here, showing you how to use the progress callback to track how far along your batch jobs are.

We walk through setting up the progress callback so you can log out the batch progress as your jobs run. You'll see how the percentage updates as each job finishes (for example, moving from 33% to 67% and then to 100%). We also go over clearing your database and logs for a clean slate, and starting your queue worker with php artisan queue:work so everything is ready.

Towards the end, we preview what's coming next: we'll use the progress data to update each job's state in the database, so you can keep track of every job in the batch individually. Plus, we'll touch on how to use that batch info on the UI to show cancel status and other states. So by the end of this episode, you'll know exactly how to monitor and log your batch progress—setting the foundation for even more powerful tracking in the upcoming episodes.

Episode discussion

No comments, yet. Be the first!