Add "registerCronFile" method to the plugin class

This commit is contained in:
Julio Cesar Laura
2013-11-25 13:01:30 -04:00
parent ad2c91fe53
commit e97a04943f
3 changed files with 86 additions and 19 deletions

View File

@@ -554,40 +554,58 @@ function executePlugins()
{ {
global $sFilter; global $sFilter;
if ($sFilter!='' && strpos($sFilter, 'plugins') === false) { if ($sFilter != '' && strpos($sFilter, 'plugins') === false) {
return 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)) { if (!is_dir($pathCronPlugins)) {
return false; return false;
} }
if ($handle = opendir($pathCronPlugins)) { if ($handle = opendir($pathCronPlugins)) {
setExecutionMessage('--- Executing cron files in bin/plugins directory in Workspace:' . SYS_SYS);
while (false !== ($file = readdir($handle))) { while (false !== ($file = readdir($handle))) {
if (strpos($file, '.php',1) && is_file($pathCronPlugins . $file)) { if (strpos($file, '.php',1) && is_file($pathCronPlugins . $file)) {
$filename = str_replace('.php' , '', $file); $filename = str_replace('.php' , '', $file);
$className = $filename . 'ClassCron'; $className = $filename . 'ClassCron';
include_once ( $pathCronPlugins . $file ); //$filename. ".php" // Execute custom cron function
executeCustomCronFunction($pathCronPlugins . $file, $className);
$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');
}
} }
} }
} }
// 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() function calculateDuration()

View File

@@ -97,7 +97,6 @@ class PMPlugin
$oPluginRegistry->registerDashlets($this->sNamespace); $oPluginRegistry->registerDashlets($this->sNamespace);
} }
/** /**
* With this function we can register the report * With this function we can register the report
* param * param
@@ -360,6 +359,17 @@ class PMPlugin
$oPluginRegistry =& PMPluginRegistry::getSingleton(); $oPluginRegistry =& PMPluginRegistry::getSingleton();
$oPluginRegistry->unregisterRestService($this->sNamespace, $classname, $path); $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 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;
}
}

View File

@@ -97,6 +97,7 @@ class PMPluginRegistry
private $_aCaseSchedulerPlugin = array (); private $_aCaseSchedulerPlugin = array ();
private $_aTaskExtendedProperties = array (); private $_aTaskExtendedProperties = array ();
private $_aDashboardPages = array (); private $_aDashboardPages = array ();
private $_aCronFiles = array ();
/** /**
* Registry a plugin javascript to include with js core at same runtime * Registry a plugin javascript to include with js core at same runtime
@@ -1435,6 +1436,27 @@ class PMPluginRegistry
} }
$language->updateLanguagePlugin($namePlugin, SYS_LANG); $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;
}
}
} }