From d636f2794d67d200e65fce416cb4d0592d481d40 Mon Sep 17 00:00:00 2001 From: Roly Rudy Gutierrez Pinto Date: Mon, 20 Aug 2018 14:40:30 -0400 Subject: [PATCH] HOR-4563-A --- workflow/engine/bin/cron_single.php | 66 +++----- workflow/engine/methods/setup/cronAjax.php | 147 +++--------------- .../ProcessMaker/BusinessModel/Files/Cron.php | 146 +++++++++++++++++ 3 files changed, 191 insertions(+), 168 deletions(-) create mode 100644 workflow/engine/src/ProcessMaker/BusinessModel/Files/Cron.php diff --git a/workflow/engine/bin/cron_single.php b/workflow/engine/bin/cron_single.php index cc9dc246e..9019c0721 100644 --- a/workflow/engine/bin/cron_single.php +++ b/workflow/engine/bin/cron_single.php @@ -9,16 +9,12 @@ require_once __DIR__ . '/../../../bootstrap/app.php'; use ProcessMaker\Core\System; use ProcessMaker\Plugins\PluginRegistry; -register_shutdown_function( - create_function( - '', - ' - if (class_exists("Propel")) { - Propel::close(); - } - ' - ) -); +$fn = ' + if (class_exists("Propel")) { + Propel::close(); + } + '; +register_shutdown_function(create_function('', $fn)); ini_set('memory_limit', '512M'); @@ -45,8 +41,7 @@ try { $cronName = $argv[4]; $workspace = $argv[5]; $dateSystem = $argv[6]; - $sNow = $argv[7]; //$date - + $sNow = $argv[7]; //date //Defines constants define('PATH_SEP', ($osIsLinux) ? '/' : '\\'); @@ -68,10 +63,7 @@ try { $classLoader->add(PATH_TRUNK . 'workflow' . PATH_SEP . 'engine' . PATH_SEP . 'src' . PATH_SEP); //Add vendors to autoloader - //$classLoader->add(PATH_TRUNK . 'vendor' . PATH_SEP . 'luracast' . PATH_SEP . 'restler' . PATH_SEP . 'vendor', 'Luracast'); - //$classLoader->add(PATH_TRUNK . 'vendor' . PATH_SEP . 'bshaffer' . PATH_SEP . 'oauth2-server-php' . PATH_SEP . 'src' . PATH_SEP, 'OAuth2'); - $classLoader->addClass('Bootstrap', - PATH_TRUNK . 'gulliver' . PATH_SEP . 'system' . PATH_SEP . 'class.bootstrap.php'); + $classLoader->addClass('Bootstrap', PATH_TRUNK . 'gulliver' . PATH_SEP . 'system' . PATH_SEP . 'class.bootstrap.php'); $classLoader->addModelClassPath(PATH_TRUNK . 'workflow' . PATH_SEP . 'engine' . PATH_SEP . 'classes' . PATH_SEP . 'model' . PATH_SEP); @@ -89,7 +81,6 @@ try { 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']); @@ -99,9 +90,6 @@ try { define('MEMCACHED_ENABLED', $arraySystemConfiguration['memcached']); define('MEMCACHED_SERVER', $arraySystemConfiguration['memcached_server']); - //require_once(PATH_GULLIVER . PATH_SEP . 'class.bootstrap.php'); - //define('PATH_GULLIVER_HOME', PATH_TRUNK . 'gulliver' . PATH_SEP); - spl_autoload_register(['Bootstrap', 'autoloadClass']); //Set variables @@ -168,8 +156,7 @@ try { while (!feof($fileDb)) { $buffer = fgets($fileDb, 4096); //Read a line - $phpCode .= preg_replace('/define\s*\(\s*[\x22\x27](.*)[\x22\x27]\s*,\s*(\x22.*\x22|\x27.*\x27)\s*\)\s*;/i', - '$$1 = $2;', $buffer); + $phpCode .= preg_replace('/define\s*\(\s*[\x22\x27](.*)[\x22\x27]\s*,\s*(\x22.*\x22|\x27.*\x27)\s*\)\s*;/i', '$$1 = $2;', $buffer); } fclose($fileDb); @@ -189,8 +176,6 @@ try { $dsnRbac .= '?encoding=utf8'; break; case 'mssql': - //$dsn .= '?sendStringAsUnicode=false'; - //$dsnRbac .= '?sendStringAsUnicode=false'; break; default: break; @@ -203,15 +188,12 @@ try { $pro['datasources']['rbac']['adapter'] = $DB_ADAPTER; $pro['datasources']['rp']['connection'] = $dsnRp; $pro['datasources']['rp']['adapter'] = $DB_ADAPTER; - //$pro['datasources']['dbarray']['connection'] = 'dbarray://user:pass@localhost/pm_os'; - //$pro['datasources']['dbarray']['adapter'] = 'dbarray'; $oFile = fopen(PATH_CORE . 'config' . PATH_SEP . '_databases_.php', 'w'); fwrite($oFile, ''); fclose($oFile); Propel::init(PATH_CORE . 'config' . PATH_SEP . '_databases_.php'); - //Creole::registerDriver('dbarray', 'creole.contrib.DBArrayConnection'); //Enable RBAC $rbac = RBAC::getSingleton(PATH_DATA, session_id()); @@ -226,7 +208,7 @@ try { /*----------------------------------********---------------------------------*/ if (PMLicensedFeatures::getSingleton()->verifyfeature('oq3S29xemxEZXJpZEIzN01qenJUaStSekY4cTdJVm5vbWtVM0d4S2lJSS9qUT0=')) { - $systemUtcTimeZone = (int)($arraySystemConfiguration['system_utc_time_zone']) == 1; + $systemUtcTimeZone = (int) ($arraySystemConfiguration['system_utc_time_zone']) == 1; } /*----------------------------------********---------------------------------*/ @@ -234,6 +216,12 @@ try { define('TIME_ZONE', ini_get('date.timezone')); + //UTC time zone + if ($systemUtcTimeZone) { + $sNow = convertToSystemUtcTimeZone($sNow); + $dateSystem = convertToSystemUtcTimeZone($dateSystem); + } + //Processing eprintln('Processing workspace: ' . $workspace, 'green'); @@ -261,7 +249,7 @@ try { case 'timereventcron': $timerEvent = new \ProcessMaker\BusinessModel\TimerEvent(); - $timerEvent->startContinueCaseByTimerEvent(date('Y-m-d H:i:s'), true); + $timerEvent->startContinueCaseByTimerEvent($sNow, true); break; case 'sendnotificationscron': sendNotifications(); @@ -434,7 +422,6 @@ function executePlugins() } // Executing registered cron files - // -> Get registered cron files $oPluginRegistry = PluginRegistry::loadSingleton(); $cronFiles = $oPluginRegistry->getCronFiles(); @@ -445,7 +432,7 @@ function executePlugins() /** * @var \ProcessMaker\Plugins\Interfaces\CronFile $cronFile */ - foreach($cronFiles as $cronFile) { + foreach ($cronFiles as $cronFile) { $path = PATH_PLUGINS . $cronFile->getNamespace() . PATH_SEP . 'bin' . PATH_SEP . $cronFile->getCronFile() . '.php'; if (file_exists($path)) { executeCustomCronFunction($path, $cronFile->getCronFile()); @@ -503,8 +490,8 @@ function calculateDuration() saveLog('calculateDuration', 'error', 'Error Calculating Duration: ' . $oError->getMessage()); } } - /*----------------------------------********---------------------------------*/ + function calculateAppDuration() { global $argvx; @@ -527,7 +514,6 @@ function calculateAppDuration() saveLog('calculateDurationByApp', 'error', 'Error Calculating Duration: ' . $oError->getMessage()); } } - /*----------------------------------********---------------------------------*/ function executeEvents($sLastExecution, $sNow = null) @@ -559,7 +545,6 @@ function executeEvents($sLastExecution, $sNow = null) setExecutionMessage("|- End Execution events"); setExecutionResultMessage("Processed $n"); - //saveLog('executeEvents', 'action', $res ); } catch (Exception $oError) { setExecutionResultMessage('WITH ERRORS', 'error'); eprintln(" '-" . $oError->getMessage(), 'red'); @@ -581,10 +566,8 @@ function executeScheduledCases($sNow = null) setExecutionMessage("Executing the scheduled starting cases"); setExecutionResultMessage('PROCESSING'); - $runDate = runDateForScheduledCases($sNow); - $oCaseScheduler = new CaseScheduler(); - $oCaseScheduler->caseSchedulerCron($runDate, $log, 1); + $oCaseScheduler->caseSchedulerCron($sNow, $log, 1); foreach ($log as $value) { $arrayCron = unserialize(trim(@file_get_contents(PATH_DATA . "cron"))); @@ -601,7 +584,7 @@ function executeScheduledCases($sNow = null) } } -function runDateForScheduledCases($sNow) +function convertToSystemUtcTimeZone($sNow) { global $arraySystemConfiguration; @@ -610,7 +593,7 @@ function runDateForScheduledCases($sNow) $systemUtcTimeZone = false; /*----------------------------------********---------------------------------*/ if (PMLicensedFeatures::getSingleton()->verifyfeature('oq3S29xemxEZXJpZEIzN01qenJUaStSekY4cTdJVm5vbWtVM0d4S2lJSS9qUT0=')) { - $systemUtcTimeZone = (int)($arraySystemConfiguration['system_utc_time_zone']) == 1; + $systemUtcTimeZone = (int) ($arraySystemConfiguration['system_utc_time_zone']) == 1; } /*----------------------------------********---------------------------------*/ @@ -766,7 +749,7 @@ function executeCaseSelfService() $appcacheDelDelegateDate, $taskSelfServiceTime, $taskSelfServiceTimeUnit //HOURS|DAYS|MINUTES - //1 + //1 ); if (time() > $dueDate["DUE_DATE_SECONDS"] && $flag == false) { @@ -898,8 +881,8 @@ function setExecutionResultMessage($m, $t = '') eprintln("[$m]", $c); } - /*----------------------------------********---------------------------------*/ + function fillReportByUser() { try { @@ -1017,7 +1000,6 @@ function synchronizeGmailLabels() saveLog("synchronizeGmailLabels", "error", "Error when synchronizing Gmail labels: " . $e->getMessage()); } } - /*----------------------------------********---------------------------------*/ function sendNotifications() diff --git a/workflow/engine/methods/setup/cronAjax.php b/workflow/engine/methods/setup/cronAjax.php index a0dcc40a1..7bf5e46a3 100644 --- a/workflow/engine/methods/setup/cronAjax.php +++ b/workflow/engine/methods/setup/cronAjax.php @@ -1,117 +1,10 @@ 1) { - $date = (isset( $arrayAux[0] )) ? trim( $arrayAux[0] ) : ""; - $workspace = (isset( $arrayAux[1] )) ? trim( $arrayAux[1] ) : ""; - $action = (isset( $arrayAux[2] )) ? trim( $arrayAux[2] ) : ""; - $status = (isset( $arrayAux[3] )) ? trim( $arrayAux[3] ) : ""; - $description = (isset( $arrayAux[4] )) ? trim( $arrayAux[4] ) : ""; - } - - $mktDate = (! empty( $date )) ? mktimeDate( $date ) : 0; - - //Filter - $sw = 1; - - if ($filter["workspace"] != "ALL" && $workspace != $filter["workspace"]) { - $sw = 0; - } - - if ($filter["status"] != "ALL") { - switch ($filter["status"]) { - case "COMPLETED": - if ($status != "action") { - $sw = 0; - } - break; - case "FAILED": - if ($status == "action") { - $sw = 0; - } - break; - } - } - - if (! empty( $filter["dateFrom"] ) && $mktDate > 0) { - if (! (mktimeDate( $filter["dateFrom"] ) <= $mktDate)) { - $sw = 0; - } - } - - if (! empty( $filter["dateTo"] ) && $mktDate > 0) { - if (! ($mktDate <= mktimeDate( $filter["dateTo"] . " 23:59:59" ))) { - $sw = 0; - } - } - - $arrayData = array (); - - if ($sw == 1) { - $arrayData = array ("DATE" => $date, "ACTION" => $action, "STATUS" => $status, "DESCRIPTION" => $description - ); - } - - return $arrayData; -} - -function cronDataGet ($filter, $r, $i) -{ - $i = $i + 1; - - $arrayData = array (); - $strAux = null; - $numRec = 0; - $cont = 0; - - $file = PATH_DATA . "log" . PATH_SEP . "cron.log"; - - if (file_exists($file)) { - $arrayFileData = file($file); - - for ($k = 0; $k <= count($arrayFileData) - 1; $k++) { - $strAux = $arrayFileData[$k]; - - if (!empty($strAux)) { - $arrayAux = cronArraySet($strAux, $filter); - - if (count($arrayAux) > 0) { - $cont = $cont + 1; - - if ($cont >= $i && count($arrayData) + 1 <= $r) { - $arrayData[] = $arrayAux; - } - } - } - } - } - - $numRec = $cont; - - return array($numRec, $arrayData); -} - -$option = (isset( $_REQUEST["option"] )) ? $_REQUEST["option"] : null; - -$response = array (); +$response = []; switch ($option) { case "LST": @@ -121,40 +14,42 @@ switch ($option) { $dateFrom = $_REQUEST["dateFrom"]; $dateTo = $_REQUEST["dateTo"]; - $arrayFilter = array ("workspace" => $workspace,"status" => $status,"dateFrom" => str_replace( "T00:00:00", null, $dateFrom ),"dateTo" => str_replace( "T00:00:00", null, $dateTo ) - ); + $filter = [ + "workspace" => $workspace, + "status" => $status, + "dateFrom" => str_replace("T00:00:00", null, $dateFrom), + "dateTo" => str_replace("T00:00:00", null, $dateTo) + ]; - $limit = isset( $_REQUEST["limit"] ) ? $_REQUEST["limit"] : $pageSize; - $start = isset( $_REQUEST["start"] ) ? $_REQUEST["start"] : 0; + $start = (int) isset($_REQUEST["start"]) ? $_REQUEST["start"] : 0; + $limit = (int) isset($_REQUEST["limit"]) ? $_REQUEST["limit"] : $pageSize; - list ($numRec, $data) = cronDataGet( $arrayFilter, $limit, $start ); + $cron = new Cron(); + list ($count, $data) = $cron->getData($filter, $start, $limit); - $response = array ("success" => true,"resultTotal" => $numRec,"resultRoot" => $data - ); + $response = [ + "success" => true, + "resultTotal" => $count, + "resultRoot" => $data + ]; break; case "EMPTY": $status = 1; - try { $file = PATH_DATA . "log" . PATH_SEP . "cron.log"; - - if (file_exists( $file )) { - //file_put_contents($file, null); - unlink( $file ); + if (file_exists($file)) { + unlink($file); } - $response["status"] = "OK"; G::auditLog("ClearCron"); } catch (Exception $e) { $response["message"] = $e->getMessage(); $status = 0; } - if ($status == 0) { $response["status"] = "ERROR"; } break; } -echo G::json_encode( $response ); - +echo G::json_encode($response); diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Files/Cron.php b/workflow/engine/src/ProcessMaker/BusinessModel/Files/Cron.php new file mode 100644 index 000000000..35d565485 --- /dev/null +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Files/Cron.php @@ -0,0 +1,146 @@ +setFilepath(PATH_DATA . 'log' . PATH_SEP . 'cron.log'); + } + + /** + * Set the cron file log path. + * + * @param string $filepath + */ + public function setFilepath($filepath) + { + $this->filepath = $filepath; + } + + /** + * Get data from file. + * + * @param array $filter + * @param int $start + * @param int $limit + * @return array + */ + public function getData($filter, $start = 0, $limit = 20) + { + if (!file_exists($this->filepath)) { + return [0, []]; + } + $result = []; + $count = 0; + $array = file($this->filepath); + foreach ($array as $line) { + if (empty($line)) { + continue; + } + $row = $this->getRow($line, $filter); + if ($row === null) { + continue; + } + $count = $count + 1; + if ($start < $count && count($result) < $limit) { + $row['DATE'] = DateTime::convertUtcToTimeZone($row['DATE']); + $result[] = $row; + } + } + return [$count, $result]; + } + + /** + * Get registry from string line. + * + * @param string $line + * @param array $filter + * @return array + */ + public function getRow($line, $filter) + { + $row = explode('|', $line); + $date = ''; + $workspace = ''; + $action = ''; + $status = ''; + $description = trim($row[0]); + + if (!empty($row)) { + $date = isset($row[0]) ? trim($row[0]) : ''; + $workspace = isset($row[1]) ? trim($row[1]) : ''; + $action = isset($row[2]) ? trim($row[2]) : ''; + $status = isset($row[3]) ? trim($row[3]) : ''; + $description = isset($row[4]) ? trim($row[4]) : ''; + } + + + $isValid = true; + if ($filter['workspace'] != 'ALL' && $workspace != $filter['workspace']) { + $isValid = false; + } + if ($filter['status'] != 'ALL') { + switch ($filter['status']) { + case 'COMPLETED': + if ($status != 'action') { + $isValid = false; + } + break; + case 'FAILED': + if ($status == 'action') { + $isValid = false; + } + break; + } + } + + $mktDate = !empty($date) ? $this->mktimeDate($date) : 0; + if (!empty($filter['dateFrom']) && $mktDate > 0) { + if (!($this->mktimeDate($filter['dateFrom']) <= $mktDate)) { + $isValid = false; + } + } + if (!empty($filter['dateTo']) && $mktDate > 0) { + if (!($mktDate <= $this->mktimeDate($filter['dateTo'] . ' 23:59:59'))) { + $isValid = false; + } + } + + if ($isValid) { + return [ + 'DATE' => $date, + 'ACTION' => $action, + 'STATUS' => $status, + 'DESCRIPTION' => $description + ]; + } + return null; + } + + /** + * Create a timestamp from a string value. + * + * @param string $date + * @return int|false + */ + public function mktimeDate($date) + { + $array = getdate(strtotime($date)); + $mktime = mktime($array['hours'], $array['minutes'], $array['seconds'], $array['mon'], $array['mday'], $array['year']); + return $mktime; + } +}