cron fix, next func for cron, next in cronsaimod

This commit is contained in:
Ulf Gebhardt 2014-10-09 22:05:44 +02:00
parent 1b74566d2b
commit a9cf661f4d
8 changed files with 52 additions and 26 deletions

View File

@ -2,9 +2,9 @@
namespace SYSTEM\CRON;
class cron {
private static function check($cron){
if( !\class_exists($cron) ||
!\is_array($parents = \class_parents($cron)) ||
public static function check($class){
if( !\class_exists($class) ||
!\is_array($parents = \class_parents($class)) ||
!\array_search('SYSTEM\CRON\cronjob', $parents)){
return false;}
return true;}
@ -17,7 +17,7 @@ class cron {
self::status($cron[\SYSTEM\DBD\system_cron::FIELD_CLASS], \SYSTEM\CRON\cronstatus::CRON_STATUS_FAIL_CLASS);
continue;}
//time to execute?
if(!\SYSTEM\CRON\crontime::check_now( time($cron[\SYSTEM\DBD\system_cron::FIELD_LAST_RUN]),
if(!\SYSTEM\CRON\crontime::check_now( strtotime($cron[\SYSTEM\DBD\system_cron::FIELD_LAST_RUN]),
$cron[\SYSTEM\DBD\system_cron::FIELD_MIN],
$cron[\SYSTEM\DBD\system_cron::FIELD_HOUR],
$cron[\SYSTEM\DBD\system_cron::FIELD_DAY],
@ -35,6 +35,19 @@ class cron {
return \SYSTEM\LOG\JsonResult::ok();
}
public static function next($class){
$cron = \SYSTEM\DBD\SYS_CRON_GET::Q1(array($class));
//check module
if(!self::check($cron[\SYSTEM\DBD\system_cron::FIELD_CLASS])){
throw new \SYSTEM\LOG\ERROR("Given class is not a cronjob");}
//time
return \SYSTEM\CRON\crontime::next_now( $cron[\SYSTEM\DBD\system_cron::FIELD_MIN],
$cron[\SYSTEM\DBD\system_cron::FIELD_HOUR],
$cron[\SYSTEM\DBD\system_cron::FIELD_DAY],
$cron[\SYSTEM\DBD\system_cron::FIELD_DAY_WEEK],
$cron[\SYSTEM\DBD\system_cron::FIELD_MONTH]);
}
private static function status($class, $status){
new \SYSTEM\LOG\CRON('Cron Status for Class '.$class.' updated to: '. \SYSTEM\CRON\cronstatus::decode($status));
return \SYSTEM\DBD\SYS_CRON_UPD::QI(array($status,time(),$class));}

View File

@ -1,23 +1,21 @@
<?php
namespace SYSTEM\CRON;
//http://coderzone.org/library/PHP-PHP-Cron-Parser-Class_1084.htm
class crontime {
public static function next($base_time,$min,$hour,$day,$day_week,$month){
list( $now_min, $now_hour, $now_day, $now_month, $now_day_week ) = preg_split( "/ /", date("i H d n N", $base_time ) );
list( $next_min, $next_hour, $next_day, $next_month, $next_year) = preg_split( "/ /", date("i H d n Y", $base_time ) );
if($month){
if($month < $now_month){ $next_year += 1;}
$next_month = $month;}
if(($month + $now_month)> 12){ $next_year += 1;}
$next_month = ($now_month+$month)%12;}
if($day){
if($day < $now_day){ $next_month += 1;}
$next_day = $day;}
if($day + $now_day> 31){ $next_month += 1;}
$next_day = ($now_day+$day)%31;}
if($hour){
if($hour < $now_hour){ $next_day += 1;}
$next_hour = $hour;}
if(($hour + $now_hour)> 24){ $next_day += 1;}
$next_hour = ($now_hour+$hour)%24;}
if($min){
if($min < $now_min){ $next_hour += 1;}
$next_min = $min;}
if(($min + $now_min)> 60){ $next_hour += 1;}
$next_min = ($now_min+$min)%60;}
if($day_week){
$day_week = $day_week % 6; // 7 and 0 both mean Sunday
$now_day_week = $now_day_week % 6; // 7 and 0 both mean Sunday
@ -28,17 +26,17 @@ class crontime {
list( $now_min, $now_hour, $now_day, $now_month, $now_day_week ) = preg_split( "/ /", date("i H d n N", $base_time ) );
list( $last_min, $last_hour, $last_day, $last_month, $last_year) = preg_split( "/ /", date("i H d n Y", $base_time ) );
if($month){
if($month > $now_month){ $last_year -= 1;}
$last_month = $month;}
if(($now_month - $month)< 12){ $last_year -= 1;}
$last_month = ($now_month-$month)%12;}
if($day){
if($day > $now_day){ $last_month -= 1;}
$last_day = $day;}
if(($now_day - $day)> 31){ $last_month -= 1;}
$last_day = ($now_day-$day)%31;}
if($hour){
if($hour > $now_hour){ $last_day -= 1;}
$last_hour = $hour;}
if(($now_hour - $hour)> 24){ $last_day -= 1;}
$last_hour = ($now_hour-$hour)%24;}
if($min){
if($min > $now_min){ $last_hour -= 1;}
$last_min = $min;}
if(($now_min - $min)> 60){ $last_hour -= 1;}
$last_min = ($now_min-$min)%60;}
if($day_week){
$day_week = $day_week % 6; // 7 and 0 both mean Sunday
$now_day_week = $now_day_week % 6; // 7 and 0 both mean Sunday
@ -46,9 +44,10 @@ class crontime {
return mktime($last_hour, $last_min, 0, $last_month, $last_day, $last_year);
}
public static function check($base_time,$last_run,$min,$hour,$day,$day_week,$month){
return self::next($last_run, $min, $hour, $day, $day_week, $month) < $base_time ? true : false;}
//new \SYSTEM\LOG\INFO('next:'.self::next($last_run, $min, $hour, $day, $day_week, $month).' bt:'.$base_time.' last:'.$last_run.' dif:'.(self::next($last_run, $min, $hour, $day, $day_week, $month) - time($base_time)).' run:'.(self::next($last_run, $min, $hour, $day, $day_week, $month) <= time($base_time) ? 'run' : 'not run'));
return self::next($last_run, $min, $hour, $day, $day_week, $month) <= $base_time ? true : false;}
public static function next_now($min,$hour,$day,$day_week,$month){
self::next(time(),$min,$hour,$day,$day_week,$month);}
return self::next(time(),$min,$hour,$day,$day_week,$month);}
public static function last_now($min,$hour,$day,$day_week,$month){
return self::last(time(),$min,$hour,$day,$day_week,$month);}
public static function check_now($last_run,$min,$hour,$day,$day_week,$month){

11
dbd/qq/SYS_CRON_GET.php Normal file
View File

@ -0,0 +1,11 @@
<?php
namespace SYSTEM\DBD;
class SYS_CRON_GET extends \SYSTEM\DB\QP {
protected static function query(){
return new \SYSTEM\DB\QQuery(get_class(),
//pg
'SELECT * FROM '.\SYSTEM\DBD\system_cron::NAME_PG.' WHERE class = ?;',
//mys
'SELECT * FROM '.\SYSTEM\DBD\system_cron::NAME_MYS.' WHERE class = ?;'
);}}

View File

@ -2,4 +2,4 @@
\SYSTEM\autoload::registerFolder(dirname(__FILE__),'SYSTEM\SAI');
SYSTEM\autoload::registerFolder(dirname(__FILE__).'/sai','SYSTEM\SAI');
SYSTEM\autoload::registerFolder(dirname(__FILE__).'/page','SYSTEM\SAI');
require_once dirname(__FILE__).'/modules/autoload_modules.php';
require_once dirname(__FILE__).'/modules/autoload.inc';

View File

@ -5,8 +5,9 @@ class saimod_sys_cron extends \SYSTEM\SAI\SaiModule {
public static function sai_mod__SYSTEM_SAI_saimod_sys_cron(){
$vars = array();
$vars['content'] = '';
$res = \SYSTEM\DBD\SYS_SAIMOD_CRON::QQ();
$res = \SYSTEM\DBD\SYS_SAIMOD_CRON::QQ();
while($r = $res->next()){
$r['next'] = date('Y-m-d H:i:s',\SYSTEM\CRON\cron::next($r['class']));
$vars['content'] .= \SYSTEM\PAGE\replace::replaceFile(\SYSTEM\SERVERPATH(new \SYSTEM\PSAI(),'modules/saimod_sys_cron/tpl/list_entry.tpl'), $r);}
return \SYSTEM\PAGE\replace::replaceFile(\SYSTEM\SERVERPATH(new \SYSTEM\PSAI(),'modules/saimod_sys_cron/tpl/tabs.tpl'), $vars);
}

View File

@ -6,5 +6,6 @@
<td>${day_week}</td>
<td>${month}</td>
<td>${last_run}</td>
<td>${next}</td>
<td>${status}</td>
</tr>

View File

@ -11,6 +11,7 @@
<th>day_week</th>
<th>month</th>
<th>last_run</th>
<th>next_run</th>
<th>status</th>
</tr>
${content}