From e97a04943f0e55ac362d8a348126f8844998deb5 Mon Sep 17 00:00:00 2001 From: Julio Cesar Laura Date: Mon, 25 Nov 2013 13:01:30 -0400 Subject: [PATCH] Add "registerCronFile" method to the plugin class --- workflow/engine/bin/cron_single.php | 52 +++++++++++++------ workflow/engine/classes/class.plugin.php | 29 ++++++++++- .../engine/classes/class.pluginRegistry.php | 24 ++++++++- 3 files changed, 86 insertions(+), 19 deletions(-) diff --git a/workflow/engine/bin/cron_single.php b/workflow/engine/bin/cron_single.php index 3a8df5026..0a439b266 100755 --- a/workflow/engine/bin/cron_single.php +++ b/workflow/engine/bin/cron_single.php @@ -554,40 +554,58 @@ function executePlugins() { global $sFilter; - if ($sFilter!='' && strpos($sFilter, 'plugins') === false) { + if ($sFilter != '' && strpos($sFilter, 'plugins') === false) { return false; } - $pathCronPlugins = PATH_CORE.'bin'.PATH_SEP.'plugins'.PATH_SEP; + $pathCronPlugins = PATH_CORE . 'bin' . PATH_SEP . 'plugins' . PATH_SEP; - //erik: verify if the plugin dir exists + // Executing cron files in bin/plugins directory if (!is_dir($pathCronPlugins)) { return false; } if ($handle = opendir($pathCronPlugins)) { + setExecutionMessage('--- Executing cron files in bin/plugins directory in Workspace:' . SYS_SYS); while (false !== ($file = readdir($handle))) { if (strpos($file, '.php',1) && is_file($pathCronPlugins . $file)) { $filename = str_replace('.php' , '', $file); $className = $filename . 'ClassCron'; - include_once ( $pathCronPlugins . $file ); //$filename. ".php" - - $oPlugin = new $className(); - - if (method_exists($oPlugin, 'executeCron')) { - $arrayCron = unserialize(trim(@file_get_contents(PATH_DATA . "cron"))); - $arrayCron["processcTimeProcess"] = 60; //Minutes - $arrayCron["processcTimeStart"] = time(); - @file_put_contents(PATH_DATA . "cron", serialize($arrayCron)); - - $oPlugin->executeCron(); - setExecutionMessage("Executing Plugins"); - setExecutionResultMessage('DONE'); - } + // Execute custom cron function + executeCustomCronFunction($pathCronPlugins . $file, $className); } } } + + // Executing registered cron files + // -> Get registered cron files + // -> Execute functions + +} +function executeCustomCronFunction($pathFile, $className) +{ + include_once $pathFile; + + $oPlugin = new $className(); + + if (method_exists($oPlugin, 'executeCron')) { + $arrayCron = unserialize(trim(@file_get_contents(PATH_DATA . "cron"))); + $arrayCron["processcTimeProcess"] = 60; //Minutes + $arrayCron["processcTimeStart"] = time(); + @file_put_contents(PATH_DATA . "cron", serialize($arrayCron)); + + //Try to execute Plugin Cron. If there is an error then continue with the next file + setExecutionMessage("------ Executing cron file: $filename"); + try { + $oPlugin->executeCron(); + setExecutionResultMessage('DONE'); + } catch (Exception $e) { + setExecutionResultMessage('FAILED', 'error'); + eprintln(" '-".$e->getMessage(), 'red'); + saveLog('executePlugins', 'error', 'Error executing Plugin ' . $filename . ': ' . $e->getMessage()); + } + } } function calculateDuration() diff --git a/workflow/engine/classes/class.plugin.php b/workflow/engine/classes/class.plugin.php index 986d773d0..415e048be 100755 --- a/workflow/engine/classes/class.plugin.php +++ b/workflow/engine/classes/class.plugin.php @@ -97,7 +97,6 @@ class PMPlugin $oPluginRegistry->registerDashlets($this->sNamespace); } - /** * With this function we can register the report * param @@ -360,6 +359,17 @@ class PMPlugin $oPluginRegistry =& PMPluginRegistry::getSingleton(); $oPluginRegistry->unregisterRestService($this->sNamespace, $classname, $path); } + + /** + * With this function we can register a cron file + * param string $cronFile + * @return void + */ + public function registerCronFile($cronFile) + { + $oPluginRegistry =& PMPluginRegistry::getSingleton(); + $oPluginRegistry->registerCronFile($this->sNamespace, $cronFile); + } } class menuDetail @@ -682,3 +692,20 @@ class dashboardPage } } +class cronFile +{ + public $namespace; + public $cronFile; + + /** + * This function is the constructor of the cronFile class + * param string $namespace + * param string $cronFile + * @return void + */ + public function __construct($namespace, $cronFile) + { + $this->namespace = $namespace; + $this->cronFile = $cronFile; + } +} \ No newline at end of file diff --git a/workflow/engine/classes/class.pluginRegistry.php b/workflow/engine/classes/class.pluginRegistry.php index 81ee5b68b..dbaf56db6 100755 --- a/workflow/engine/classes/class.pluginRegistry.php +++ b/workflow/engine/classes/class.pluginRegistry.php @@ -97,6 +97,7 @@ class PMPluginRegistry private $_aCaseSchedulerPlugin = array (); private $_aTaskExtendedProperties = array (); private $_aDashboardPages = array (); + private $_aCronFiles = array (); /** * Registry a plugin javascript to include with js core at same runtime @@ -1435,6 +1436,27 @@ class PMPluginRegistry } $language->updateLanguagePlugin($namePlugin, SYS_LANG); } - } + } + + /** + * Register a cron file in the singleton + * + * @param unknown_type $namespace + * @param unknown_type $cronFile + */ + public function registerCronFile ($namespace, $cronFile) + { + $found = false; + foreach ($this->_aCronFiles as $row => $detail) { + if ($cronFile == $detail->cronFile && $namespace == $detail->namespace) { + $detail->cronFile = $cronFile; + $found = true; + } + } + if (!$found) { + $cronFile = new cronFile( $namespace, $cronFile ); + $this->_aCronFiles[] = $cronFile; + } + } }