Playing
02. Queue primer

Transcript

00:00
For the purpose of this course, we're going to use simple database queues. But once you're done with this, you can upgrade this to use something like Redis, and you can even bring Laravel Horizon in to make this much easier to manage.
00:11
Let's go ahead and look at a quick queue primer so we know what we're doing. And then once we push this batch of jobs to our queue, you'll understand more about what's happening behind the scenes. OK, so Laravel by default, when we install it,
00:23
create everything is already set up to work with queues on the database. If we head over to our database here, you'll see that we've got this jobs table. So this holds all of the jobs that we are currently running within our queue. When we take a job class in Laravel and we queue this, this will be put into this table.
00:41
Nothing will happen until we start our queue worker. But then behind the scenes, each of these jobs will be run after in the background. So let's go ahead. And before we start up our queue worker, create out a very simple job,
00:54
queue it and then see what happens. OK, so to do this, we're going to go over to the command line and use php artisan make job and we can call this anything we want. Let's just call this example just to get started with.
01:07
OK, so if we open up this example job, let's just take a look at this. So as you can see, we've got this implementing should queue. So by default, when we go ahead and dispatch this job, this is going to go ahead and queue this. Into the job, we can pass anything within the constructor
01:25
and then the main thing is the handle method. This is what we do with this data. So let's say you have a long running task that needs to happen in the background. In our case, the example we're looking at is creating a server.
01:36
We don't want to run all of this functionality in one go as the user hits that button. That's going to be really, really slow. So we want to push it to the background so the user can come away from the page and return back later if they need to.
01:49
Now for this job, we're going to go ahead and just sleep for five seconds. We'll take a look at later passing stuff through to the constructor, but that will go ahead and just do something in the background for five seconds. And then that job will be complete.
02:01
Let's go over to our web routes just to dispatch this as an example. So I'm going to go ahead and create our route here and we'll just call this job and it's quite our closure here. And let's go ahead and send this to the queue.
02:15
So there's a couple of ways that we could do this. We can either use the dispatch helper so we can take the job that we've just created, new it up, pass in any data that we need into the job, and then that will be put into the queue.
02:27
What we can also do is use the class itself. So we can say example, dispatch, and then we can pass any of the arguments that go through to the constructor directly into this dispatch method.
02:39
Either way works. Let's just go ahead and use the dispatch helper. I much prefer doing this. Okay, so now that we've done that, let's go over to the browser,
02:46
head over to that job endpoint and just wait. Okay, so what's happened now is this is being put into the background. Of course, we didn't see a five second delay because this is now queued. We head over to our database and give this a refresh.
02:59
You can see that this has now been put onto the default queue. So we can specify which queue we want to put these things on, and it would be a good idea for the functionality that we're building in this course. And you can also see a payload here.
03:11
Let's go ahead and copy this and just paste this into our editor and just see what we get. So I'm just going to create out a plain text scratch file and paste this in. So this data contains everything it needs to know about that job that is being created.
03:24
So the maximum amount of tries, the actual data that we pass through to there in the constructor, all of the stuff that we need is inside of here. Once we go ahead and start our queue worker,
03:36
this is going to go ahead and start to process this serialized data, and it will use all of the data that we have inside of here. So for example, if we had passed through a user into here, it will still be able to find that data and do something with it.
03:49
In our case, this is going to be using a server that we're creating and a bunch of jobs. Okay. So now that we've got that into our queue, let's go ahead and look at running our queue. So to do this, we go over to the command line and at least locally in a production environment,
04:05
this is a little bit different. You'd want a supervisor to make sure that your queue is always running and you'd probably want to use Redis and Horizon. But for now, at least, let's go ahead and just say queue work.
04:17
And this is a long running process. So while we're building out this functionality, we're going to have to leave this running in the background. And a really important thing is that if we make any changes to the contents of our job,
04:28
we're going to have to cancel off our queue worker and restart it. Any code that you add to this example job is not going to take any effect until you restart your queue. So let's go over to the database again,
04:41
take a look at this and let's look at what happens when we run this. So I'm going to run this head straight over to the database. And this is now queued. Now we had a sleep on here for five seconds.
04:53
So that job has taken five seconds to actually do anything. And then as you can see, it's now being removed. Over in the command line, you'll see that this is running. And then, yeah, sure enough, five seconds later, this is now done.
05:06
So all of this happening in the background while we process our queue and each job is being run one after another. So that is pretty much all we need to know about basics of queues, at least in the database for the rest of the course.
05:20
But what we want to do is push a batch of jobs together that work as an entire unit to our queue. Let's take a look at how we do that in the next episode before we dive in to everything else.
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!