Playing
01. Introduction and setup

Transcript

00:00
In this course, let's learn how to batch jobs in Laravel to form a step-by-step list of tasks and then apply state support for each job using model states.
00:09
So we can show this in the UI. So here, we're using the example of creating a server, but you can apply everything in this course to any list of tasks that needs to be run and then complete.
00:19
OK, let's go ahead and click Create Server here as the example. And in the background, these jobs are being queued in a batch. And what we're doing is we're periodically
00:28
fetching the status of the jobs using those model states. And as you can see, the list is running through, and it's telling us when each job is being completed. When we get to the end of this, we perform any action.
00:41
In our case, we're just redirecting over to the created server. But obviously, this can apply to any list of jobs that you need to create in your applications.
00:50
Let's adjust one of these jobs that we've got here so it fails, and I'll show you what this looks like. So we're just going to throw an exception from this job. If we come back over to the browser
00:59
and head back over to creating this, let's wait for this to get to the failed job that we are manually failing. And this is going to hang, and it's
01:07
going to show us a status to say that this is failed. Let's just wait for a couple more minutes. And there we go. So I've chosen to represent this as just a black circle.
01:18
And the great thing about this is you can also store the exception so you can tell the user what went wrong at this particular step.
01:26
We're also, at any point, able to completely get rid of this batch of jobs. So I can just click Destroy Server in this case, and it just gets rid of it.
01:35
That removes it from the queue. It removes it from the database, and everything has gone, and we can start again. Let's go ahead and look at stopping this midway.
01:44
If we just start this, we can wait, or we can just immediately destroy this server. Let's just wait for one of these jobs to finish. And once we've done this, we can go ahead and hit Destroy.
01:53
And once again, we perform a few cleanup actions to get rid of all of the state that's hanging around. The great thing about this pattern is we're actually able to go over to a different type of creation for this.
02:06
So in our case, as the example, we're creating a server. Well, what if we want to create a different type of server, one that performs a slightly different list of jobs? Well, in this case, we can just go ahead
02:17
and create a new class for this, add it to our factory, and that's going to predefine the list of jobs that we need to create out. Because we're doing this, what we can also do
02:25
is very easily remove, or add, or adjust the order of any of the jobs inside of here. So let's say that for some reason we wanted to get rid of the PHP step.
02:36
Well, we can go ahead and just adjust the order here, go and restart our queue. And if we head back over here, when we click Create Server, you can see that that has disappeared,
02:46
and we just have three jobs now. So you can infinitely adjust this based on the type of thing that you're creating, or you can add and remove jobs from any of these things
02:56
that you create as well. OK, so feel free to skip the rest of this episode if you already have an app set up, but let's go ahead and get a fresh project created
03:05
so we can get started on building out this functionality. OK, let's go ahead and set up a new project here. We'll call this Laravel Job Batching, and let's go ahead and hit Enter on that.
03:15
We'll use the Laravel Breeze starter kit, and let's go ahead and choose Livewire with the Vault Class API. It doesn't matter too much, and if you
03:22
are not planning to use Livewire, feel free to choose any stack you want. OK, let's run through all of this and get a project set up, and let's go ahead and choose MySQL as our database,
03:32
and let's go ahead and run our default migrations and create that out. OK, now that's done, let's go into that Laravel Job Batching directory, open this up in our editor,
03:42
and we should be good to go. So once that's open, there's nothing really else we need to do apart from just make sure that we can access this and make sure
03:49
that we can register an account. So let's head into our dashboard. We'll be creating the pages out a little bit later once we get to this, but now we are in to a fresh project.
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!