در حال بارگذاری ...

Using Context Stacks0:00

The context has a feature called stacks, which you can think of as an array. So we've been working with just a key and a value, a stack is a key and an array that we can just push things into and then, well, we can get that array and then we can work with the data. So let's look at an example to where whenever we dispatch our contactUs job, we are going to essentially write it into a stack that says that this job was queued. So the idea could be that we could have a base QueuedJob class that would automatically write something to the context so that if we needed to keep track of historical information about the jobs that were queued within the life of a request, then something like this would be useful.

Pushing Jobs into Stack0:45

about the jobs that were queued within the life of a request, then something like this would be useful. Now, I'm not going to go through all of that trouble to create a base class and all of that, but that's the general idea. So here, inside of our handle method, we will simply use the context. But instead of calling add, we're going to call push because we're going to push a value into a stack called queuedJobs. And we don't need to initialize this as an array or anything like that. It's automatically going to be done for us. So I'm going to json_encode this job into the queuedJobs stack so that then we can

Testing Queued Jobs Count1:15

It's automatically going to be done for us. So I'm going to json_encode this job into the queued jobs stack so that then we can do whatever we need to. And in fact, really what I want to do is go to our test and let's test how many jobs are queued up. So we'll just copy one of our tests and we'll set this text, contactUs, queues one job. We don't need to forget the activeAccount in this particular case, at least I don't think so. It really doesn't matter. All that matters is that we have an account here.

It really doesn't matter. All that matters is that we have an account here. So we'll just use the withSession to include that account. We are going to make a post request to /contact. Now this doesn't return a 200. Instead this returns a redirect. So we will assert that the status is 302 and we also need to include some data with this post request. I believe it was called contactMessage and then we'll just have some message, hello from the test.

I believe it was called ContactMessage and then we'll just have some message, hello from the test. And yeah, I think that's going to be fine. We don't need this other assert. So let's get rid of that. So then we are, first of all, let's call this $stack. We will get the queued jobs $stack. Even though this is a stack, we use the get method to retrieve that. And then we will expect that the $stack has just one element and hopefully this is going to pass.

Validating Stack with Multiple Jobs2:35

And then we will expect that the stack has just one element and hopefully this is going to pass. So let's go to the command line, let's run our test and hopefully we see three passes and we do. Let's go to our contact, no, not our contact us job. Let's go to the ContactController and let's just dispatch another job. Whenever we do this, our test is going to fail because we've queued up two jobs, which means that we have two jobs inside of our context, which means that now we can change this contact us queues two jobs and our stack should be two. So if we run that test again, then all three of those should pass and we're good to go.

this contactUs queues two jobs and our stack should be two. So if we run that test again, then all three of those should pass and we're good to go. But of course, I don't want this to be two jobs. I want it to be one job. So let's change back to just dispatching one contactUs job and we're going to be good to go there. So a stack is relatively straightforward. It is just an array in the context. So anytime that you need the functionality of an array to do whatever that you need throughout the lifecycle of a request, then use a stack.

Dehydrating and Hydrated Events3:39

So anytime that you need the functionality of an array to do whatever that you need throughout the lifecycle of a request, then use a stack. But do note it is an array. It's not a collection. Now we have two events that we can listen for and we would typically set them up inside of the AppServiceProvider boot method. The first is called dehydrating and this is an event that fires whenever a job is dispatched to the queue. So we would pass in a closure that is going to get a repository, a context repository, and then we could do whatever we needed to with that context.

So we would pass in a closure that is going to get a repository, a context repository, and then we could do whatever we needed to with that context. So if we needed to supply any kind of extra information to the job or we needed to set up something, then what we would do is simply add data to the context so that we can retrieve that information in the next event, which is called hydrated. And this event fires whenever a queued job begins to execute. And like dehydrating, you simply pass in a closure that is going to get the context repository so that you could do, well, whatever it is that you need to do. So the example that the documentation gives simply passes the locale configuration setting so that when a job is dispatched, a hidden value is added to the context called locale.

So the example that the documentation gives simply passes the locale configuration setting so that when a job is dispatched, a hidden value is added to the context called locale and then the value is supplied from the app config so that whenever the job actually executes, the job can use this value to set the appropriate location. So it would check if it has a hidden value called locale and if so, then it would set the app locale to whatever was supplied in the context. So it's primarily just a way of passing data to ensure that you have that data when dispatching and executing queued jobs. But in our particular example, there really isn't a good reason to use these events, but they are there just in case if you need them.

But in our particular example, there really isn't a good reason to use these events, but they are there just in case if you need them.

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