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.

Episode summary

In this episode, we're focusing on improving our job queue by automatically transitioning the very first job from a 'pending' status to 'in progress' as soon as the batch of jobs is put into the queue. Previously, we just tested this using a simple web route, but now we're making it a real part of the batch processing flow.

We kick things off by clearing out the database to make sure we're starting with a clean slate. Then, we dig into adding a before callback to our batch—this callback runs before the batch tasks actually get going. Within that callback, we grab our server's tasks and ensure they are sorted by the order attribute. This way, we always know which task is supposed to go first.

Once we've got the first job in the right order, we use the same status transition as before and move its state from pending to in progress. Finally, we test things out—when we create a new batch of server tasks, we check the database and confirm that our first task is sitting at 'in progress' just like we want.

Next up, we'll look at how to handle transitioning each job as they finish, so the entire batch of jobs runs smoothly in the right sequence!

Episode discussion

No comments, yet. Be the first!