php Version of cron Timing Task Executor Usage Example
- 2021-07-12 05:20:43
- OfStack
In this paper, the use of php version of cron timer task executor is described, which is a very practical 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 function 1 consists of three parts:
1. Configuration file:
The configuration file is used to return the timed task file to be executed. Pay attention to the use of * under 1. There are two modes, that is,
Y-m-d H: i:
N H: i: Week (1-7 weeks 1-Sunday)
The configuration file croning. php is as follows:
/**
* Task Manager Profile
*
* Y-m-d H:i : Years Month Day Hour Points
* N H:i : Week (1 - 7| Week 1 - Sunday ) Hour Points
*
* 2013-12-25 19:49 : Fixed time, execute only 1 Times
* *-12-25 20:00 : On a certain day of the year A certain hour and a certain minute
* 2013-12-25 *:49 : Every hour of a day 49 Execute by points 1 Times
* *-*-* 20:00 : Every night 8 Point 0 Sub-execution
* *-*-* *:* Executed every minute
* 2 20:01 : Weekly 2 Adj. 20:01 Time is executed 1 Times
*
* * The number indicates any time at the current position. And so on ....
*
* Format:
* array(
* key=>value,
* );
*
* Description:
* key Is the defined execution time, value Is the execution file, which can be an array or a string, when the same as 1 When multiple tasks are performed in time, in order to avoid key Please use the overlay of 1 Dimensional array pattern.
*
*/
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')
);
2. The php file that the server cronjob mainly executes:
The php file mainly processes and analyzes which files can be executed at that time. And write to the execution log file.
<?php
/**
* cron Task system 1 Executed file, 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(array('Common', 'loadClassFile'));
AHA::initConfig(include APP_ROOT . '/_config/inc.php'); // Load configuration file
// When there is no configuration file for execution
if (!file_exists(APP_ROOT . '/_config/croning.php')) {
exit('cron failed,please check the cron config!');
}
$__all = include APP_ROOT . '/_config/croning.php';
// When the data is illegal
if (!$__all || !is_array($__all)) {
exit('cron failed,please check the cron config!');
}
$__echo = true; // Whether to output to screen
$__time_star = microtime(true);
$__now = time();
Common::fileLog(DATA_ROOT . '/log/cron_index.log', ' Execute cron Begin ******************************' . 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_match('@^([\d\*]+) ([\d\*]+):([\d\*]+)$@U', $__key, $match);
} else {// Normal handling
preg_match('@^([\d\*]+)\-([\d\*]+)\-([\d\*]+) ([\d\*]+):([\d\*]+)$@U', $__key, $match);
}
if ($match) {
array_shift($match);
if (__getPreg($match, $__now)) {// Is it a file to execute
$__onFile = array_merge($__onFile, is_array($__value) ? $__value : array($__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 . ' Start of execution ----------' . date('Y-m-d H:i:s') . '-----------', $__echo);
include ONING_ROOT . '/' . $__value;
Common::fileLog(DATA_ROOT . '/log/cron_index.log', $__value . ' End of execution ( Spend time: ' . ((microtime(true) - $__time_star2) * 1000) . 'ms)-------------', $__echo);
}
}
}
Common::fileLog(DATA_ROOT . '/log/cron_index.log', ' Execute cron End (1 Total execution time: ' . ((microtime(true) - $__time_star) * 1000) . 'ms)*************' . date('Y-m-d H:i:s') . '*****************' . "\n\n", $__echo);
/**
* Process the regular results and return whether the file is to be executed at that time
* @param array $match Regular result, array
* @param integer $__now Timestamp at that time
* @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;
}
3. Numerous timing files to execute:
This is really to execute the code: including collection, data collation and analysis, file path to write to the configuration file value to go. For files executed in the same time, remember the 1-dimensional array mode.
Interested friends can debug and run 1 example program in this paper, and I believe there will be great gains.