تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Running a Queued Job0:00

Now that you understand how pipelines work, we can return to this code here. What specifically happens when we dispatch this job? OK, let's take a look. We can see right here, for the purposes of the demo, when we handle it, all we do is log something to this file here. So, for example, if I come to Safari, I give it a refresh, we've now dispatched a job, and because we're using the database connection, if I give this a refresh, there's our job. We have one order, or one job, to reconcile the user's account. So that means, if I switch back to Sublime, I'm not going to see anything logged yet because it's sitting there in the stack, it's in the queue waiting for a worker to pick it up and

So that means, if I switch back to Sublime, I'm not going to see anything logged yet because it's sitting there in the stack, it's in the queue waiting for a worker to pick it up and finish the job. OK, let's do that now. php artisan queue:work. Now, a quick tip, if you add the --once flag here, all that effectively means is, pick up the next job, perform it, and then you're done. Don't handle any other jobs in the queue. OK, so now, if I come back, we do see that the job was executed. OK, so that assumes we have a job that should be queued.

Synchronous vs Queued0:58

OK, so now, if I come back, we do see that the job was executed. OK, so that assumes we have a Job that should be queued. However, a Job doesn't have to be queued. It often is, but you can remove this if you want, in which case it'll be performed synchronously. So take a look. Let's reset, and I'll clear out the log file. OK, so now, when I dispatch the job, we will hit the handle method immediately because it's no longer running through a queue. All right, come back to Safari, give it a refresh, and now it's not being thrown into the database.

Dispatchable and PendingDispatch1:27

All right, come back to Safari, give it a refresh, and now it's not being thrown into the database. We immediately see it here. OK, so let's figure out what's going on here. It sounds like we have two paths. One would be the synchronous approach, and the second would be the queued approach. OK, so let's source dive together, beginning at the dispatch method. We'll go to our job here, and you can see it uses this trait called Dispatchable. We've already discussed that. And here we go.

We've already discussed that. And here we go. So when I dispatch this job, it returns to me a new instance of PendingDispatch. There's our next step. So we'll go in here, and if we look at the methods, you'll see once again this is where we have things like delay. So that means here I could say, or excuse me, dispatch this job, but I do want to delay it however long, 30 minutes after they sign up or do something like that. OK. What else here?

OK. What else here? Now, I have this pendingDispatch, but I still don't understand what's happening here. Because we have an instance of pendingDispatch, but then what? Well, if we go down to the destruct method, we talked about this a little, an episode or two ago. It's kind of a funky thing that Laravel does, but it does allow for kind of a neat API. So here you can see Laravel leverages PHP's destruct magic method. So when the object is being destructed, again, automatically, you'll see here the job is officially dispatched.

ShouldQueue Determines Path2:49

So when the object is being destructed, again, automatically, you'll see here the job is officially dispatched. All right. So now we have our next step. Let's dig into that dispatcher class. OK. So we have a dispatch method here, and now we're getting somewhere. So we need to figure out, are we dispatching to the queue, or are we dispatching immediately? And at the beginning of the video, we reviewed both of those. Now we happen to know that we're dispatching immediately.

And at the beginning of the video, we reviewed both of those. Now we happen to know that we're dispatching immediately. But let's figure out how Laravel determines that. So we have a method commandShouldBeQueued. And there it is. So it's looking at the command. Now in this case, it's kind of combining job terminology with commands. Just think of command as your job. So this is your ReconcileAccountJob. So what it's doing is checking, well, does your job, very simple, does it implement the

So this is your reconcile account job. So what it's doing is checking, well, does your job, very simple, does it implement the ShouldQueue interface? If it is, then you want it to be queued. If it's not, then if we return to where we were, it calls dispatchNow. OK. Let's come down to that method. Now again, this allows for things like commands and handler classes, but that doesn't really apply to us. We don't have a handler.

apply to us. We don't have a handler. So we're going to set up a callback function here where we resolve your ReconcileAccount class or job out of the container, and then it triggers a handle method. OK. So now here, I can see that we are calling this method. Now if you're wondering, well, you already have that command, right? And in fact, let's just dd it so you can see it. OK. If I switch over to Safari and run it, sure enough, we have an instance of ReconcileAccount.

Container-Resolved Handle Call4:26

OK. If I switch over to Safari and run it, sure enough, we have an instance of ReconcileAccount. So if you're wondering, well, why don't you just say command handle? The reason is because in your handle method, you have automatic resolution. So that means I should be able to inject and auto resolve anything I need. So we talked about this earlier, like maybe I need the FileSystem for some reason. OK. Well, if I dd that and return to Safari, it's going to blow up. And again, that's because right here, we called the handle method directly. So let's remove it and instead hook up the method to Laravel's Container class.

And again, that's because right here, we called the handle method directly. So let's remove it and instead hook up the method to Laravel's container class. So this is how we can call a method but also resolve any dependencies that are necessary. OK. So now if I give it a refresh, you can see Laravel checked the type hint in the parameter list. It saw we wanted a file system, and it resolved an instance out of the container and gave it to us. So that's why it's being done in this way. So now we're at this point here.

Pipeline and Bus Dispatch5:22

So that's why it's being done in this way. So now we're at this point here. We've set up the closure that's going to resolve and call that handle method, but we haven't quite done it yet. That happens here. Notice the familiar pipeline API. So we're saying send the job, send reconcileAccount through a series of pipes, and then trigger the callback. And all the callback does is it resolves the job and calls the handle method. OK.

And all the callback does is it resolves the job and calls the handle method. OK. So now, well, what pipes do we have? We've learned how to set up pipes, but which ones do we have? Well, if we take a look, by default, that's an array. And now let's see. You'll see there's a pipeThrough method. So if you wanted to send the job through a series of pipes, you could call this method. But in our case, we don't have that. So once again, we dispatch now.

But in our case, we don't have that. So once again, we dispatch now. So this is effectively what's happening for our particular job. OK. So now think about it. We've now figured out that when I dispatch a job in this way, and that job does not implement the ShouldQueue interface, that means it will be dispatched immediately, or synchronously, in other words. Now, we dug through the code and we figured out, OK, what that effectively is doing in the Dispatcher class is sending your job through the pipeline and then triggering your job's

Now, we dug through the code and we figured out, OK, what that effectively is doing in the Dispatcher class is sending your job through the pipeline and then triggering your job's handle method. So that means we would never do this. But just to show you what we're effectively doing here, that means if you wanted, you could resolve the Dispatcher class out of the container and then call a dispatch method. Like so. So here, we would need a new instance. Let me just rewrite this real quick for you, and we'll get something like that. So imagine you have a job here and you want to dispatch it.

Let me just rewrite this real quick for you, and we'll get something like that. So imagine you have a job here and you want to dispatch it. This is effectively what you're doing. Let's give that a shot. I'll come back to Safari, give it a refresh. Oh, we forgot to import this. Eliminate Contracts BusDispatcher. And again, notice that keyword bus. Anyway, so I'll come back. Actually, let's clear out the storage file.

Anyway, so I'll come back. Actually, let's clear out the storage file. We give it another run, and that works, and we get the same thing here. So when I call reconcileAccountDispatch, this is basically what it's doing. And we've learned when you call that method, all that's doing is sending the job through a pipeline. So something like this. I could say, create a new pipeline, and we'll give it the container. OK, so you have your pipeline. We're going to send our job through any series of pipes that you might need, and then, like

Same thing. So one last thing. I don't want to overwhelm you with information. However, yes, you could do this. In fact, let's get rid of all of that. Yes, you can do this. But there's also a Bus facade. You'll see it right here. And you'll notice many of the methods here you're already familiar with, like dispatch. Dispatch a job or command through the pipeline.

And you'll notice many of the methods here you're already familiar with, like dispatch. Dispatch a job or command through the pipeline. So that means this all could be replaced like so. And let me import that one. Okay, this would be the same thing. So again, I come back, give it a refresh, switch back here, and there is our second lock. Exact same thing. So in closing, you now know that when you call bus dispatch, it's ultimately going to trigger this method here, where we figure out, well, should it be queued or should it

So in closing, you now know that when you call bus dispatch, it's ultimately going to trigger this method here, where we figure out, well, should it be queued or should it be dispatched immediately? In our case, it's immediately. And then in our case, we have a job. So it wraps the call to the handle method in a closure so that it can then be sent through the pipeline. All right, so we understand that. In the next episode, if this is a queued job, which it will often be, in most cases, it'll probably be queued.

In the next episode, if this is a queued job, which it will often be, in most cases, it'll probably be queued. So what happens on that condition? Let's find out.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟