From 971de1fc81f12f3af21c6217b508b1bb178c71c8 Mon Sep 17 00:00:00 2001 From: Roly Rudy Gutierrez Pinto Date: Fri, 25 Sep 2020 15:04:43 -0400 Subject: [PATCH] PMCORE-2189 Migrate to queue job - Cron File: cron.php - Activity: report_by_process --- .../ProcessMaker/TaskScheduler/TaskTest.php | 30 ++++++++ workflow/engine/bin/cron_single.php | 37 ++-------- .../src/ProcessMaker/TaskScheduler/Task.php | 69 +++++++++++++++---- 3 files changed, 89 insertions(+), 47 deletions(-) diff --git a/tests/unit/workflow/engine/src/ProcessMaker/TaskScheduler/TaskTest.php b/tests/unit/workflow/engine/src/ProcessMaker/TaskScheduler/TaskTest.php index 0634e1856..74cb8c234 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/TaskScheduler/TaskTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/TaskScheduler/TaskTest.php @@ -338,4 +338,34 @@ class TaskTest extends TestCase Queue::assertPushed(TaskScheduler::class); } } + + /** + * This test verify the fillReportByProcess activity method for synchronous and asynchronous execution. + * @test + * @covers ProcessMaker\TaskScheduler\Task::runTask() + * @covers ProcessMaker\TaskScheduler\Task::fillReportByProcess() + * @dataProvider asynchronousCases + */ + public function it_should_test_fillReportByProcess_method($asynchronous) + { + $task = new Task($asynchronous, ''); + $dateInit = $this->faker->dateTime; + $dateFinish = $this->faker->dateTime; + + //assert synchronous for cron file + if ($asynchronous === false) { + ob_start(); + $task->fillReportByProcess($dateInit, $dateFinish); + $printing = ob_get_clean(); + $this->assertRegExp("/User Reporting/", $printing); + } + + //assert asynchronous for job process + if ($asynchronous === true) { + Queue::fake(); + Queue::assertNothingPushed(); + $task->fillReportByProcess($dateInit, $dateFinish); + Queue::assertPushed(TaskScheduler::class); + } + } } diff --git a/workflow/engine/bin/cron_single.php b/workflow/engine/bin/cron_single.php index 505e1724e..d5c300e7c 100644 --- a/workflow/engine/bin/cron_single.php +++ b/workflow/engine/bin/cron_single.php @@ -317,10 +317,12 @@ try { $task->executePlugins(); } /*----------------------------------********---------------------------------*/ - if (empty($argvx) || strpos($argvx, "report_by_user") !== false) { + if (strpos($argvx, "report_by_user") !== false) { $task->fillReportByUser($dateInit, $dateFinish); } - fillReportByProcess(); + if (strpos($argvx, "report_by_process") !== false) { + $task->fillReportByProcess($dateInit, $dateFinish); + } synchronizeDrive(); synchronizeGmailLabels(); /*----------------------------------********---------------------------------*/ @@ -589,37 +591,6 @@ function setExecutionResultMessage($m, $t = '') } /*----------------------------------********---------------------------------*/ -function fillReportByProcess() -{ - try { - global $argvx; - global $dateInit; - global $dateFinish; - - if (strpos($argvx, "report_by_process") === false) { - return false; - } - - if ($dateInit == null) { - eprintln("You must enter the starting date.", "red"); - eprintln('Example: +init-date"YYYY-MM-DD HH:MM:SS" +finish-date"YYYY-MM-DD HH:MM:SS"', "red"); - return false; - } - - $dateFinish = ($dateFinish != null) ? $dateFinish : date("Y-m-d H:i:s"); - $appcv = new AppCacheView(); - $appcv->setPathToAppCacheFiles(PATH_METHODS . 'setup' . PATH_SEP . 'setupSchemas' . PATH_SEP); - - setExecutionMessage("Calculating data to fill the 'Process Reporting'..."); - $appcv->fillReportByProcess($dateInit, $dateFinish); - setExecutionResultMessage("DONE"); - } catch (Exception $e) { - setExecutionResultMessage("WITH ERRORS", "error"); - eprintln(" '-" . $e->getMessage(), "red"); - saveLog("fillReportByProcess", "error", "Error in fill report by process: " . $e->getMessage()); - } -} - function synchronizeDrive() { try { diff --git a/workflow/engine/src/ProcessMaker/TaskScheduler/Task.php b/workflow/engine/src/ProcessMaker/TaskScheduler/Task.php index 0d36bde95..fb55143ec 100644 --- a/workflow/engine/src/ProcessMaker/TaskScheduler/Task.php +++ b/workflow/engine/src/ProcessMaker/TaskScheduler/Task.php @@ -15,8 +15,8 @@ use Criteria; use Exception; use G; use Illuminate\Support\Facades\Log; -use ProcessMaker\Plugins\PluginRegistry; use ProcessMaker\Core\JobsManager; +use ProcessMaker\Plugins\PluginRegistry; use Propel; use ResultSet; use SpoolRun; @@ -396,24 +396,25 @@ class Task /** * This fills the report by user. - * @param datetime $dateInit - * @param datetime $dateFinish + * @param string $dateInit + * @param string $dateFinish + * @return boolean */ public function fillReportByUser($dateInit, $dateFinish) { + if ($dateInit == null) { + if ($this->asynchronous === false) { + eprintln("You must enter the starting date.", "red"); + eprintln('Example: +init-date"YYYY-MM-DD HH:MM:SS" +finish-date"YYYY-MM-DD HH:MM:SS"', "red"); + } + if ($this->asynchronous === true) { + $message = 'You must enter the starting date. Example: +init-date"YYYY-MM-DD HH:MM:SS" +finish-date"YYYY-MM-DD HH:MM:SS"'; + Log::channel('taskScheduler:taskScheduler')->info($message, Bootstrap::context($context)); + } + return false; + } $job = function() use($dateInit, $dateFinish) { try { - if ($dateInit == null) { - if ($this->asynchronous === false) { - eprintln("You must enter the starting date.", "red"); - eprintln('Example: +init-date"YYYY-MM-DD HH:MM:SS" +finish-date"YYYY-MM-DD HH:MM:SS"', "red"); - } - if ($this->asynchronous === true) { - $message = 'You must enter the starting date. Example: +init-date"YYYY-MM-DD HH:MM:SS" +finish-date"YYYY-MM-DD HH:MM:SS"'; - Log::channel('taskScheduler:taskScheduler')->info($message, Bootstrap::context($context)); - } - return false; - } $dateFinish = ($dateFinish != null) ? $dateFinish : date("Y-m-d H:i:s"); @@ -432,4 +433,44 @@ class Task }; $this->runTask($job); } + + /** + * This fills the report by process. + * @param string $dateInit + * @param string $dateFinish + * @return boolean + */ + public function fillReportByProcess($dateInit, $dateFinish) + { + if ($dateInit == null) { + if ($this->asynchronous === false) { + eprintln("You must enter the starting date.", "red"); + eprintln('Example: +init-date"YYYY-MM-DD HH:MM:SS" +finish-date"YYYY-MM-DD HH:MM:SS"', "red"); + } + if ($this->asynchronous === true) { + $message = 'You must enter the starting date. Example: +init-date"YYYY-MM-DD HH:MM:SS" +finish-date"YYYY-MM-DD HH:MM:SS"'; + Log::channel('taskScheduler:taskScheduler')->info($message, Bootstrap::context()); + } + return false; + } + $job = function() { + try { + + $dateFinish = ($dateFinish != null) ? $dateFinish : date("Y-m-d H:i:s"); + $appcv = new AppCacheView(); + $appcv->setPathToAppCacheFiles(PATH_METHODS . 'setup' . PATH_SEP . 'setupSchemas' . PATH_SEP); + + $this->setExecutionMessage("Calculating data to fill the 'Process Reporting'..."); + $appcv->fillReportByProcess($dateInit, $dateFinish); + $this->setExecutionResultMessage("DONE"); + } catch (Exception $e) { + $this->setExecutionResultMessage("WITH ERRORS", "error"); + if ($this->asynchronous === false) { + eprintln(" '-" . $e->getMessage(), "red"); + } + $this->saveLog("fillReportByProcess", "error", "Error in fill report by process: " . $e->getMessage()); + } + }; + $this->runTask($job); + } }