This article brings you relevant knowledge about laravel, which mainly introduces the usage and principles of scheduled tasks. It also explains the related issues of scheduled tasks according to the application scenarios. Below Let's take a look, I hope it will be helpful to everyone.
[Related recommendations: laravel video tutorial]
A website system often has many Scheduled tasks need to be executed. For example, push subscription messages, statistics related data, etc. Linux generally uses crontab to set up and manage scheduled tasks. However, as the number of tasks increases, managing scheduled tasks becomes more troublesome and prone to management confusion. Laravel's solution to this problem is to set up only one scheduled task. All scheduled tasks in the business are processed and judged on this scheduled task, realizing the management of scheduled tasks at the code level.
First configure crontab:
1 |
|
The above means to set the scheduled task to be executed every minute. The specific business configuration is placed in App\Console\Kernel In the schedule method:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Basic principle:
schedule:run This specification is in the vendor\illuminate\console\Scheduling\ScheduleRunCommand class It is defined inside, and the definition form is the same as the general scheduled task:
1 2 3 4 5 |
|
When laravel parses the command, the ScheduleRunCommand class is merged with the commands array in the Kernel class:
1 2 3 4 5 6 7 8 9 10 11 |
|
So the php artisan schedule:run command is a built-in command of the framework.
When the command is started, it will find the handle method in the class by default for execution:
1 2 3 4 5 6 7 8 |
|
php The artisan schedule:run command will scan all instructions registered in Kernel::schedule every minute and determine the Whether the instruction has reached the execution cycle, if so, push it into the queue to be executed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
ScheduleRunCommand::handle function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Avoid task overlap:
Sometimes the execution time of a single scheduled task is too long. After the next execution time, the last execution task has not been completed. At this time, we can use the withoutOverlapping() method to avoid task overlap. In the withoutOverlapping method, lock the corresponding task (the same is true for the onOneServer method):
1 2 3 4 |
|
Only when the corresponding task lock is obtained can the task be executed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
The task runs in the background :
Since scheduled tasks are executed in sequence, the execution time of the previous task is too long, which will affect the execution time of the next task. Therefore, we can use the runInBackground method to execute the task in the background, which is somewhat similar to a shell. The role of &:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
In addition to the above method, we can also use laravel's scheduled tasks to call Shell commands:
1 |
|
You can also use closure Package scheduling:
1 2 |
|
If you want to know more about how to use it, you can check laravel’s documentation:
https://laravelacademy.org/post/19517.html
[Related recommendations: laravel video tutorial]
The above is the detailed content of Detailed explanation of laravel scheduled task usage and principles. For more information, please follow other related articles on the PHP Chinese website!