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

Introducing Laravel Scheduler0:00

Hey there, now that we have the web component of our Laravel application running and communicating with the database, it's time we look into another important entry point, which is the scheduler component. This component exposes an artisan command that needs to be executed every minute, and when it runs, it executes any scheduled commands registered in the Laravel application console kernel. To ensure that the artisan command is invoked every minute, we will rely on the cron daemon, which comes by default in every Ubuntu installation. I'm sure it's on your server whether or not you're using AWS. So let's look into it.

Reviewing System Crontab0:42

I'm sure it's on your server whether or not you're using AWS. So let's look into it. At every Ubuntu machine, there is a system-wide cron configuration file located at /etc/crontab. Let's view this file. So nano /etc/crontab. The first directive we have here is for the shell cron will use to execute commands. It's set to the original sh shell at /bin/sh. Then we have four cron jobs defined by default. For each job, there is a cron expression, which defines when this job is supposed to run.

Creating User Crontab2:46

we isolate their cron configurations as well. So for each Laravel application, we are going to use a separate cron configuration file. And to do that, we will use the crontab command to edit the system user-specific cron file. So let's do it. And before we do anything, let's switch to the laracast's user, sudo su laracast. Then we run crontab -e to edit the user-specific cron file. The operating system tells us that there is no crontab file for the laracast's user and asks which editor we want to use for editing this file. I'll use the default nano editor and hit enter. Now let's scroll all the way down and add our cron job.

Adding Schedule Cron Job3:29

I'll use the default nano editor and hit enter. Now let's scroll all the way down and add our cron job. Since we want to run this job every minute, we will use * for all parts of the cron expression. So every minute, of every hour, of every day, of every month, on every day of the week. Then we start typing our command. We need to run php artisan schedule:run, yes. So we start with php8.1, which points to the CLI binary of php8.1. Then add the path to the artisan file, artisan. And then add schedule:run.

Then add the path to the artisan file, artisan. And then add schedule:run. This command will run the schedule:run artisan command using the php 8.1 executable which is on our server already. That way, if we have multiple php versions installed on the server, cron knows that we won't use php 8.1 for this particular cron job. Now to be able to read the output of this command, we need to append it to a log file. So let's append to /home/laracasts/www/storage/logs/cron.log. Cron will create this cron.log file in the same directory where our Laravel logs live. That makes it easier for us to inspect all the log files in one place.

Cron will create this cron.log file in the same directory where our laravel logs live. That makes it easier for us to inspect all the log files in one place. And finally, let's redirect standard error to standard out. So errors are also sent to the cron.log file. So we redirect standard error represented by two to the file descriptor one, which is standard out. And that's it. Let's save the file. And crontab tells us that it's installing our new crontab job. Let's go check our logs directory, cd home/laracasts/www/storage/logs, list the files.

Verifying Cron Log Output5:42

And crontab tells us that it's installing our new crontab job. Let's go check our logs directory, cd home/laracasts/www/storage/logs, list the files and our log file wasn't created yet. That means the cron job hasn't run yet. Let's wait a little bit, give it some time. And then let's list the files again. And here we go. Let's check the contents of the file. And we can see that the command was executed and Laravel printed no scheduled commands are ready to run.

Avoiding Thundering Herd6:18

And we can see that the command was executed and Laravel printed no scheduled commands are ready to run. That's because our Laravel application doesn't have any commands registered in the Kernel. But if there were any commands, they will be executed when the cron job runs. So with this setup, each Laravel application on the server will run the schedule:run command every minute and log the output to its own log file. There is one thing that you need to be aware of, though, if you have multiple applications that need to run multiple scheduled jobs at the same time, your server will be overwhelmed with work to do at the beginning of each minute, you will have a thundering herd of tasks fighting over the available CPU and memory.

with work to do at the beginning of each minute, you will have a thundering heart of tasks fighting over the available CPU and memory. And that's not a good thing. And to avoid that, it's recommended that you dispatch those scheduled jobs to the queue to be executed later via queue workers, they will be dispatched on the queue when the cron is invoked, or when the schedule:run command is run by crontab. But the actual execution will happen later when your queue workers have the capacity. You can read more about dispatching scheduled jobs to the queue in the Laravel documentation. And I think it's a pretty neat feature of the framework because it avoids a huge problem that otherwise would be very complicated to fix.

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