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.
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!