PM-4147 "Problemas con cron" SOLVED

Issue:
    Problemas con cron
Cause:
    Problemas con cron
Solution:
    Se actualizo el codigo de los crones: cron, ldapcron, messageeventcron, timereventcron
    por una version mas actual
This commit is contained in:
Victor Saisa Lopez
2015-11-25 11:54:52 -04:00
parent 3f45e0df02
commit 322f1729ed
8 changed files with 456 additions and 2051 deletions

View File

@@ -1,177 +1,229 @@
<?php
/**
* cron.php
* @package workflow-engine-bin
*/
try {
//Set variables
$cronName = pathinfo($_SERVER['SCRIPT_FILENAME'], PATHINFO_FILENAME);
$osIsLinux = strtoupper(substr(PHP_OS, 0, 3)) != 'WIN';
if ( !defined('PATH_SEP') ) {
define("PATH_SEP", (substr(PHP_OS, 0, 3) == "WIN")? "\\" : "/");
}
$arrayCronConfig = [
'cron' => ['title' => 'CRON'],
'ldapcron' => ['title' => 'LDAP Advanced CRON'],
'messageeventcron' => ['title' => 'Message-Event CRON'],
'timereventcron' => ['title' => 'Timer-Event CRON']
];
$docuroot = explode(PATH_SEP, str_replace('engine' . PATH_SEP . 'methods' . PATH_SEP . 'services', '', dirname(__FILE__)));
array_pop($docuroot);
array_pop($docuroot);
$pathhome = implode(PATH_SEP, $docuroot) . PATH_SEP;
//try to find automatically the trunk directory where are placed the RBAC and Gulliver directories
//in a normal installation you don't need to change it.
array_pop($docuroot);
$pathTrunk = implode(PATH_SEP, $docuroot) . PATH_SEP ;
array_pop($docuroot);
$pathOutTrunk = implode( PATH_SEP, $docuroot) . PATH_SEP ;
// to do: check previous algorith for Windows $pathTrunk = "c:/home/";
define('PATH_HOME', $pathhome);
define('PATH_TRUNK', $pathTrunk);
define('PATH_OUTTRUNK', $pathOutTrunk);
//Define constants
define('PATH_SEP', ($osIsLinux)? '/' : '\\');
require_once PATH_TRUNK . "framework/src/Maveriks/Util/ClassLoader.php";
require_once (PATH_HOME . 'engine' . PATH_SEP . 'config' . PATH_SEP . 'paths.php');
require_once PATH_CORE . 'classes' . PATH_SEP . 'class.system.php';
$arrayPathToCron = [];
$flagPathToCron = false;
$config = System::getSystemConfiguration();
//Path to CRON by $_SERVER['SCRIPT_FILENAME']
$arrayAux = explode(PATH_SEP, str_replace('engine' . PATH_SEP . 'bin', '', realpath($_SERVER['SCRIPT_FILENAME'])));
$e_all = defined('E_DEPRECATED') ? E_ALL & ~E_DEPRECATED : E_ALL;
$e_all = defined('E_STRICT') ? $e_all & ~E_STRICT : $e_all;
$e_all = $config['debug'] ? $e_all : $e_all & ~E_NOTICE;
array_pop($arrayAux);
array_pop($arrayAux);
G::LoadSystem('inputfilter');
$filter = new InputFilter();
$config['debug'] = $filter->validateInput($config['debug']);
$config['memory_limit'] = $filter->validateInput($config['memory_limit']);
$config['wsdl_cache'] = $filter->validateInput($config['wsdl_cache'],'int');
$config['time_zone'] = $filter->validateInput($config['time_zone']);
// Do not change any of these settings directly, use env.ini instead
ini_set('display_errors', $config['debug']);
ini_set('error_reporting', $e_all);
ini_set('short_open_tag', 'On');
ini_set('default_charset', "UTF-8");
ini_set('memory_limit', $config['memory_limit']);
ini_set('soap.wsdl_cache_enabled', $config['wsdl_cache']);
ini_set('date.timezone', $config['time_zone']);
define ('DEBUG_SQL_LOG', $config['debug_sql']);
define ('DEBUG_TIME_LOG', $config['debug_time']);
define ('DEBUG_CALENDAR_LOG', $config['debug_calendar']);
define ('MEMCACHED_ENABLED', $config['memcached']);
define ('MEMCACHED_SERVER', $config['memcached_server']);
define ('TIME_ZONE', $config['time_zone']);
//Cron status
$bCronIsRunning = false;
$sLastExecution = null;
$processcTimeProcess = 0;
$processcTimeStart = 0;
$force = false;
$osIsLinux = strtoupper(substr(PHP_OS, 0, 3)) != "WIN";
for ($i = 1; $i <= count($argv) - 1; $i++) {
if (strpos($argv[$i], "+force") !== false) {
$force = true;
unset($argv[$i]);
break;
if (!empty($arrayAux) && $arrayAux[count($arrayAux) - 1] == 'workflow') {
$arrayPathToCron = $arrayAux;
$flagPathToCron = true;
}
}
if (!$force && file_exists(PATH_DATA . "cron")) {
//Windows flag
//Get data of cron file
$arrayCron = unserialize(trim(file_get_contents(PATH_DATA . "cron")));
$bCronIsRunning = (boolean)($arrayCron["bCronIsRunning"]);
$sLastExecution = $arrayCron["sLastExecution"];
$processcTimeProcess = (isset($arrayCron["processcTimeProcess"]))? (int)($arrayCron["processcTimeProcess"]) : 10; //Minutes
$processcTimeStart = (isset($arrayCron["processcTimeStart"]))? $arrayCron["processcTimeStart"] : 0;
}
if (!$force && $osIsLinux) {
//Linux flag
//Check if cron it's running
exec("ps -fea | grep cron.php | grep -v grep", $arrayOutput);
if (count($arrayOutput) > 1) {
$bCronIsRunning = true;
if (!$flagPathToCron) {
throw new Exception('Error: Unable to execute the ' . $arrayCronConfig[$cronName]['title'] . ', the path is incorrect');
}
}
//if (!$force && $bCronIsRunning && $processcTimeStart != 0) {
// if ((time() - $processcTimeStart) > ($processcTimeProcess * 60)) {
// //Cron finished his execution for some reason
// $bCronIsRunning = false;
// }
//}
$pathHome = implode(PATH_SEP, $arrayPathToCron) . PATH_SEP;
if ($force || !$bCronIsRunning) {
//Start cron
$arrayCron = array("bCronIsRunning" => "1", "sLastExecution" => date("Y-m-d H:i:s"));
@file_put_contents(PATH_DATA . "cron", serialize($arrayCron));
array_pop($arrayPathToCron);
try {
//Data
$ws = null;
$argsx = null;
$sDate = null;
$dateSystem = date("Y-m-d H:i:s");
$pathTrunk = implode(PATH_SEP, $arrayPathToCron) . PATH_SEP;
for ($i = 1; $i <= count($argv) - 1; $i++) {
if (!isset($argv[$i])) {
continue;
}
array_pop($arrayPathToCron);
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];
$pathOutTrunk = implode(PATH_SEP, $arrayPathToCron) . PATH_SEP;
define('PATH_HOME', $pathHome);
define('PATH_TRUNK', $pathTrunk);
define('PATH_OUTTRUNK', $pathOutTrunk);
//Check deprecated files
switch ($cronName) {
case 'ldapcron':
$fileBinDeprecated = PATH_HOME . 'engine' . PATH_SEP . 'bin' . PATH_SEP . 'plugins' . PATH_SEP . 'ldapadvanced.php';
if (file_exists($fileBinDeprecated)) {
@unlink($fileBinDeprecated);
if (file_exists($fileBinDeprecated)) {
throw new Exception('Error: ' . $arrayCronConfig[$cronName]['title'] . ' requires that the "' . $fileBinDeprecated . '" file has been deleted.');
}
}
break;
}
//Include files
require_once(PATH_HOME . 'engine' . PATH_SEP . 'config' . PATH_SEP . 'paths.php');
require_once(PATH_TRUNK . 'framework' . PATH_SEP . 'src' . PATH_SEP . 'Maveriks' . PATH_SEP . 'Util' . PATH_SEP . 'ClassLoader.php');
//Class Loader - /ProcessMaker/BusinessModel
$classLoader = \Maveriks\Util\ClassLoader::getInstance();
$classLoader->add(PATH_TRUNK . 'framework' . PATH_SEP . 'src' . PATH_SEP, 'Maveriks');
$classLoader->add(PATH_TRUNK . 'workflow' . PATH_SEP . 'engine' . PATH_SEP . 'src' . PATH_SEP, 'ProcessMaker');
$classLoader->add(PATH_TRUNK . 'workflow' . PATH_SEP . 'engine' . PATH_SEP . 'src' . PATH_SEP);
//Load classes
G::LoadClass('system');
$arraySystemConfiguration = System::getSystemConfiguration();
$e_all = (defined('E_DEPRECATED'))? E_ALL & ~E_DEPRECATED : E_ALL;
$e_all = (defined('E_STRICT'))? $e_all & ~E_STRICT : $e_all;
$e_all = ($arraySystemConfiguration['debug'])? $e_all : $e_all & ~E_NOTICE;
//Do not change any of these settings directly, use env.ini instead
ini_set('display_errors', $arraySystemConfiguration['debug']);
ini_set('error_reporting', $e_all);
ini_set('short_open_tag', 'On');
ini_set('default_charset', 'UTF-8');
ini_set('memory_limit', $arraySystemConfiguration['memory_limit']);
ini_set('soap.wsdl_cache_enabled', $arraySystemConfiguration['wsdl_cache']);
ini_set('date.timezone', $arraySystemConfiguration['time_zone']);
define('DEBUG_SQL_LOG', $arraySystemConfiguration['debug_sql']);
define('DEBUG_TIME_LOG', $arraySystemConfiguration['debug_time']);
define('DEBUG_CALENDAR_LOG', $arraySystemConfiguration['debug_calendar']);
define('MEMCACHED_ENABLED', $arraySystemConfiguration['memcached']);
define('MEMCACHED_SERVER', $arraySystemConfiguration['memcached_server']);
define('TIME_ZONE', ini_get('date.timezone'));
//CRON command options
$arrayCommandOption = [
'force' => '+force'
];
//CRON status
$flagIsRunning = false;
$lastExecution = '';
$force = false;
if (in_array($arrayCommandOption['force'], $argv)) {
unset($argv[array_search($arrayCommandOption['force'], $argv)]);
$force = true;
}
if (!$force && file_exists(PATH_DATA . $cronName)) {
//Windows flag
//Get data of CRON file
$arrayCron = unserialize(trim(file_get_contents(PATH_DATA . $cronName)));
$flagIsRunning = (bool)((isset($arrayCron['flagIsRunning']))? $arrayCron['flagIsRunning'] : $arrayCron['bCronIsRunning']);
$lastExecution = (isset($arrayCron['lastExecution']))? $arrayCron['lastExecution'] : $arrayCron['sLastExecution'];
}
if (!$force && $osIsLinux) {
//Linux flag
//Check if CRON it's running
exec('ps -fea | grep ' . $cronName . '.php | grep -v grep', $arrayOutput);
$counter = 0;
foreach ($arrayOutput as $value) {
if (preg_match('/^.*\s' . $cronName . '\.php.*$/', $value) ||
preg_match('/^.*\s.+(?:\x2F|\x5C)' . $cronName . '\.php.*$/', $value)
) {
$counter++;
}
}
//If $sDate is not set, so take the system time
if (!empty($sDate) && preg_match("/^[1-9]\d{3}\-(?:0[1-9]|1[0-2])\-(?:0[1-9]|[12][0-9]|3[01])(?:\s(?:[0-1]\d|2[0-3])\:[0-5]\d\:[0-5]\d)?$/", $sDate)) {
eprintln("[Applying date filter: $sDate]");
} else {
$sDate = $dateSystem;
if ($counter > 1) {
$flagIsRunning = true;
}
}
if ($ws == null) {
$oDirectory = dir(PATH_DB);
$cws = 0;
if ($force || !$flagIsRunning) {
//Start CRON
$arrayCron = ['flagIsRunning' => '1', 'lastExecution' => date('Y-m-d H:i:s')];
file_put_contents(PATH_DATA . $cronName, serialize($arrayCron));
while (($sObject = $oDirectory->read()) !== false) {
if (($sObject != ".") && ($sObject != "..")) {
if (is_dir(PATH_DB . $sObject)) {
if (file_exists(PATH_DB . $sObject . PATH_SEP . "db.php")) {
$cws = $cws + 1;
try {
$cronSinglePath = PATH_CORE . 'bin' . PATH_SEP . 'cron_single.php';
system("php -f \"" . dirname(__FILE__) . PATH_SEP . "cron_single.php\" $sObject \"$sDate\" \"$dateSystem\" $argsx", $retval);
}
$workspace = '';
$dateSystem = date('Y-m-d H:i:s');
$date = '';
$argvx = '';
for ($i = 1; $i <= count($argv) - 1; $i++) {
if (!isset($argv[$i])) {
continue;
}
if (preg_match('/^\+w(.+)$/', $argv[$i], $arrayMatch)) {
$workspace = trim($arrayMatch[1], '"');
} else {
$flagDate = false;
if (preg_match('/^\+d(.+)$/', $argv[$i], $arrayMatch) && in_array($cronName, ['cron'])) {
$date = trim($arrayMatch[1], '"');
$flagDate = true;
}
if (!$flagDate) {
$argvx = $argvx . (($argvx != '')? ' ' : '') . $argv[$i];
}
}
}
} else {
if (!is_dir(PATH_DB . $ws) || !file_exists(PATH_DB . $ws . PATH_SEP . "db.php")) {
throw new Exception("Error: The workspace \"$ws\" does not exist");
if (!empty($date) && preg_match('/^' . '[1-9]\d{3}\-(?:0[1-9]|1[0-2])\-(?:0[1-9]|[12][0-9]|3[01])' . '(?:\s' . '(?:[0-1]\d|2[0-3])\:[0-5]\d\:[0-5]\d' . ')?$/', $date)) {
eprintln('[Applying date filter: ' . $date . ']');
} else {
$date = $dateSystem;
}
$cws = 1;
$counterw = 0;
system("php -f \"" . dirname(__FILE__) . PATH_SEP . "cron_single.php\" $ws \"$sDate\" \"$dateSystem\" $argsx", $retval);
if ($workspace == '') {
$d = dir(PATH_DB);
while (($entry = $d->read()) !== false) {
if ($entry != '' && $entry != '.' && $entry != '..') {
if (is_dir(PATH_DB . $entry)) {
if (file_exists(PATH_DB . $entry . PATH_SEP . 'db.php')) {
$counterw++;
passthru('php -f "' . $cronSinglePath . '" "' . base64_encode(PATH_HOME) . '" "' . base64_encode(PATH_TRUNK) . '" "' . base64_encode(PATH_OUTTRUNK) . '" ' . $cronName . ' ' . $entry . ' "' . $dateSystem . '" "' . $date . '" ' . $argvx);
}
}
}
}
} else {
if (!is_dir(PATH_DB . $workspace) || !file_exists(PATH_DB . $workspace . PATH_SEP . 'db.php')) {
throw new Exception('Error: The workspace "' . $workspace . '" does not exist');
}
$counterw++;
passthru('php -f "' . $cronSinglePath . '" "' . base64_encode(PATH_HOME) . '" "' . base64_encode(PATH_TRUNK) . '" "' . base64_encode(PATH_OUTTRUNK) . '" ' . $cronName . ' ' . $workspace . ' "' . $dateSystem . '" "' . $date . '" ' . $argvx);
}
eprintln('Finished ' . $counterw . ' workspaces processed');
} catch (Exception $e) {
throw $e;
}
eprintln("Finished $cws workspaces processed.");
} catch (Exception $e) {
eprintln("Has produced the following error:", "red");
eprintln("* " . $e->getMessage());
eprintln("[DONE]", "green");
//End CRON
$arrayCron = ['flagIsRunning' => '0', 'lastExecution' => date('Y-m-d H:i:s')];
file_put_contents(PATH_DATA . $cronName, serialize($arrayCron));
} else {
eprintln('The ' . $arrayCronConfig[$cronName]['title'] . ' is running, please wait for it to finish' . "\n" . 'Started in ' . $lastExecution);
eprintln('If do you want force the execution use the option "' . $arrayCommandOption['force'] . '", example: php -f ' . $cronName . '.php +wworkflow ' . $arrayCommandOption['force'] ,'green');
}
//End cron
$arrayCron = array("bCronIsRunning" => "0", "sLastExecution" => date("Y-m-d H:i:s"));
@file_put_contents(PATH_DATA . "cron", serialize($arrayCron));
} else {
eprintln("The cron is running, please wait for it to finish.\nStarted in $sLastExecution");
eprintln("If do you want force the execution use the option '+force', example: php -f +wworkflow +force" ,"green");
echo 'Done!' . "\n";
} catch (Exception $e) {
echo $e->getMessage() . "\n";
}