Switching the UI Theme0:06
All right, so now that you understand the essentials of sending notifications and emails, I think the next thing we should touch upon just a little is cued jobs. So lemme show you Now, before we get started real quick, to be honest, this pink is kind of killing me. So let's do this. Let's go into app css, and I'm gonna swap it out with a different Daisy UI theme called Sunset.
and I'm gonna swap it out with a different Daisy UI theme called Sunset. And I'll show you a little tip. We can set this as the default theme, which means if in layout dot, um, blade PHP, we do not set this attribute, it will use Sunset as a default. So this is great, especially if we only ever have a single theme. Now, if I come back, we have kind of a brownish orange.
Reviewing Notification Email Flow0:40
ever have a single theme. Now, if I come back, we have kind of a brownish orange. It is what it is. Okay? So let's create a new idea, build a house, and we've learned that as part of publishing this idea. We also fire off an email. So if I go into idea controller, sure enough, right here. So you persist the idea and then you notify them, uh, that their idea is now published.
and then you notify them, uh, that their idea is now published. Okay? So if we switch back, if we go to Mail Pit, which is local host port, uh, port 80 25, sure enough, we have our email. So real quick, notice that it's from Laravel, which doesn't quite make sense. Of course, all of that can be configured. Uh, but sure enough, we do have our email and this is fine. So here's the issue. Um, in production, even to this day,
Why Use Queued Jobs1:20
Uh, but sure enough, we do have our email and this is fine. So here's the issue. Um, in production, even to this day, it can sometimes take a little bit of time for that email to fire off. And is that something the user needs to wait for? No. And then next, imagine a situation where as the project manager, when you publish an idea, maybe the entire team should be notified. And let's say there's 50 people on the team. Well, now we have to fire off 50 emails as part of that, um,
And let's say there's 50 people on the team. Well, now we have to fire off 50 emails as part of that, um, publishing request. And once again, does it make sense for the manager to wait for 50 emails to be dispatched? No, it can be done behind the scenes. And whenever you find yourself saying, can this be done behind the scenes, then you want queued jobs, that's precisely what it does in the first place. Okay, so let me show you some things.
Queuing Notifications with ShouldQueue2:04
that's precisely what it does in the first place. Okay, so let me show you some things. If I switch back, let's go into our notification. And as it turns out, if we implement, uh, an interface called should Q, and this is actually kind of magical in a way. When Laravel fires off this notification, it's actually going to read the class and it's gonna ask itself, do you use the should Q interface?
and it's gonna ask itself, do you use the should Q interface? If you do, oh, that's an indication that we should actually dispatch this as part of a queue job. Okay? So, um, let's do this. Let's take 30 seconds to quickly discuss Q versus job versus worker. For worker. Um, draw, draw a comparison to a human person who's in charge of doing a thing, right?
For worker. Um, draw, draw a comparison to a human person who's in charge of doing a thing, right? They're a worker. Alright, what is a job? It's the thing, it's the thing that needs to be done. All right? In this case, the job would be publish the idea, the queue. Think of that as like the container. It, it, it houses all of the jobs that need to be done. So let's say we have 20 different jobs and each of those jobs is something different, right?
So let's say we have 20 different jobs and each of those jobs is something different, right? Those need to be performed. We're gonna throw those onto a queue, big stack of papers, and then a worker is gonna take one of those papers, the job and do it. And then when he is done or she's done, they'll come back and grab another job and do it. And if you want, you can hire more workers, just like a real business, you could hire four workers
And if you want, you can hire more workers, just like a real business, you could hire four workers and then four of them are gonna take, uh, four jobs. 1, 2, 3, 4. They're gonna do it. When each of them is respectively done, they come back and they get the next job, and you cycle and you work through the queue. That's all you need to know. All right? So if I switch back, check this out. Now, if I come back and let's delete this,
So if I switch back, check this out. Now, if I come back and let's delete this, and I want you to notice that our inbox is empty at the moment, okay? If I return to our demo and create a new one, learn more about cues. So now this is key here. If I switch back to mail pits, ah, we don't see the email. So what's going on? Is it just taking a really long time?
we don't see the email. So what's going on? Is it just taking a really long time? No. If we hit refresh, no, it's not gonna come through. It's never gonna come through even if I refresh a thousand times. Okay, so what's going on? Well, here's what you need to remember. We are now dispatching that notification as part of a queued job. So we threw it into the queue,
Running Workers with queue:work4:22
as part of a queued job. So we threw it into the queue, but at the moment, have we hired any workers? No, nobody works and nobody works at the station or whatever the, uh, the comparison would be, right? So nobody's picking up a job and, uh, performing that work. Okay? So how do we hire somebody? How do we, uh, queue up, um, a worker? Well, we can do that from the command line. If I run PHP Artisan,
Well, we can do that from the command line. If I run PHP Artisan, and once again, just look for q If you're trying to learn more about Qs, let's figure out what commands, uh, related to Qs we have. So in this case, we can see that there's a bunch of confusing Q commands. As always, to be honest, there's a, there's a few here that I have never even used before. All right, so that once you care about are q work,
that I have never even used before. All right, so that once you care about are q work, start processing jobs, hire a worker would be a good way to think of it. All right? You can retry a failed job. You can monitor the size of your queues, you can listen to a given queue and you can, um, list out any jobs that did not complete for some reason, maybe an error or an exception was thrown. And then also you can clear out any
or an exception was thrown. And then also you can clear out any of the jobs from a given queue. Okay? So let's start with Q work. All right, the queue had one piece of paper, one job, and we started it and now we're done. Which means now if we switch back, there we go. We have now received the email because once again we have somebody working on it behind the scenes.
because once again we have somebody working on it behind the scenes. It's great. Now, as you might have guessed, we can chew a lot more than just emails, even though that's the most common and typical example. So in your head, just imagine for your project anything that is potentially long running that could be instead performed behind the scenes so as to allow for better performance for the user. Because remember at the end of the day,
Creating and Dispatching Jobs6:11
to allow for better performance for the user. Because remember at the end of the day, that is the entire point of this, we want to improve performance and response times for the user. So if we can do it behind the scenes and then immediately return a response to the user, we should do so. Alright, so as you can guess, we have a make command for this. I can say peach B Artisan, make me a job
we have a make command for this. I can say peach B Artisan, make me a job and name it anything you want. We have an example here. So in our example, like if, if Publish podcast really, or I'm sorry, publish idea was, um, long running, which is not in our example, but if it was, then we might wanna create a dedicated job for that. Uh, let's say, um, maybe as part
a dedicated job for that. Uh, let's say, um, maybe as part of publishing an idea, if we had a team, we, we recompile statistics or something like that. So you might say update idea statistics, if that's something that might take a little bit of time. Um, yeah, what you choose will depend on what you're building anyways, this would be a perfectly fine example for a job. Now you're gonna have a brand new jobs directory.
this would be a perfectly fine example for a job. Now you're gonna have a brand new jobs directory. So if I switch to Peach V Storm, what I really like about this approach is many of these folders you do not see until you require them. And it wasn't always this way in Laravel. And I think that led to a little bit of confusion because sometimes it might be term overload for a new beginner. But now if you don't have a job,
for a new beginner. But now if you don't have a job, then you don't see a Jobs's directory at all and you may never see one for the entire project, which is great. But now we do have a job and if we take a look at it, again, it's just a plain old PHP class. It implements the should Q interface and we've learned what happens behind the scenes.
It implements the should Q interface and we've learned what happens behind the scenes. And then we have the ability to handle the job. And this is where you perform your long running task. You might, um, read the idea and parse something you might, uh, loop over. If we had members, um, or team, we might loop over all of the members, uh, as part of that team, maybe team members for each. And then you recompile their activity and things like that.
of that team, maybe team members for each. And then you recompile their activity and things like that. Uh, maybe you have experience or achievements, uh, that are contingent and updated every time an idea's published or something like that. It doesn't really matter. You get the idea. So if we just wanna play around with this for now, I could say log. Um, the job is being processed
I could say log. Um, the job is being processed and actually I wanna say the update job statistics, uh, job is being processed just to prove that we do see something. Alright? So anytime you log, and this is a, a good time to talk about logging, it's very important when you're debugging and figuring out what went wrong. Um, if we call this logger helper function,
and figuring out what went wrong. Um, if we call this logger helper function, well when this line runs, it will be stored within storage logs, Laravel log, and at the moment it's empty because we haven't logged anything here. Alright, let's give it a shot. So let's manually dispatch the job like this PHP Artisan tinker. Let's find the job. I think it was, what was it? Update idea statistics. And I'm gonna dispatch it.
Let's find the job. I think it was, what was it? Update idea statistics. And I'm gonna dispatch it. Alright, so that's effectively going to throw this job onto the queue. So once again though, if I switch back to my editor notice in Laravel log, I don't see anything. And this is, I promise you this is, um, this is a point of confusion I often ran into when I was first learning because I would keep forgetting that putting something onto the queue does not mean the
because I would keep forgetting that putting something onto the queue does not mean the queue is currently being worked on. And that's why you should always go back to, um, a real life example. And, and sometimes I would use a pizza delivery, right? Um, a pizza to be made could be a job, but it's not gonna be worked on until you have a worker who actually makes that pizza and delivers it, right? Until then, it's just, it's just an order form.
who actually makes that pizza and delivers it, right? Until then, it's just, it's just an order form. We gotta make a pepperoni pizza, but nobody's working on that until you actually work the Q. So let's work the Q PHP, artisan Q work, and there we go. It found the job, it ran it. So now if I switch back, sure enough, we can see, uh, that we processed that job. Perfect. Okay, so here's the other thing I wanna show you.
that we processed that job. Perfect. Okay, so here's the other thing I wanna show you. Let's, uh, hit control C to close out of our uh, q work command. And let's run this one more time. Okay, now we have one job on the queue, but where can we see that? Well, by default, if I open up my ENV file, you can see that the queued connection is database. But you can use, uh, a synchronous driver,
that the queued connection is database. But you can use, uh, a synchronous driver, you can use MIM Patch, you can use Redis, you can make up your own thing if you want. You can use some new third party tool if it has support. Um, but by default it uses the database to handle queues, which, you know, what will, will take you much further than you might think. Okay? So that means if I open up my database and if we go to the jobs table, sure enough,
Okay? So that means if I open up my database and if we go to the jobs table, sure enough, we have one queued job. So we have a job sitting in that stack just waiting to be worked on PHP artisan queue work, pick up the job, complete it. And now if I switch back to table plus give this a refresh, and now we have no jobs in the queue, we are done. And now here's a cool thing. Let's say right now we have one worker running, right?
And now here's a cool thing. Let's say right now we have one worker running, right? Let's say you had got a lot of jobs to work on and you wanna spawn two more workers, you know how easy it is, just open two CLI tabs and run PHP artists and queue work. So now you're running that command three times and you have three workers. And of course, in production you can configure all of this incredibly easily.
And of course, in production you can configure all of this incredibly easily. If you're using a tool like Laravel Forge the dashboard makes it fairly seamless. So that's great. So here's what I'd recommend. By default, there's really no reason for your notifications or mailable to not be queued. So as a default, just ensure that you always implement that interface or you can, uh, set that automatically from the command line.
that interface or you can, uh, set that automatically from the command line. Alright? That's step one. Next, from your environment file, ensure that, uh, your queue connection is set to something other than sync. Sync means well synchronous, right? As part of the current request, it's going to run. Now we're gonna do this behind the scenes. So we'll store these jobs in the database. We will fire up some workers.
So we'll store these jobs in the database. We will fire up some workers. The workers will grab the job and work on it, uh, so that we can return to the user as quickly as possible. And one last time, the entire point of all of this is to improve performance for the user.
