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

Why Use Async Tasks0:00

When we interact with a web application, we expect all interactions to be fast. You click on a button and you expect to see some kind of a response within a couple of seconds maximum. If anything took longer than that, you would immediately assume something is wrong. Programs also have the same expectation. So here if we hit this API endpoint from a program, the program expects a response within a few seconds. As a developer, I'll make sure to configure a proper timeout that terminates the request if it takes longer than a few seconds. Otherwise, my program may get stuck waiting for the endpoint to respond, and that could

if it takes longer than a few seconds. Otherwise, my program may get stuck waiting for the endpoint to respond, and that could affect the entire system. Waiting for things to happen is necessary though. But in general, waiting too long for HTTP responses is bad for so many reasons. If any task in our application requires a long time to finish, it's better we run that task asynchronously. Respond to the HTTP request first, and then do the work later. That's what this series is all about. I'm going to show you how to run tasks asynchronously in Laravel.

Creating a Laravel Job1:07

That's what this series is all about. I'm going to show you how to run tasks asynchronously in Laravel. Laravel ships with a high performance queue system that allows us to run tasks asynchronously, monitor them and handle failures. Here I have a fresh Laravel application. We are going to start by creating a task or in Laravel terms, we're going to create a job. To create a job, we are going to the terminal and we are going to call the php artisan make:job command. So php artisan make:job and we are going to call our DropSendWelcomeEmail.

job command. So php artisan make:job and we are going to call our DropSendWelcomeEmail. And we hit enter and the job is created. We go back to php store and under the app directory, we find the jobs directory. And that's where our newly created job is placed. We click on a job and we open it and we can see that every job created by the artisan command will have two methods, a constructor and the handle method. Inside the handle method here we put all the business logic for our job. Our example job here sends an email. But since this is just an example job, we are going to use that php sleep function to

Running Job Synchronously2:14

Our example job here sends an email. But since this is just an example job, we are going to use that sleep function to simulate work being done. So we are going to sleep for three seconds. And we are also going to add a log line just to show us that the job actually ran. So here is now that we have our job ready, let's run it in the route route that comes with a fresh Laravel application. So we are going to web.php route file. And in the route route, we are going to call the job. So we are going to create an instance of the job, SendWelcomeEmail and finally call the.

And in the route route, we are going to call the job. So we are going to create an instance of the job, send welcomeEmail and finally call the handle method. Now let's go to the browser and visit this route. So we hit refresh. And as you can see, the browser is loading for three seconds before it responds back to us. And that's because we added a sleep method inside our job. We are not sending the job to the queue. So let's do that.

Configuring Database Queue3:12

We are not sending the job to the queue. So let's do that. But before we can send the job to the queue, let's configure our queue first. To configure the queue, we go back to phpStorm and visit the queue.php configuration file. Inside the configuration file, we can see multiple queue connections, one for the database been stocked, sqs and redis. These are all different storage drivers that Laravel queues support. In our example application, we are going to use the database queue driver. So we need to configure our database in order for jobs to be stored there. So let's visit the Laravel documentation and find the driver notes and prerequisites.

So we need to configure our database in order for jobs to be stored there. So let's visit the Laravel documentation and find the driver notes and prerequisites. Check the database section. And here we can see that we need to run php artisan queue:table to create the migration that creates the jobs table where the jobs are going to be stored. So we go back to the terminal and run php artisan queue:table. And after the migration is created, we are going to migrate our database. So php artisan migrate. And that's it. If we go to SQLPro and refresh, we can see the newly created jobs table here.

Dispatching Job to Queue4:19

And that's it. If we go to SQL Pro and refresh, we can see the newly created jobs table here. And now that we have the database ready, it's time we dispatch our job to the queue. So we go back to phpStorm and head to the routes file. And instead of calling the handle method directly, we are going to dispatch the job using the dispatch helper. So we are going to call dispatch on the SendWelcomeEmail job. And we are going to remove this from here. Now if we've edited the browser, the job is going to be dispatched to the queue. But as we saw, there are multiple connections to the queue.

Now if we've edited the browser, the job is going to be dispatched to the queue. But as we saw, there are multiple connections to the queue. Let's find what's the default connection that we have in our .env file. So if we visit the .env file and check the queue connection, it's the database. So we must make sure that the queue connection is correctly set to the connection that we are going to use in our application. And in our example, we are going to use the database connection. So let's head to the browser and refresh. And as you can see, we got a response back immediately. The job didn't run.

And as you can see, we got a response back immediately. The job didn't run. We didn't have to wait for three seconds. And that's because the job was actually dispatched to the queue instead of running. If we go to the storage/logs/laravel.log file, we don't see anything printed. That means that the job didn't run. Now let's check the database and refresh. And we can see that the job is dispatched to the queue. And if we check the payload, we can find that the command or the job that was dispatched is the SendWelcomeEmail job that we dispatched from our route.

Starting Queue Worker5:56

And if we check the payload, we can find that the command or the job that was dispatched is the SendWelcomeEmailJob that we dispatched from our route. Now that we have the job stored in the queue, let's run the job in the background. And to do that, we need to start a worker. A worker is a php process that scans our queue store. In this case, it's the database and runs any jobs it finds there. So let's go to the terminal to start a worker. To start a worker, we need to call the php artisan queue:work command. So php artisan queue:work. And once we started the worker, it scanned the database and found the job that we just

So php artisan queue:work. And once we started the worker, it scanned the database and found the job that we just dispatched and ran it right away. And if we dispatch multiple jobs by recalling our route multiple times, you will see that the worker will start processing these jobs one by one. Perfect. So far, we have learned how to create a job, how to dispatch it to the queue, and how to configure the queue connection. We also learned how to start a worker. In the next episode, we are going to look at the things we can configure the Laravel

We also learned how to start a worker. In the next episode, we are going to look at the things we can configure the Laravel queue system to do for us.

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