Skip to content

Commit c2ec86e

Browse files
committed
#1.13.5 Improved crontab, prevent tasks overlapping if the task runs for longer than cron interval
1 parent 653ea8c commit c2ec86e

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

classes/core_cron.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
22
class Core_Cron{
33

4-
public static function update_interval($code)
4+
public static function update_interval($code, $now=null)
55
{
66
$bind = array(
77
'record_code' => $code,
8-
'now' => Phpr_DateTime::now()->toSqlDateTime()
8+
'now' => $now ? $now : Phpr_DateTime::now()->toSqlDateTime()
99
);
1010
Db_DbHelper::query('insert into core_cron_table (record_code, updated_at) values (:record_code, :now) on duplicate key update updated_at =:now', $bind);
1111
}
@@ -53,7 +53,7 @@ public static function queue_job($handler_name, $param_data=array())
5353
Db_DbHelper::query('insert into core_cron_jobs (handler_name, param_data, created_at) values (:handler_name, :param_data, :now)', $bind);
5454
}
5555

56-
private static function execute_cronjobs()
56+
public static function execute_cronjobs()
5757
{
5858
// Worker can perform only 5 jobs per run
5959
//
@@ -79,7 +79,7 @@ private static function execute_cronjobs()
7979
}
8080
}
8181

82-
private static function execute_crontabs()
82+
public static function execute_crontabs()
8383
{
8484

8585
$modules = Core_ModuleManager::listModules();
@@ -101,19 +101,23 @@ private static function execute_crontabs()
101101
continue;
102102

103103

104+
$now = Phpr_DateTime::now();
104105
$last_exec = Phpr_DateTime::parse(self::get_interval($code), Phpr_DateTime::universalDateTimeFormat);
105106
$next_exec = $last_exec->addMinutes($options['interval']);
106-
$can_execute = Phpr_DateTime::now()->compare($next_exec);
107+
$can_execute = $now->compare($next_exec);
107108

108109
if ($can_execute == -1)
109110
continue;
110111

111-
112112
try
113113
{
114+
self::update_interval( $code );//set last run to now to help prevent repeat triggers on long tasks
114115
$method = $options['method'];
115-
if ($module->$method())
116-
self::update_interval( $code );
116+
if ($module->$method()){
117+
self::update_interval( $code ); //time of completion
118+
} else {
119+
self::update_interval( $code, $last_exec ); //not run, revert to last completed time
120+
}
117121

118122
}
119123
catch (Exception $ex)

cron.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Execute cron as a standalone
3+
* Execute all cron tasks as a standalone
44
*
55
* Example usage:
66
* /usr/local/bin/php -q /home/YOUR_USERNAME/public_html/modules/core/cron.php

updates/version.dat

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,4 +465,5 @@
465465
#1.13.1 Bug Fix: sql now() being compared to localised PHP datetime causing cron timing issues.
466466
#1.13.2|@1_13_2_framework_update Security fix
467467
#1.13.3|@1_13_3_framework_update Evocode Security Patch. Must read http://evocode.com/blog/lemonstand-v1-vulnerability/
468-
#1.13.4 Bug Fix: CURLOPT_HTTPHEADER set with underscores caused issue on some stacks
468+
#1.13.4 Bug Fix: CURLOPT_HTTPHEADER set with underscores caused issue on some stacks
469+
#1.13.5 Improved crontab, prevent tasks overlapping if the task runs for longer than cron interval

0 commit comments

Comments
 (0)