Detailed implementation method of php scheduled task

  • 2020-06-07 04:04:37
  • OfStack

As I analyzed in uchome, uchome does this
Store all scheduled tasks in the database
2. Each time the user performs an operation or opens a page, one scheduled task is executed according to the order, that is, round judgment, when the time is up, it is executed in the user process.
uchome's scheduled task code is in./source/ function_cron.php
Above only analysis uchome code about the planning task of the approximate results, hopefully someone has a good way to share.
ignore_user_abort()
I thoroughly studied planning tasks a while ago, and I think there are many ideas for planning tasks. However, the most suitable one for Web is the trigger type, which is similar to DZ and PHPWind. It seems that most web applications do the same
1. In order to provide stable triggering, crontab+wget or ab are used to provide regular access
2, nohup + php write the daemon
3. Write an infinite loop directly with php and provide trigger, in which case you need to use cache or database to help push out the loop, and you must use sleep or usleep to control the trigger frequency
4, crontab + php
5. web trigger mode is also different. One is timed trigger and the other is periodic trigger
The concrete realization train of thought is easy to think, do not say much
Your own host must use crontab to execute the command line PHP
Nothing can only be written in code
crontab is better, and if you use a loop, you'd better keep the loop and the handler separate as well, otherwise memory will increase by 1.

Today I accidentally found this function in the php manual -ignore_user_abort. This function will help us to implement planning tasks like cron1 in linux. Let's see how to do this.
First take a look at the php manual's explanation of this function
Description

int ignore_user_abort ([ bool $setting ] )
Sets whether a client disconnect should cause a script to be aborted.

This means that the following program executes whether or not the client closes the browser.
Let's look at the parameters
Parameters

setting
If not set, the function will only return the current setting.

This function takes one argument to decide whether to enable ignore_user_abort.
Then take a look at its return value:

Return Values
Returns the previous setting, as a boolean.

It is said that the previous setting is returned, and it is worth bool, after my test, this statement is not true, what is returned is obviously int type, if you do not believe, you can write an php file to test.
With all that said, how do you actually use the php function for planning tasks? Another function, set_time_limit, can set the running time of the program to unlimited through set_time_limit0), the default running time of php is 30 seconds, through set_time_limit(0) can make the program unlimited execution. Add ignore_user_abort (1) and set_time_limit(0) before the program executes. Let me give you an example.


<?php
ignore_user_abort(); // run script in background
set_time_limit(0); // run script forever
$interval=30; // do every 15 minutes ... 
do{
$fp = fopen( ' text3.txt','a');
fwrite($fp,'test');
fclose($fp);
sleep($interval); // wait 15 minutes
}while(true);
?>


Related articles: