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
11. Handling failed jobs anywhere in the chain

Transcript

00:00
With the kind of batch that we're building here, we want to stop the rest of the jobs from running if there's a failure, and that's going to happen automatically, but we want to mark the job that has failed, the one that's currently in progress as a failed state.
00:16
So how do we do that? Well, first of all, let's go ahead and just pick one of our jobs to fail. We'll use install nginx since that is the one just in the middle here. And we can either immediately fail it or wait five seconds and then go ahead.
00:30
And just throw a new exception in here just to fail this. So let's go ahead and throw an exception and that will now fail. Let's go back over to our stack of jobs that we're pushing in this batch, and we're going to use the catch callback.
00:43
So this will be invoked whenever the first job fails in this batch. Again, we're going to get through the batch into here so we can access any information. But we're going to use the server to grab the task that's
00:56
currently in progress and fail that. So we did that exactly here. So we're going to use exactly the same functionality to get this to work. This will be the one that's failed.
01:05
We'll access the state. We'll come down here and we'll transition this to a failed state from our state list. Okay. Let's go ahead and restart our queue.
01:15
If we haven't already made sure everything's nice and tidy, hit create server, and we'll head back over to our list of server tasks. And again, we'll wait five seconds to the, for the first one to complete. The next one we're going to wait five seconds.
01:28
And that is going to fail that for us. And there we go. So now the entire batch has stopped and this one is still pending. This is exactly what we want.
01:36
If we head over to job batches, you can see we've got total jobs here, pending jobs two and failed jobs one. So everything is now working and we have a failed state, which we can later on show in the UI.

Episode summary

In this episode, we tackle how to handle failed jobs anywhere in a batch job chain. Sometimes, you want the entire batch process to halt if even a single job fails. We walk through forcing a failure in the middle of our chain—specifically on the "install nginx" job—just to see what happens and make sure everything responds as expected.

We use exception throwing to simulate the failure, and then we dive into using the catch callback for the batch. This callback is invoked as soon as any job in the batch fails. Within this callback, we grab the task currently in progress and explicitly mark it as failed in our system.

After running the queue and setting things up, we observe the batch results: only one job is in a failed state, the rest stop running, and everything is neatly marked so we can later show this info in the UI. Basically, by following along, you’ll see how to ensure failed jobs are properly recorded and the whole chain stops running as soon as any hiccup happens.

Episode discussion

No comments, yet. Be the first!