From ebfd16fea1272e7e39226a5f9099e9604eec929d Mon Sep 17 00:00:00 2001 From: Roly Rudy Gutierrez Pinto Date: Thu, 24 Sep 2020 19:49:32 -0400 Subject: [PATCH] PMCORE-2187 Migrate to queue job - Cron File: cron.php - Activity: plugins --- .../ProcessMaker/TaskScheduler/TaskTest.php | 28 +++++++ workflow/engine/bin/cron_single.php | 79 +---------------- .../src/ProcessMaker/TaskScheduler/Task.php | 84 +++++++++++++++++++ 3 files changed, 115 insertions(+), 76 deletions(-) diff --git a/tests/unit/workflow/engine/src/ProcessMaker/TaskScheduler/TaskTest.php b/tests/unit/workflow/engine/src/ProcessMaker/TaskScheduler/TaskTest.php index e6d661be8..b493b3a22 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/TaskScheduler/TaskTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/TaskScheduler/TaskTest.php @@ -280,4 +280,32 @@ class TaskTest extends TestCase 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); + } + } } diff --git a/workflow/engine/bin/cron_single.php b/workflow/engine/bin/cron_single.php index dd7856c62..0bd6532f7 100644 --- a/workflow/engine/bin/cron_single.php +++ b/workflow/engine/bin/cron_single.php @@ -313,7 +313,9 @@ try { if (empty($argvx) || strpos($argvx, "clean-self-service-tables") !== false) { $task->cleanSelfServiceTables(); } - executePlugins(); + if (empty($argvx) || strpos($argvx, "plugins") !== false) { + $task->executePlugins(); + } /*----------------------------------********---------------------------------*/ fillReportByUser(); fillReportByProcess(); @@ -370,81 +372,6 @@ try { 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() { global $sLastExecution; diff --git a/workflow/engine/src/ProcessMaker/TaskScheduler/Task.php b/workflow/engine/src/ProcessMaker/TaskScheduler/Task.php index ca672e90b..bd3033931 100644 --- a/workflow/engine/src/ProcessMaker/TaskScheduler/Task.php +++ b/workflow/engine/src/ProcessMaker/TaskScheduler/Task.php @@ -14,6 +14,7 @@ use Criteria; use Exception; use G; use Illuminate\Support\Facades\Log; +use ProcessMaker\Plugins\PluginRegistry; use ProcessMaker\Core\JobsManager; use Propel; use ResultSet; @@ -308,4 +309,87 @@ class Task }; $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()); + } + } + } }