PMCORE-2187 Migrate to queue job - Cron File: cron.php - Activity: plugins

This commit is contained in:
Roly Rudy Gutierrez Pinto
2020-09-24 19:49:32 -04:00
committed by Julio Cesar Laura Avendaño
parent a073b65e08
commit ebfd16fea1
3 changed files with 115 additions and 76 deletions

View File

@@ -280,4 +280,32 @@ class TaskTest extends TestCase
Queue::assertPushed(TaskScheduler::class); Queue::assertPushed(TaskScheduler::class);
} }
} }
/**
* This test verify the executePlugins activity method for synchronous and asynchronous execution.
* @test
* @covers ProcessMaker\TaskScheduler\Task::runTask()
* @covers ProcessMaker\TaskScheduler\Task::executePlugins()
* @dataProvider asynchronousCases
*/
public function it_should_test_executePlugins_method($asynchronous)
{
$task = new Task($asynchronous, '');
//assert synchronous for cron file
if ($asynchronous === false) {
ob_start();
$task->executePlugins();
$printing = ob_get_clean();
$this->assertRegExp("/plugins/", $printing);
}
//assert asynchronous for job process
if ($asynchronous === true) {
Queue::fake();
Queue::assertNothingPushed();
$task->executePlugins();
Queue::assertPushed(TaskScheduler::class);
}
}
} }

View File

@@ -313,7 +313,9 @@ try {
if (empty($argvx) || strpos($argvx, "clean-self-service-tables") !== false) { if (empty($argvx) || strpos($argvx, "clean-self-service-tables") !== false) {
$task->cleanSelfServiceTables(); $task->cleanSelfServiceTables();
} }
executePlugins(); if (empty($argvx) || strpos($argvx, "plugins") !== false) {
$task->executePlugins();
}
/*----------------------------------********---------------------------------*/ /*----------------------------------********---------------------------------*/
fillReportByUser(); fillReportByUser();
fillReportByProcess(); fillReportByProcess();
@@ -370,81 +372,6 @@ try {
G::outRes(G::LoadTranslation("ID_EXCEPTION_LOG_INTERFAZ", array($token)) . "\n"); G::outRes(G::LoadTranslation("ID_EXCEPTION_LOG_INTERFAZ", array($token)) . "\n");
} }
function executePlugins()
{
global $argvx;
if ($argvx != "" && strpos($argvx, "plugins") === false) {
return false;
}
$pathCronPlugins = PATH_CORE . 'bin' . PATH_SEP . 'plugins' . PATH_SEP;
// 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: ' . config("system.workspace"));
while (false !== ($file = readdir($handle))) {
if (strpos($file, '.php', 1) && is_file($pathCronPlugins . $file)) {
$filename = str_replace('.php', '', $file);
$className = $filename . 'ClassCron';
// Execute custom cron function
executeCustomCronFunction($pathCronPlugins . $file, $className);
}
}
}
// Executing registered cron files
// -> Get registered cron files
$oPluginRegistry = PluginRegistry::loadSingleton();
$cronFiles = $oPluginRegistry->getCronFiles();
// -> Execute functions
if (!empty($cronFiles)) {
setExecutionMessage('Executing registered cron files for Workspace: ' . config('system.workspace'));
/**
* @var \ProcessMaker\Plugins\Interfaces\CronFile $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());
} else {
setExecutionMessage('File ' . $cronFile->getCronFile() . '.php ' . 'does not exist.');
}
}
}
}
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("\n--- Executing cron file: $pathFile");
try {
$oPlugin->executeCron();
setExecutionResultMessage('DONE');
} catch (Exception $e) {
setExecutionResultMessage('FAILED', 'error');
eprintln(" '-" . $e->getMessage(), 'red');
saveLog('executePlugins', 'error', 'Error executing cron file: ' . $pathFile . ' - ' . $e->getMessage());
}
}
}
function executeEvents() function executeEvents()
{ {
global $sLastExecution; global $sLastExecution;

View File

@@ -14,6 +14,7 @@ use Criteria;
use Exception; use Exception;
use G; use G;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use ProcessMaker\Plugins\PluginRegistry;
use ProcessMaker\Core\JobsManager; use ProcessMaker\Core\JobsManager;
use Propel; use Propel;
use ResultSet; use ResultSet;
@@ -308,4 +309,87 @@ class Task
}; };
$this->runTask($job); $this->runTask($job);
} }
/**
* This execute plugins cron.
* @return boolean
*/
public function executePlugins()
{
$job = function() {
$pathCronPlugins = PATH_CORE . 'bin' . PATH_SEP . 'plugins' . PATH_SEP;
// Executing cron files in bin/plugins directory
if (!is_dir($pathCronPlugins)) {
return false;
}
if ($handle = opendir($pathCronPlugins)) {
$this->setExecutionMessage('Executing cron files in bin/plugins directory in Workspace: ' . config("system.workspace"));
while (false !== ($file = readdir($handle))) {
if (strpos($file, '.php', 1) && is_file($pathCronPlugins . $file)) {
$filename = str_replace('.php', '', $file);
$className = $filename . 'ClassCron';
// Execute custom cron function
$this->executeCustomCronFunction($pathCronPlugins . $file, $className);
}
}
}
// Executing registered cron files
// -> Get registered cron files
$pluginRegistry = PluginRegistry::loadSingleton();
$cronFiles = $pluginRegistry->getCronFiles();
// -> Execute functions
if (!empty($cronFiles)) {
$this->setExecutionMessage('Executing registered cron files for Workspace: ' . config('system.workspace'));
/**
* @var \ProcessMaker\Plugins\Interfaces\CronFile $cronFile
*/
foreach ($cronFiles as $cronFile) {
$path = PATH_PLUGINS . $cronFile->getNamespace() . PATH_SEP . 'bin' . PATH_SEP . $cronFile->getCronFile() . '.php';
if (file_exists($path)) {
$this->executeCustomCronFunction($path, $cronFile->getCronFile());
} else {
$this->setExecutionMessage('File ' . $cronFile->getCronFile() . '.php ' . 'does not exist.');
}
}
}
};
$this->runTask($job);
}
/**
* This execute custom cron function.
* @param string $pathFile
* @param string $className
*/
public function executeCustomCronFunction($pathFile, $className)
{
include_once $pathFile;
$plugin = new $className();
if (method_exists($plugin, '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
$this->setExecutionMessage("\n--- Executing cron file: $pathFile");
try {
$plugin->executeCron();
$this->setExecutionResultMessage('DONE');
} catch (Exception $e) {
$this->setExecutionResultMessage('FAILED', 'error');
if ($this->asynchronous === false) {
eprintln(" '-" . $e->getMessage(), 'red');
}
$this->saveLog('executePlugins', 'error', 'Error executing cron file: ' . $pathFile . ' - ' . $e->getMessage());
}
}
}
} }