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
04. Properly triggering a job batch

Transcript

00:00
So we've spoken about this batch ID. Now, when we go ahead and crowd some sort of entity in our application that needs
00:07
to keep track of this batch progress, we're going to need to store this batch ID on that model. Let's take a look at the flow of triggering a job batch or dispatching a job batch and storing this so we can later go ahead and query it. Okay.
00:24
So we're going to head over and we are using the example of servers. So let's make a model out called server here. And let's go ahead and create a migration with that as well. If we head over to the create servers table, let's just fill in some basic
00:37
metadata about this, so we'll just keep this really simple. We'll set up a type. This is going to be something like an app server that we want to create a database specific server, anything that could have a different list of jobs associated with it.
00:52
This is completely optional, but it's just going to add a little bit of flexibility. If we want to dispatch a different set of jobs per server. Okay. So we are going to add in a timestamp in here when this was provisioned.
01:05
So this will come a little bit later, but let's add this in here. So let's say provisioned at, and we will make this nullable. Okay. So the really important thing in here is we need to store a string with the batch
01:18
ID so we can query this later and we can set this to nullable because when we initially create this, we may not have a batch ID. Okay. Let's go ahead and run the migrations on this.
01:29
And then we'll go ahead and set up a really simple button within a live wire component to click and create this server out. Okay. So we'll start out by using live wire to make out a create server component.
01:40
This is going to be a full page component. Let's head over to our web routes and create this out. We can get rid of this batching example as well. Okay.
01:48
So down here, let's go and create our route to create a server or any of the entities that you're creating. So we'll say servers slash create. We're going to go ahead and point this to that create server live wire component.
02:02
And we've called the job the same. So just make sure we're importing the live wire component here and we can actually get rid of the example job here. We can get rid of all of these actually, just to make this a little bit
02:15
easier and we can get rid of these too. Okay. So let's go ahead and pull this in properly. And we'll pull that in from our live wire page.
02:23
Great. Okay. So let's add our middleware and name to this as well. And we should be done.
02:28
Okay, great. So let's call this servers dot create and let's head over to servers and create in the browser. Okay.
02:37
So we don't have a layout at the moment. So if we head over to this component, we can go ahead and use the layout attribute to point over to the default layout that we get within Naravel Breeze and there we go.
02:47
Great. All right. So if we head over to the component here, let's just start to build this out. We kind of want this to look like our dashboard, so we can just
02:54
copy the whole thing over to here. Get rid of the base layout at the top and the bottom, and we can indent everything back in and we'll have our header in here. So we'll call this create server.
03:06
And then we're going to add a button down here to create that out. Let's head back over and there we go. So let's add a button to create this server out now. All right.
03:14
So let's get rid of this actually, because we're not going to use that container and let's just use a primary button from this component set of Breeze to create our server. So when we click on this, we want to kick off the process of obviously
03:30
creating a server, but also then going ahead and starting that batch. So let's hook this up with wire click to create a server and let's head over to the create server component and create our method to deal with this. And again, if you're not using live wire, it doesn't matter.
03:49
You can use any method here to trigger everything that we're about to do. So let's create this create server method out. And in here, we're going to create the server. So let's go and assign to this server and create.
04:05
And then in here, we just need to pass in the type. So in our case, we're just going to hard code this to an app server. But of course you might have a form for the user to choose which type of thing they're creating out, in which case this would be the thing that you would change over.
04:19
Okay. Let's just make sure this is working. So when I hit create server and yeah, let's just go ahead and set over in our server model, everything to unguard.
04:28
So let's say protected guarded, and we'll set that to false. Let's try that one more time. And there we go. So over in the database, we should now have a server created.
04:38
Okay. So it's up to you where you actually trigger the batch, but to be honest, putting this directly inside of here, the batch creation here, it doesn't feel right to me. It makes sense considering a server could potentially be created by anyone to
04:53
have this inside of a model observer. So we'll create a model observer out. We'll create the batch. We'll get the batch ID associated with the server.
05:02
And then the flow of creating a server is pretty much done. We can just handle any of the batch events from there. So let's go ahead and create an observer out for this. So let's make an observer.
05:13
We're going to call this server observer, and we're going to pass in the model server. So this, everything gets hooked up. You don't need to do that, but we'll do that to get some scaffolding. And then we can just do nothing in here.
05:24
So you can just create the server and do whatever else, what you need to do, show a message, anything. Okay. So let's go over to the server observer and let's take a look in here.
05:35
So we've got a created event created by default. Let's just get rid of everything else out in here and just focus on this created event. So when a server gets created, either by the user or by an admin panel, here is where we want to start to create this batch.
05:52
So let's start to do that now from what we looked at earlier. We'll go ahead and use the bus facade. We use batch. We're going to pass in a list of these that we want to create, and this is going to
06:01
change up a little bit later, depending on the server type, which we've passed in. We'll use a factory to have a predefined set of jobs that we want to perform based on the type of model. Okay.
06:12
So in here, let's just new all of these up. So remember we have create server from our server jobs and let's new up install Nginx, let's new up install PHP and let's new up finalize server. And we're pretty much done.
06:29
Remember, we want to go ahead and dispatch this in here as well. Okay. So let's go ahead and make sure this works. And then we'll figure out how we get the batch ID to update the
06:39
server once it's been created. Before we do that, let's just make sure we head over to our server model and make sure that we have our observer hooked up and we can use an attribute for this. So we can use the observed by attribute and we can pass into here any of
06:53
the observers that we've created. So in our case, that's the server observer. And once we pull that in now, that will be hooked up. Okay.
07:01
So let's go over and try this out. So I'm going to go back over to the database. I'm going to clear out this job batches table. And if we head over to the interface and hit create server, that will of course
07:11
have gone ahead and created the server for us, but it should have also created that job batch and you can see that it has tells us how many jobs there are, the pending jobs, all of that information. But over in here, we don't have the batch ID.
07:24
So how do we figure out the batch ID from this? Well, if we go ahead and assign this batch to a variable, we can pluck the ID directly from this. So because we're using an observer, we have the instance of the
07:37
server that's been created in here. And then at the end of this, we can just use server update and go ahead and update that with the batch ID. So in our case, we've got the batch in here and we can just pluck the ID out.
07:52
Now, what we can do just before we do this, let's just take a look at everything that this batch gives us when we create this out. So if we hit create server again, you can see that we get an instance of this batch, it tells us all of the information that we have within that table.
08:05
And this batch class also has a bunch of other methods as well. So as an example, let's go over to batch.php and just have a look at the method list. So you can see that we've got a list of pending jobs here. We can cancel this batch.
08:19
We can see if it was canceled. We can check if it's finished. We can delete this fresh to fetch this fresh data. If we need to check, if it has failures, there is a ton of
08:30
stuff that you can do with that. And we're going to be covering a lot of that through the course. Okay. Let's get rid of that die dump.
08:35
Let's go back over and hit create server that has now been created. And now over here, you can see that the batch ID is being stored. So what we can do now is during the life cycle of our queue running and us hitting the page, whether we navigate away and back again, we can fetch the batch out
08:53
using this batch ID that will return to us a batch class that we just opened. And then we can just start to query it. And when we later on couple this with LiveWire polling, we can just continuously fetch any information about this batch that we need.
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!