From dfb10bc7aa6a1c58af71cfaa765f10caf9074224 Mon Sep 17 00:00:00 2001 From: Victor Saisa Lopez Date: Thu, 30 Aug 2012 15:05:39 -0400 Subject: [PATCH] BUG 9630 "cron.php script can have several instances running..." SOLVED - cron.php script can have several instances running at the same time - Problem solved, added validation in cron.php script to avoid run several times * Available from version 2.0.44 --- workflow/engine/bin/cron.php | 110 ++++++++++++++++------------ workflow/engine/bin/cron_single.php | 21 +----- 2 files changed, 65 insertions(+), 66 deletions(-) diff --git a/workflow/engine/bin/cron.php b/workflow/engine/bin/cron.php index 6e326af4c..1aa1619ea 100755 --- a/workflow/engine/bin/cron.php +++ b/workflow/engine/bin/cron.php @@ -52,59 +52,73 @@ define ('MEMCACHED_ENABLED', $config['memcached']); define ('MEMCACHED_SERVER', $config['memcached_server']); define ('TIME_ZONE', $config['time_zone']); -//default values +//Default values $bCronIsRunning = false; -$sLastExecution = ''; -if ( file_exists(PATH_DATA . 'cron') ) { - $aAux = unserialize( trim( @file_get_contents(PATH_DATA . 'cron')) ); - $bCronIsRunning = (boolean)$aAux['bCronIsRunning']; - $sLastExecution = $aAux['sLastExecution']; -} -else { - //if not exists the file, just create a new one with current date - @file_put_contents(PATH_DATA . 'cron', serialize(array('bCronIsRunning' => '1', 'sLastExecution' => date('Y-m-d H:i:s')))); +$sLastExecution = null; + +if (file_exists(PATH_DATA . "cron")) { + $arrayAux = unserialize(trim(@file_get_contents(PATH_DATA . "cron"))); + $bCronIsRunning = (boolean)($arrayAux["bCronIsRunning"]); + $sLastExecution = $arrayAux["sLastExecution"]; } -$WS = ''; -$argsx = ''; -$sDate = ''; -$dateSystem = date("Y-m-d H:i:s"); +if (!$bCronIsRunning) { + //Start cron + $arrayAux = array("bCronIsRunning" => "1", "sLastExecution" => date("Y-m-d H:i:s")); + @file_put_contents(PATH_DATA . "cron", serialize($arrayAux)); -for ($i = 1; $i <= count($argv) - 1; $i++) { - if( strpos($argv[$i], '+d') !== false){ - $sDate = substr($argv[$i],2); - } else if( strpos($argv[$i], '+w') !== false){ - $WS = substr($argv[$i],2); - } else { - $argsx .= ' '.$argv[$i]; - } -} + //Data + $ws = null; + $argsx = null; + $sDate = null; + $dateSystem = date("Y-m-d H:i:s"); -//if $sDate is not set, so take the system time -if ($sDate != "") { - eprintln("[Applying date filter: $sDate]"); -} else { - $sDate = $dateSystem; -} - - -if( $WS=='' ){ - $oDirectory = dir(PATH_DB); - $cws = 0; - while($sObject = $oDirectory->read()) { - if (($sObject != '.') && ($sObject != '..')) { - if (is_dir(PATH_DB . $sObject)) { - - if (file_exists(PATH_DB . $sObject . PATH_SEP . 'db.php')) { - $cws++; - system("php -f \"".dirname(__FILE__).PATH_SEP."cron_single.php\" $sObject \"$sDate\" \"$dateSystem\" $argsx", $retval); + for ($i = 1; $i <= count($argv) - 1; $i++) { + if (strpos($argv[$i], "+d") !== false) { + $sDate = substr($argv[$i],2); + } else { + if (strpos($argv[$i], "+w") !== false) { + $ws = substr($argv[$i], 2); + } else { + $argsx = $argsx . " " . $argv[$i]; + } } - } } - } -} else { - $cws = 1; - system("php -f \"".dirname(__FILE__).PATH_SEP."cron_single.php\" $WS \"$sDate\" \"$dateSystem\" $argsx", $retval); -} -eprintln("Finished $cws workspaces processed."); + + //If $sDate is not set, so take the system time + if ($sDate != null) { + eprintln("[Applying date filter: $sDate]"); + } else { + $sDate = $dateSystem; + } + + if ($ws == null) { + $oDirectory = dir(PATH_DB); + $cws = 0; + + while($sObject = $oDirectory->read()) { + if (($sObject != ".") && ($sObject != "..")) { + if (is_dir(PATH_DB . $sObject)) { + if (file_exists(PATH_DB . $sObject . PATH_SEP . "db.php")) { + $cws = $cws + 1; + + system("php -f \"".dirname(__FILE__).PATH_SEP."cron_single.php\" $sObject \"$sDate\" \"$dateSystem\" $argsx", $retval); + } + } + } + } + } else { + $cws = 1; + + system("php -f \"".dirname(__FILE__).PATH_SEP."cron_single.php\" $ws \"$sDate\" \"$dateSystem\" $argsx", $retval); + } + + //End cron + $arrayAux = array("bCronIsRunning" => "0", "sLastExecution" => date("Y-m-d H:i:s")); + @file_put_contents(PATH_DATA . "cron", serialize($arrayAux)); + + eprintln("Finished $cws workspaces processed."); +} else { + eprintln("The cron is running, please wait for it to finish.\n- Started in $sLastExecution"); +} diff --git a/workflow/engine/bin/cron_single.php b/workflow/engine/bin/cron_single.php index 2ad5616f8..c97a6af60 100755 --- a/workflow/engine/bin/cron_single.php +++ b/workflow/engine/bin/cron_single.php @@ -97,21 +97,6 @@ require_once ("classes/model/AppEvent.php"); require_once ("classes/model/CaseScheduler.php"); //G::loadClass("pmScript"); -//default values -$bCronIsRunning = false; -$sLastExecution = ''; - -if (file_exists(PATH_DATA . 'cron')) { - $arrayAux = unserialize(trim(@file_get_contents(PATH_DATA . 'cron'))); - $bCronIsRunning = (boolean)($arrayAux['bCronIsRunning']); - $sLastExecution = $arrayAux['sLastExecution']; -} else { - //if not exists the file, just create a new one with current date - $arrayAux = array('bCronIsRunning' => '1', 'sLastExecution' => date('Y-m-d H:i:s')); - - @file_put_contents(PATH_DATA . 'cron', serialize($arrayAux)); -} - if (!defined('SYS_SYS')) { $sObject = $argv[1]; $sNow = $argv[2]; @@ -221,9 +206,9 @@ if (!defined('SYS_SYS')) { processWorkspace(); } -//finally update the file -$arrayAux = array('bCronIsRunning' => '0', 'sLastExecution' => date('Y-m-d H:i:s')); -@file_put_contents(PATH_DATA . 'cron', serialize($arrayAux)); + + + function processWorkspace() {