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
16. Polling to show status

Transcript

00:00
Let's first of all, update our entire state here.
00:02
So if the server is provisioned, we show a message and then otherwise we're going to show our progress list. And we're going to set that to poll for a certain amount of milliseconds to update the state.
00:12
So the first thing that we want to do is come over to the server model. So let's open that up and let's go ahead and create out a helper to check if this is provisioned. So we'll say is provisioned.
00:24
And really this is just if the date is not set. So we'll just say not is null and this provisioned at, and of course you can change this up to whatever you are doing with your models. Okay.
00:37
So now that we have this helper, we're going to go ahead and just add an if statement in here and say server is provisioned. If it is provisioned, we'll go ahead and do something. Otherwise we will do something else.
00:48
And in actual fact, I'm going to end the if there, and I'm just going to duplicate this down just to keep things tidier rather than having an else. So let's end the if there, pull this up and let's go ahead and add just a little div in here with some styles.
01:05
So let's set a background to grade 200, make that rounded large, set some padding and set the text to center. And in here we'll just say server. And then output the server ID in here as well.
01:20
Okay. Let's just update this to save not provisioned. And once the server does get provisioned and we're polling, let's just go over to this and set this in here to the current day and time.
01:30
We should see this. Great. Okay. So now let's look at going ahead and pulling this to get it to update.
01:38
So to do this within LiveWire, we are going to wrap this entire thing in a div. This is incredibly straightforward to do. And of course you could use WebSockets to update this in real time. But I think pulling this, particularly if you're using a LiveWire makes sense.
01:53
We're going to say wire pull. Then we're going to give a timeout. Now you can choose anything you want in here. We'll leave it as the default.
01:59
I'm just going to say every 300 milliseconds, or we could even change that, bump it up, depending on how accurate you want this to be in real time to 500 milliseconds. So now that we're doing that, if we head back over and unprovisioned this,
02:14
first of all, we're going to create a completely new server in a second. What's happening in the background now is this is going to just keep pulling this to update it. That's probably too fast actually.
02:24
So let's go ahead and set this to every two seconds. That kind of makes sense. It's entirely up to you what you do. Okay.
02:30
So now that this is updating every two seconds, what this means is that when we create the server and we have our queue running, this is always going to be kept up to date. Let's go ahead and just get rid of everything that we've already got in
02:41
the database and redirect over to this and see what happens. So if we head over to the database, I'm going to get rid of, let's get rid of our server tasks first. Get rid of our server, get rid of our job batches.
02:54
And let's start from scratch. So now when I click create server, as long as the queue is running, so let's just make sure we are running our queue somewhere over here, PHP artisan queue work, we should see this update.
03:08
So if I click create server, that's now pulling in the backend. We know that our state is changing in the database. So when this iterates through, it's going to update this every two seconds and you can adjust this if you want.
03:21
So let's wait. I think this was five seconds. This one. And there we go.
03:27
That's been changed. And again, we're just going to keep waiting for this and there we go. So we've got a slight sort of delay in the polling because it's two seconds and we'll see some overlap, but as you can see, once that gets to the end,
03:40
the data changes and the entire view here is refreshed and we end up with our server. And then when we refresh, of course, there are no tasks it's provisioned. So we just see any of the details about our server or whatever you were running jobs for.
03:55
So super simple with Livewire just to go ahead and pull this to update it. I'm actually going to decrease this very slightly as well. So there we go. We're now getting a fresh up-to-date list of all of the status of every single task.
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!