Playing
03. Batching jobs in Laravel

Transcript

00:00
OK, so let's take a look at the difference
00:01
between dispatching a single job and doing this in a batch. We need a batch because our creation process of a server or anything that you're doing all need to happen together, one after each other.
00:15
And these all need to be aware of each other in a single batch. This means that if anything is canceled, we can cancel the rest of the batch.
00:22
If someone clicks a button, we can cancel the entire batch of jobs that we're running. We can't do that if we are dispatching individual jobs for each step along the way.
00:33
OK, let's go ahead and create out some jobs that we're going to use for the rest of the course. So over in our jobs section just here, let's go ahead and create some out for our server creation.
00:42
So let's make out a job. We're going to put this in a server directory. And the first one is going to be called create server. So this is the first job.
00:50
We're going to create the server and then perform an action after this. Maybe the next one is installing Nginx. So that's our web server.
00:58
And then maybe we have another one to install PHP, which we saw from the example. And then finally, maybe we have a finalized server job. So you can add as many of these as you want.
01:10
It doesn't matter. Now, each of these jobs, rather, like I just said, than doing these individually, so if we were to do create server in here and then dispatch another job,
01:21
so install Nginx, for example, these are not aware of each other. So if something fails at the create server point, the install Nginx job will probably
01:31
have already been tried to run as well. So we want these to happen in a very specific order. And we want these to be grouped together. And that's where batches come in.
01:40
So there's a slight difference that we need to make to each of the jobs that we create when we form a batch. And that is to use the batchable trait.
01:48
So we can either tack this onto the end, or we can just say use batchable. So from illuminate and bus, we just want to use this trait in here.
01:56
This now makes this job batchable that we can form as part of a batch. So we're going to basically do that for every single one. Let's do that and put in the namespace for each of these.
02:06
And then once we've done this, we can start to create our batch. OK, so each of these is batchable. How do we create a batch?
02:13
Well, we do this in a very similar way, but the syntax is completely different. So what we do is we manually pull in the bus facade from Laravel.
02:22
And then we go ahead and say batch. Into this, we pass an array of all of the jobs that we want to batch together. So in our case, we just want to new these up.
02:31
So let's say new create server. And we'll go down to the next job. These will happen in order as well. So at this point, we want these to be in the order
02:39
that we want them to run. Let's go ahead and say install nginx. And we'll new up install php. And then finally, we will have the finalized server job here.
02:50
Great. OK, so this, just running this on its own, is not going to do anything. The last thing that we need to do
02:57
is go ahead and dispatch this, much like we did with the dispatch helper or the static dispatch method. So we're just going to say dispatch.
03:04
And that's it. That's going to put that into our queue. Now for each of these, let's just go over to them. And under handle, go ahead and add in a sleep.
03:13
Let's just choose two seconds for now. So we'll go over to each one and just do something in here that takes a little bit of time. Once we've done this, we can go ahead and run this on our queue.
03:24
OK, so each of these is now going to run for two seconds. And of course, into here, you can pass any dependencies. You can do anything within the handle method to get this to work.
03:33
OK, so we're going to come over and just start up our queue. So let's go ahead and say queue work. And let's go over to the database and make sure everything's clear here.
03:42
And let's go ahead and run this. So let's run this in here. Let's go back over to our terminal. And as you can see, each of these is now running in succession.
03:50
So one after another, they're running. Obviously, we had that two-second timeout. But you can see these are running one after each other. If we head over to our database and over to the job batches table,
04:02
you'll see that this has been assigned a batch ID. So this allows us to identify the batch. What this means is that once the batch is in the database or wherever we are running our queues and it is working,
04:16
we can actually pick this out of the database and query the batch with any details. So as you can see, what this does is it stores the status of the batch. So it tells us how many jobs are in the batch, the pending jobs,
04:30
which in our case is zero because all of these have already been run through, and how many have failed. And it will tell us which jobs have failed as well. It'll also tell us when this was canceled.
04:41
If it is, of course, by default, this is null. But what this means is we can very easily within our code pluck out this, query it, work out the percentage completion of the entire batch, work out how many pending jobs are still to be run.
04:54
And we can find out a bunch more data about this, which we're going to use throughout the course. So now that we have seen job batches, it's time to go ahead and build this up as an example,
05:06
as if we were implementing this in a real application. And then what we're going to do is instead of relying on all of this data to give the status of each job, we're going to use model states to make this much easier.
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!