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
09. Transitioning the first job

Transcript

00:00
Let's start with a really simple transition. And that is when we first put this batch of jobs into our queue, we want to take the first job and transition it from pending to in progress. We've already done that using that test web route, but this is now going to form part of our batch. Okay.
00:18
So first thing, let's just clear out everything that we've got in the database once again, and make sure that we are starting fresh. And let's look at another callback within our batch. This is before. So we have a before callback, which is called before this starts to run.
00:33
What we can do in here is access the batch, which we're not necessarily going to need, but we can also bring our server into scope here and we can access our server tasks. So let's grab all of the tasks, but grab the first one that returns to us the first server task in the list. Now, at this point, we need to figure out, well, how do we know that this is in the correct order? Well, what we could do is head over to our server model.
00:58
And remember we stored in the database when we looked at our server tasks, the order, that's the order of things that we know that's defined in our code is the order they need to run. So we can just really easily order this in here by that order. And we can order that in ascending order. So we always know that when we grab the first task from here, it will always be the first one with the order of one.
01:20
Okay. So now that we've figured that out and we know that we're definitely getting the first task that needs to run, we can just do what we did in the last episode, grab the state. And we can transition this state to in progress. So let's go ahead and say in progress, give the fully qualified class name to that.
01:36
And that is it. When this batch gets created, the first one will transition to in progress. So over to the create server button, let's click on this. Let's head straight over to the database with our server tasks, have a look.
01:48
And there we go. The first one is now in progress. Our next job is to listen as each of these jobs progresses, which we've already done with this progress and then transition each of the jobs in order. Let's take a look at how we do that next.
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!