Basic File Logging0:00
Let's begin with the most basic of examples. Logging something to a file. We'll say, hello there. All right, so if we return finished, and then I hit this route, like so, we should now have a new item in the logs. And there it is. Okay, but now let's run it through a queue. Now a quick heads up, you never need to send basic logging through a queue. It's very, very fast. This is only an example to get us up and running, and then we'll review more practical
Dispatching a Job0:27
It's very, very fast. This is only an example to get us up and running, and then we'll review more practical examples. Okay, so I will tweak this code here, and we're going to run it through this dispatch helper function. Now we can reference what we might call a Job class, or we can even pass a closure here. It's really nice that Laravel allows for it. And behind the scenes, everything gets serialized properly. So now we're saying, I want to dispatch a new job, and that job is just going to log something to a file.
Sync Queue Default0:53
So now we're saying, I want to dispatch a new Job, and that Job is just going to log something to a file. Okay, so if I switch back now, and we try it again, here's the thing. It's still going to be instant, as you see there. So let's figure out why. I'll delete that, and we'll go into my config, queue section, and you'll see the default queue connection is sync, and that stands for synchronous, which means anything I run through a queue will happen just as if it was not in the queue. It happens synchronously, along with every other action in the request. All right, let's change it though.
Switching Queue to Redis1:25
It happens synchronously, along with every other action in the request. All right, let's change it though. Now if we scroll down, you'll see here are all the different queue connections we can reference. So for example, if I want to go through a database, that's perfectly fine in many situations. I can use a tool called Beanstalkd. I can use Amazon SQS. I can use Redis. I can even write my own connection in my own driver. Let's start with Redis.
I can even write my own connection in my own driver. Let's start with Redis. I have that on my machine. If I run redis-cli, you see it's available. If not, you can do a quick Google search to figure out how to pull it onto your machine. It's very simple. Okay, so we're going to update our default queue connection to be Redis. So you'll see it's reading an environment item here called QUEUE_CONNECTION. Let's go to my .env file. If I scroll down, again, out of the box with Laravel, it's set to sync.
Let's go to my .env file. If I scroll down, again, out of the box with Laravel, it's set to sync. We're now going to change it to Redis. Okay, now if we give this a run, it might blow up, and it does. And that's because if you want to use Redis queues, well, you need to pull in an extra package. We can do that like so. Again, don't feel like you have to memorize that. You'll see it right there in the documentation setup. Excellent.
You'll see it right there in the documentation setup. Excellent. So let's come back and give it another refresh, and there we go. Okay, so now think what's different. In the first example at the beginning of the video, we hit the route, and as part of that request, we immediately logged information to a file. However, after that, we changed it. Now we're dispatching this job. We're throwing it onto a queue just like the pizza delivery example in the previous episode. So that means if I switch over to my log file, I'm not going to see it yet.
Running a Queue Worker3:29
So now we have this stack of jobs at the end of your desk, jobs that need to be completed. And at the moment, you don't have a worker. There's nobody there to help you, so the job doesn't get completed. All right, take a look at this. If I boot up php artisan, you'll see you have a queue section right here. Take a look at this right down here at the bottom. Queue:work. Start processing jobs on the queue. That sounds like exactly what we need. We need a worker to knock out these jobs.
That sounds like exactly what we need. We need a worker to knock out these jobs. Okay, php artisan queue:work. Aha! So now, take a look. If I switch back, we'll see all of those times I refreshed the page, that created a total of four jobs, and because we now have a worker, one by one, it completed those jobs. So what this means, once again, just to drill this in, when we hit this route, we're not logging anything yet. We queued it up for somebody else to handle, and then we returned.
logging anything yet. We queued it up for somebody else to handle, and then we returned. Okay, so again, notice if I run this and refresh, I will now see this log to the file, and that's because we do have this queue worker up and running. But if I hit Ctrl C and close that out, delete this, we're going to be in that same boat again. I refresh the page, and nothing happens here because the worker ain't working. Nobody's there to help you out. Don't forget that. You need to boot up a worker.
Delaying Job Execution4:50
Don't forget that. You need to boot up a worker. Again, using our pizza delivery example, almost think of this as your worker clocking in. And later, I'll show you on your production server how to run this command and ensure that it never accidentally closes out or cancels out. Okay, let's finish up by reviewing one more fun example. Delayed job execution. So we're going to say, let's delay this job two minutes. So right now it's 11.01, something like around 11.03. I want this to execute.
We don't see any jobs yet, and that's because, once again, we've delayed the execution. So let's go ahead and fast forward about a minute at this point, and we should finally see the job go through. And there we go, right on cue, so to speak. We see our log. And if I switch over to the terminal, only when it was ready did it process that job. All right, so in this lesson, you learned about the helpful dispatch function. This will accept either a dedicated class or a closure, which we chose in this case. Secondly, we learned how to delay the execution of a job. We could just as well have said, in a day or in two days, I want you to execute this.
Secondly, we learned how to delay the execution of a job. We could just as well have said, in a day or in two days, I want you to execute this job. Anything you want. Now, if you're curious when and why you might do that, probably in most scenarios, you won't. But there might be situations like the User signs up, and maybe you want to update their account or prepare a notification 30 minutes after they've registered. Or maybe you want to schedule an email for 24 hours after they register. This would be one way to do that. All right, let's keep moving forward in the next episode.
