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
08. Working with model states

Transcript

00:00
Okay.
00:00
So let's talk about model states and how they're going to help us. We'll get everything set up. First of all, using this package from sparsey, and then we'll go ahead and look at how we transition each of our jobs within our batch to a different state.
00:13
Okay. Let's go ahead and just get this installed and then we'll go ahead and look at how this works. So let's go over and pull this package in.
00:20
We'll go over and see if there's anything else we need to do. We can publish the config file, but to be honest, we don't really need to. Okay. Let me show you how to set the rest of this up.
00:30
So remember our state exists on the server task that we created. So if we open up the server task model, the first thing that we need to do is pull in the has state trait or has state trait from this package. The next thing we want to do is make sure we cast some kind of column to
00:49
a specific state that we have set up. So let's go ahead and just add in our cast. We don't have a column. We don't really know what we're casting this to at the moment.
00:57
So let's go ahead and figure that out. So what we want to do is add a state column to the server task, which will give us a representation of each of the states that we have. So let's go ahead and make out a migration here and add a state
01:13
column to our server tasks model. And let's create this out. So add state to server tasks and let's go down. Okay.
01:22
So we just want this represented as a string. So let's go ahead and call that state. And we'll set this nullable. We probably won't have any state when we first start this, but
01:33
we can set a default state as well. So let's just set that to nullable for now and see how we go. Okay. Let's go ahead and migrate our changes.
01:41
And there we go. Okay. So now what we want to do is cast this state, which we can pluck out as a overall state class.
01:50
So to do this, let's just comment that out temporarily. We want to go and find somewhere to create this out. Let's create out a directory in here called states. And we're going to create an overall state class using this package for any of
02:04
the states that our server tasks can hold. So let's create out a class here and we'll call this server task state. Let's create this out according to the documentation. So this is going to be an abstract class called server task state.
02:17
Doesn't matter what you call this. And we're going to extend the base state class that we have within that package. Now into here, we're going to define out the config for this. Let's just go ahead and define this out.
02:29
And then we'll look at what this allows us to do with each of our states. So we have a config in here, which returns a state config class. And then in here, we are going to call parent config. And then now we can just start to define this out.
02:45
This needs to be static as well. Okay. So the config for each of our states is as follows. By default, we want this to be in a pending state.
02:55
So we don't have that just yet. So let's just write this down here. The next thing that we want to do is allow a transition. So we want to allow each of our states to transition from a
03:05
certain state to another state. Now let's just think about this. We want, before we define these out, we want pending to be allowed to transition to in progress.
03:16
Let's just say in progress. So that's our pending state for any of our tasks. They will then be able to go into an in progress state. We do the same thing again.
03:28
And let's think about the next one in progress needs to be able to transition to complete. And then finally, we need to have an in progress transition allowed to transition into a failed state.
03:46
So this is pretty much everything that we need to do. We just need to figure out the transition classes or the state classes that we want to create, but pending can go to in progress. In progress can then go to complete for each of our jobs,
04:01
but also our jobs can fail. So in progress could also go into failed. Okay. So how do we define out each of our states?
04:08
Well, inside of here, we just want to create out a new class and let's just start with the pending. First of all, and then we can just copy and paste these over. So these are really simple.
04:17
They just need to extend the base server task state that we have created. So let's go ahead and extend that. And that's pretty much it, to be honest. So let's go ahead and just start to update what we've got here.
04:27
So the default state is going to be pending. So let's create out a pending class string in there. And then we want to allow pending to go into an in progress state. Let's go ahead and create out all of these and then we'll just map everything up.
04:43
So we want an in progress state and we want a complete state and we also want a failed state. So we just create out all of these classes in here. Let's go ahead and make sure each of these extend the server task state.
05:01
So let's extend server task state from here. We'll do the same for this one and the same for this one as well. Okay. So now over in server task state, we can just define these out.
05:16
So in progress, and let's get rid of that comment. And then we want in progress to be able to transition to complete. And we want in progress to be able to transition to failed. So this might all seem a little bit complicated, potentially pointless,
05:40
but now this means that we cannot transition any of these in any other order apart from what we have defined. Okay. So now that we have this set up with the cast, if we just head back
05:51
over to our server task, we can now add in this server task state as our cast. When the tasks get created, what this will now do is it will set them all as a default of pending. We'll quickly look at how we can transition these and then we'll
06:10
build this into our batch progress. So we can progress each one as they go. Okay. Let's go ahead and try this out.
06:16
So we'll go over to the database, clear out everything that we've got in here. So all of our servers, all of our tasks, all of our job batches, and let's go ahead and create one out. And if we've done anything wrong, we can go ahead and fix it up.
06:27
Let's click create server. That looks like it worked back over to the database. Let's just have a look at our server tasks and sure enough, we now have a state column.
06:36
So let's go ahead and open that up. And there we go. So we now have this state represented as the pending class. What we can do is we can take any of these tasks now and we can transition
06:47
them to a new state as we have defined in our rules, let's just manually do that within some sort of web route or something for now, and then we'll look at hooking this into our batch flow. So let's just take one of these IDs.
07:00
So let's just take four and transition that into the in progress state. Uh, we'll do this just on our web routes, just to save a bit of time and just keep this as simple as possible. So let's go ahead and create out a really simple route here to test
07:13
this, and we'll just say state. And in here, we're going to grab out the server task by its ID. So let's pull in our server task and we'll find that by its ID. Then what we do is we access the state.
07:26
So that's the casted thing that we have defined within our model. And then we can say transition to, and we can just give a new state. So in our case, we will transition from pending to in progress. And we just give the fully qualified class name to that.
07:42
So when we hit this, it's going to take this task, grab the state and transition it to in progress. If we hadn't defined out the state, just inside of here that we could do this, it wouldn't allow us to do it.
07:54
We'd see an error. Okay. Let's go over to forward slash state and just check this out. And there we go.
08:01
Let's go over to the database, give this a refresh. And there we go. We've now transitioned the pending for this one to in progress. Now, what this means is that when we build out the UI, we can very
08:10
easily list through all of the tasks and we can work out what the state is. And we can visually update this to show that it's in progress. If it fails, we can show this as well. And we'll do that later and add some helper methods to allow us to easily
08:26
change around some classes, render stuff out. For now, though, we need to work out how to do this, but as part of the batch. So we need to listen for any events within our batch and transition each of these as we go.
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!