This example describes the PHP version of the Cron Timer task executor usage, which is a very useful functional application. The specific methods are as follows:
Since the server crontab can only be accurate to minutes, the starting point of the program is also minutes.
This feature consists of three parts altogether:
First, the configuration file:
The configuration file is used to return the scheduled task file to be executed, note the use of the * is OK, there are two modes, is
Y-m-d h:i: Time and date
N h:i: Week (1-7| Monday-Sunday) cent
The configuration file croning.php is as follows:
/**
* Task Manager Profile
*
* y-m-d h:i: Date and Time
* N h:i : Week (1-7| Monday-Sunday)
*
* 2013-12-25 19: 49: Fixed time, only once
* *-12-25 20:00: A certain hour of the year a certain point
* 2013-12-25 *:49:49 points per hour of the day are executed once
* *-*-* 20:00
: Every night 8:0 *
*-*-* *:* : Every minute is executing
* 2 20:01 : Every Tuesday 20:01 time to perform a * * *
*
number indicates the current position any time.
and so on ..... * *
Format:
* Array (
* key=>value,
*);
*
* NOTE:
* key is the definition of the execution time, value is the execution of the file, can be an array or string, when there are multiple tasks at the same time execution, in order to avoid the overwrite key, please use one-dimensional array mode.
*
*
/return Array (
' 2013-12-25 19:49 ' => ' 123.php ',
' 2013-12-* 18:00 ' => ' 456.php ',
' 1 08:00 ' => ' 6546.php ',
' *-12-25 19:49 ' =>array (' 444.php ', ' 456.php ')
;
Second, the server cronjob the main implementation of PHP files:
The PHP file mainly deals with and analyzes which files were executed at that time. and write to the execution log file.
<?php/** * Cron task Unified Execution of files, no timeout/header (' content-type:text/html; Charset=utf-8 ');
Set_time_limit (0);
Define (' App_root ', DirName (__file__));
Define (' Aha_root ', DirName (app_root)); Define (' Core_root ', aha_root.
'/__core '); Define (' Data_root ', aha_root.
'/data '); Define (' Model_root ', app_root.
'/model '); Define (' Oning_root ', app_root. '/oning '); Timed execution file directory require core_root.
'/common.php '; Require Core_root. '/aha.php ';
Load the framework core file Spl_autoload_register (' Common ', ' loadclassfile ')); Aha::initconfig (include App_root. '/_config/inc.php '); The IF (!file_exists (app_root) is loaded when the configuration file//does not exist for the executed profile.
'/_config/croning.php ') {exit (' cron failed,please check the cron config! ');} $__all = include app_root.
'/_config/croning.php ';
If data is not valid if (!$__all | |!is_array ($__all)) {exit (' cron failed,please check the cron config! ');} $__echo = true;
Output to screen $__time_star = Microtime (true);
$__now = time (); Common::filelog (Data_root. '/log/cron_index.log ', ' execute cron start ******************************' . Date (' y-m-d h:i:s ', $__now).
' ****************************** ', $__echo);
$__onfile = Array (); if ($__all) {foreach ($__all as $__key => $__value) {if Strpos ($__key, '-') = = False) {//weekly processing Preg_mat
CH (' @^ ([\d\*]+) ([\d\*]+):([\d\*]+) $@u ', $__key, $match);
else {//normal processing Preg_match (' @^ ([\d\*]+) \-([\d\*]+) \-([\d\*]+) ([\d\*]+):([\d\*]+) $@u ', $__key, $match);
} if ($match) {array_shift ($match); if (__getpreg ($match, $__now)) {//is the file to be executed $__onfile = Array_merge ($__onfile, Is_array ($__value)? $__value:arr
Ay ($__value));
}} if ($__onfile) {$__onfile = Array_unique ($__onfile); foreach ($__onfile as $__value) {if (file_exists) (Oning_root. '/' .
$__value)) {$__time_star2 = Microtime (true); Common::filelog (Data_root. '/log/cron_index.log ', $__value. ' Execution started----------'. Date (' y-m-d h:i:s ').
'-----------', $__echo); Include Oning_root. '/' .
$__value; Common::filelOG (data_root. '/log/cron_index.log ', $__value. ' End of execution (take time: '. ((Microtime (True)-$__time_star2) * 1000).
' Ms '-------------', $__echo); }} common::filelog (Data_root. '/log/cron_index.log ', ' Execute cron End (total execution time: '. ((Microtime (True)-$__time_star) * 1000). ' Ms ' ************* '. Date (' y-m-d h:i:s '). '*****************' .
"\ n", $__echo); /** * handles the regular result and returns whether the file was $match regular result of the * @param array, the array * @param integer $__now the time stamp * @return bool/function
__getpreg ($match, $__now) {$back = false;
List ($__y, $__m, $__d, $__n, $__h, $__i) = Explode ('-', date (' Y-m-d-n-h-i ', $__now));
$ARGC = count ($match);
if ($ARGC = = 3) {$ARGC = $match [0] = = ' * '? $__n: $match [0];
$ARGC. = '; $ARGC. = $match [1] = = ' * '?
$__h: $match [1];
$ARGC. = ': '; $ARGC. = $match [2] = = ' * '?
$__i: $match [2]; $back = Date (' N h:i ', $__now) = = Date ($ARGC, $__now)?
True:false;
} elseif ($ARGC = = 5) {$ARGC = $match [0] = = ' * '? $__y: $match [0];
$ARGC. = '-'; $ARGC. = $match [1] = = ' * '?
$__m: $match [1];
$ARGC. = '-'; $ARGC. = $match [2] = = ' * '?
$__d: $match [2];
$ARGC. = '; $ARGC. = $match [3] = = ' * '?
$__h: $match [3];
$ARGC. = ': '; $ARGC. = $match [4] = = ' * '?
$__i: $match [4]; $back = Date (' y-m-d h:i ', $__now) = = Date ($ARGC, $__now)?
True:false;
return $back;
}
Third, a large number of scheduled documents to be executed:
This is the real code to execute: including collection, data collation and analysis, and file paths written to the value of the configuration file. Files executed at the same time, remember the one-dimensional array pattern.
Interested friends can be debugged to run the example program, I believe there will be a great harvest.