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
07. Tracking jobs in the database

Transcript

00:00
In order to track each of the jobs that we have pushed to this batch,
00:04
we're actually going to create out a new table, which temporarily pushes these down here to the database. And then we can use model state, which we're going to look at in the next episode to transition the state from pending to in progress, potentially transition to failed if something failed. And then we can really easily in the UI show this progress.
00:26
Okay, let's get started. So what we want to do is somewhere in here, go ahead and say server, and let's just kind of scaffold this out tasks. And we want to batch create all of these tasks inside of here. So that might look like save many, and then we want a list or a collection of all of these tasks as some sort of server task model. Okay, let's go ahead and just bear
00:53
that in mind. And let's create our model for this. So we'll make out a model in here called server task. And let's create a migration alongside of that as well. Just before we forget, let's go over to server task the model. And let's set guarded to false so we could globally unguard everything. And let's fill in the database migration for this.
01:18
Okay, so we know that this needs to belong to a server. So let's go ahead and say foreign ID server ID, and we'll constrain that in the database. Next up is going to be the order. So let's go ahead and create out an integer in here for the order that's going to be 123, etc. And remember, when we created out our app server here, we specify the order, this is going to be
01:41
pretty important. First of all, if you want to visually show which order is in, although you could just use an index. But also, this is going to be easier to pluck this out of the database based on the progress of your batch as well. We'll see that a little bit later. Okay, so we've got an order in there. Let's go ahead and also store a string for the job. So this is going to
02:03
be the job name as the class in the database, not ideal, but it allows us to fetch this out of the database if we ever need it. So this is going to be something like the jobs, create server job, and we'll store the full namespace there just in case we need it later. For now, that's pretty much it. We are going to come back to this and add some more a little bit later. But let's go ahead and
02:27
migrate this out now and get these at least into the database. Okay, let's go ahead and run our migrations. And now let's figure out how we can collect up all of the tasks here, create a server task model from this data, and then persist it in the database. So because each of the server types or entity types that we're creating are all going to share the same functionality, let's create out
02:52
a trait which creates tasks as in that server task model from the list of jobs that we've defined in here. So I'm just going to create our directory in here called traits, and we'll create a class out in here called creates tasks. And let's go ahead and set this to be a trait. And all this is going to do is just have a single tasks method, which is going to collect up the jobs from each of
03:17
them server types and return to us a list of classes. So if we head over to our app server, let's go ahead and use this trait in here. So creates tasks. And now what we can do is when we resolve this out of our factory, we can use tasks method to return all of the jobs but as a server task. So this is what it looks like. We're going to go ahead and use a collection here to collect
03:40
up the jobs from this, we're going to map through them and return a server type model, but we're not going to persist this just yet. So let's see what this looks like. Let's iterate over each of these map through them. For each of these, we will get the task itself, which is going to be the job. And then we'll get the order in here, which is the key. So we could even go ahead
04:02
and type in that as a job if we wanted to. In here, we're going to build that server task up. So let's build that model up by using the server task model that we just created. And we're going to make this not persist it at this point, we want to return these and store them in one go, just for speed. So we're not iterating over and creating too many queries. So in here,
04:22
we're going to have an order, which is going to be the index that we gave this. So remember, that is this thing just here. And then we're going to go ahead and store any of the other data around this that we need. So we storing a job here, which is the class for the task. So we can just use the native get class function for that task. And we can store any other data here that
04:44
we want to as well. We'll come back to this a little bit later, because we're going to store things like a title and a description. So we can persist these and modify them for specifically for the database. Okay, so now that we've made that we need to associate this with the server. So we're just going to say server task server, we don't have a relationship for that just yet.
05:02
But we'll build that. And let's go ahead and associate it with this server, which remember comes from the app server, server just here. Okay, let's go back over to here and finally return that server task that we are creating. Let's create this relationship first of all. So if we head over to the server task model, we know that this belongs to a server, let's create that
05:26
out in here. So this belongs to and server. And we should be good. So let's now that we've collected this up and created out a server task list. Let's see what this looks like over in our server observer. So we'll go ahead and die dump on server type. And let's go ahead and grab out the tasks now. And let's see what we get. So if we head over and click this, there we go. So we've
05:53
now got a collection of each of the tasks in this specific order. So we've got create server all of the details, including that foreign key all the way through. Now what we can do with this is we can use save many to pass these tasks in and create out that relationship. And that will persist that in the database. Now we don't have this tasks relationship yet. So that's a really simple
06:16
has many, let's go ahead and create this out now with tasks. And let's return this has many. And that is server task. Let's create this out. And there we go, we should be good. So let's check it out. We're going to go over to the database, get rid of everything in here that we don't need. We've got our server task model. Let's see what happens. Okay, so over to our creation page,
06:41
let's click this and let's head over to the database. And there we go. So we now have the list of tasks in the database stored. And that means that we can fetch them at any point that we need. In the next episode, we're going to look at adding a state to these with model states in Laravel. We'll pull in a package to handle that for us. And what this means is we can transition
07:03
each of these to a different state. So initially, when our batch runs, this is going to be in progress. Then when this finishes, this one's going to be in progress and this one will be complete. This one will be pending because obviously it hasn't started yet. So now that we have this list of tasks in here, let's talk about model states in Laravel in the next episode.
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!