PMCORE-2186 Migrate to queue job - Cron File: cron.php - Activity: clean-self-service-tables

This commit is contained in:
Roly Rudy Gutierrez Pinto
2020-09-24 17:48:05 -04:00
committed by Julio Cesar Laura Avendaño
parent ebfc73ca11
commit a073b65e08
3 changed files with 147 additions and 72 deletions

View File

@@ -57,7 +57,13 @@ class TaskTest extends TestCase
$printing = ob_get_clean(); $printing = ob_get_clean();
//assert if message is contained in output buffer //assert if message is contained in output buffer
$this->assertRegExp("/{$message}/", $printing); if ($asynchronous === false) {
$this->assertRegExp("/{$message}/", $printing);
}
//assert if not showing message
if ($asynchronous === true) {
$this->assertEmpty($printing);
}
} }
/** /**
@@ -76,19 +82,37 @@ class TaskTest extends TestCase
$task->setExecutionResultMessage($message, 'error'); $task->setExecutionResultMessage($message, 'error');
$printing = ob_get_clean(); $printing = ob_get_clean();
//assert if message is contained in output buffer //assert if message is contained in output buffer
$this->assertRegExp("/{$message}/", $printing); if ($asynchronous === false) {
$this->assertRegExp("/{$message}/", $printing);
}
//assert if not showing message
if ($asynchronous === true) {
$this->assertEmpty($printing);
}
ob_start(); ob_start();
$task->setExecutionResultMessage($message, 'info'); $task->setExecutionResultMessage($message, 'info');
$printing = ob_get_clean(); $printing = ob_get_clean();
//assert if message is contained in output buffer //assert if message is contained in output buffer
$this->assertRegExp("/{$message}/", $printing); if ($asynchronous === false) {
$this->assertRegExp("/{$message}/", $printing);
}
//assert if not showing message
if ($asynchronous === true) {
$this->assertEmpty($printing);
}
ob_start(); ob_start();
$task->setExecutionResultMessage($message, 'warning'); $task->setExecutionResultMessage($message, 'warning');
$printing = ob_get_clean(); $printing = ob_get_clean();
//assert if message is contained in output buffer //assert if message is contained in output buffer
$this->assertRegExp("/{$message}/", $printing); if ($asynchronous === false) {
$this->assertRegExp("/{$message}/", $printing);
}
//assert if not showing message
if ($asynchronous === true) {
$this->assertEmpty($printing);
}
} }
/** /**
@@ -106,8 +130,14 @@ class TaskTest extends TestCase
$task->saveLog('', '', $description); $task->saveLog('', '', $description);
$file = PATH_DATA . "log/cron.log"; $file = PATH_DATA . "log/cron.log";
$this->assertFileExists($file); $this->assertFileExists($file);
$contentLog = file_get_contents($file); if ($asynchronous === false) {
$this->assertRegExp("/{$description}/", $contentLog); $contentLog = file_get_contents($file);
$this->assertRegExp("/{$description}/", $contentLog);
}
if ($asynchronous === true) {
$contentLog = file_get_contents($file);
$this->assertNotRegExp("/{$description}/", $contentLog);
}
} }
/** /**
@@ -222,4 +252,32 @@ class TaskTest extends TestCase
Queue::assertPushed(TaskScheduler::class); Queue::assertPushed(TaskScheduler::class);
} }
} }
/**
* This test verify the cleanSelfServiceTables activity method for synchronous and asynchronous execution.
* @test
* @covers ProcessMaker\TaskScheduler\Task::runTask()
* @covers ProcessMaker\TaskScheduler\Task::cleanSelfServiceTables()
* @dataProvider asynchronousCases
*/
public function it_should_test_cleanSelfServiceTables_method($asynchronous)
{
$task = new Task($asynchronous, '');
//assert synchronous for cron file
if ($asynchronous === false) {
ob_start();
$task->cleanSelfServiceTables();
$printing = ob_get_clean();
$this->assertRegExp("/DONE/", $printing);
}
//assert asynchronous for job process
if ($asynchronous === true) {
Queue::fake();
Queue::assertNothingPushed();
$task->cleanSelfServiceTables();
Queue::assertPushed(TaskScheduler::class);
}
}
} }

View File

@@ -310,7 +310,9 @@ try {
executeScheduledCases(); executeScheduledCases();
executeUpdateAppTitle(); executeUpdateAppTitle();
executeCaseSelfService(); executeCaseSelfService();
cleanSelfServiceTables(); if (empty($argvx) || strpos($argvx, "clean-self-service-tables") !== false) {
$task->cleanSelfServiceTables();
}
executePlugins(); executePlugins();
/*----------------------------------********---------------------------------*/ /*----------------------------------********---------------------------------*/
fillReportByUser(); fillReportByUser();
@@ -824,46 +826,3 @@ function sendNotifications()
saveLog("ExecuteSendNotifications", "error", "Error when sending notifications " . $e->getMessage()); saveLog("ExecuteSendNotifications", "error", "Error when sending notifications " . $e->getMessage());
} }
} }
/**
* Clean unused records in tables related to the Self-Service Value Based feature
*
* @see processWorkspace()
*
* @link https://wiki.processmaker.com/3.2/Executing_cron.php#Syntax_of_cron.php_Options
*/
function cleanSelfServiceTables()
{
try {
global $argvx;
// Check if the action can be executed
if ($argvx !== "" && strpos($argvx, "clean-self-service-tables") === false) {
return false;
}
// Start message
setExecutionMessage("Clean unused records for Self-Service Value Based feature");
// Get Propel connection
$cnn = Propel::getConnection(AppAssignSelfServiceValueGroupPeer::DATABASE_NAME);
// Delete related rows and missing relations, criteria don't execute delete with joins
$cnn->begin();
$stmt = $cnn->createStatement();
$stmt->executeQuery("DELETE " . AppAssignSelfServiceValueGroupPeer::TABLE_NAME . "
FROM " . AppAssignSelfServiceValueGroupPeer::TABLE_NAME . "
LEFT JOIN " . AppAssignSelfServiceValuePeer::TABLE_NAME . "
ON (" . AppAssignSelfServiceValueGroupPeer::ID . " = " . AppAssignSelfServiceValuePeer::ID . ")
WHERE " . AppAssignSelfServiceValuePeer::ID . " IS NULL");
$cnn->commit();
// Success message
setExecutionResultMessage("DONE");
} catch (Exception $e) {
$cnn->rollback();
setExecutionResultMessage("WITH ERRORS", "error");
eprintln(" '-" . $e->getMessage(), "red");
saveLog("ExecuteCleanSelfServiceTables", "error", "Error when try to clean self-service tables " . $e->getMessage());
}
}

View File

@@ -3,6 +3,8 @@
namespace ProcessMaker\TaskScheduler; namespace ProcessMaker\TaskScheduler;
use Application; use Application;
use AppAssignSelfServiceValueGroupPeer;
use AppAssignSelfServiceValuePeer;
use AppDelegation; use AppDelegation;
use App\Jobs\TaskScheduler; use App\Jobs\TaskScheduler;
use Bootstrap; use Bootstrap;
@@ -13,6 +15,7 @@ use Exception;
use G; use G;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use ProcessMaker\Core\JobsManager; use ProcessMaker\Core\JobsManager;
use Propel;
use ResultSet; use ResultSet;
use SpoolRun; use SpoolRun;
@@ -62,14 +65,16 @@ class Task
public function setExecutionMessage(string $message) public function setExecutionMessage(string $message)
{ {
Log::channel('taskScheduler:taskScheduler')->info($message, Bootstrap::context()); Log::channel('taskScheduler:taskScheduler')->info($message, Bootstrap::context());
$len = strlen($message); if ($this->asynchronous === false) {
$linesize = 60; $len = strlen($message);
$rOffset = $linesize - $len; $linesize = 60;
$rOffset = $linesize - $len;
eprint("* $message"); eprint("* $message");
for ($i = 0; $i < $rOffset; $i++) { for ($i = 0; $i < $rOffset; $i++) {
eprint('.'); eprint('.');
}
} }
} }
@@ -93,7 +98,9 @@ class Task
$color = 'yellow'; $color = 'yellow';
Log::channel('taskScheduler:taskScheduler')->warning($message, Bootstrap::context()); Log::channel('taskScheduler:taskScheduler')->warning($message, Bootstrap::context());
} }
eprintln("[$message]", $color); if ($this->asynchronous === false) {
eprintln("[$message]", $color);
}
} }
/** /**
@@ -104,16 +111,20 @@ class Task
*/ */
public function saveLog(string $source, string $type, string $description) public function saveLog(string $source, string $type, string $description)
{ {
$context = [ if ($this->asynchronous === true) {
'type' => $type, $context = [
'description' => $description 'type' => $type,
]; 'description' => $description
Log::channel('taskScheduler:taskScheduler')->info($source, Bootstrap::context($context)); ];
try { Log::channel('taskScheduler:taskScheduler')->info($source, Bootstrap::context($context));
G::verifyPath(PATH_DATA . "log" . PATH_SEP, true); }
G::log("| $this->object | " . $source . " | $type | " . $description, PATH_DATA); if ($this->asynchronous === false) {
} catch (Exception $e) { try {
Log::channel('taskScheduler:taskScheduler')->error($e->getMessage(), Bootstrap::context($context)); G::verifyPath(PATH_DATA . "log" . PATH_SEP, true);
G::log("| $this->object | " . $source . " | $type | " . $description, PATH_DATA);
} catch (Exception $e) {
Log::channel('taskScheduler:taskScheduler')->error($e->getMessage(), Bootstrap::context($context));
}
} }
} }
@@ -174,10 +185,14 @@ class Task
if ($result->next()) { if ($result->next()) {
$this->setExecutionResultMessage("WARNING", "warning"); $this->setExecutionResultMessage("WARNING", "warning");
$message = "Emails won't be sent, but the cron will continue its execution"; $message = "Emails won't be sent, but the cron will continue its execution";
eprintln(" '-" . $message, "yellow"); if ($this->asynchronous === false) {
eprintln(" '-" . $message, "yellow");
}
} else { } else {
$this->setExecutionResultMessage("WITH ERRORS", "error"); $this->setExecutionResultMessage("WITH ERRORS", "error");
eprintln(" '-" . $e->getMessage(), "red"); if ($this->asynchronous === false) {
eprintln(" '-" . $e->getMessage(), "red");
}
} }
$this->saveLog("resendEmails", "error", "Error Resending Emails: " . $e->getMessage()); $this->saveLog("resendEmails", "error", "Error Resending Emails: " . $e->getMessage());
@@ -202,7 +217,9 @@ class Task
$this->saveLog('unpauseApplications', 'action', 'Unpausing Applications'); $this->saveLog('unpauseApplications', 'action', 'Unpausing Applications');
} catch (Exception $e) { } catch (Exception $e) {
$this->setExecutionResultMessage('WITH ERRORS', 'error'); $this->setExecutionResultMessage('WITH ERRORS', 'error');
eprintln(" '-" . $e->getMessage(), 'red'); if ($this->asynchronous === false) {
eprintln(" '-" . $e->getMessage(), 'red');
}
$this->saveLog('unpauseApplications', 'error', 'Error Unpausing Applications: ' . $e->getMessage()); $this->saveLog('unpauseApplications', 'error', 'Error Unpausing Applications: ' . $e->getMessage());
} }
}; };
@@ -223,7 +240,9 @@ class Task
$this->saveLog('calculateDuration', 'action', 'Calculating Duration'); $this->saveLog('calculateDuration', 'action', 'Calculating Duration');
} catch (Exception $e) { } catch (Exception $e) {
$this->setExecutionResultMessage('WITH ERRORS', 'error'); $this->setExecutionResultMessage('WITH ERRORS', 'error');
eprintln(" '-" . $e->getMessage(), 'red'); if ($this->asynchronous === false) {
eprintln(" '-" . $e->getMessage(), 'red');
}
$this->saveLog('calculateDuration', 'error', 'Error Calculating Duration: ' . $e->getMessage()); $this->saveLog('calculateDuration', 'error', 'Error Calculating Duration: ' . $e->getMessage());
} }
}; };
@@ -244,10 +263,49 @@ class Task
$this->saveLog('calculateDurationByApp', 'action', 'Calculating Duration by Application'); $this->saveLog('calculateDurationByApp', 'action', 'Calculating Duration by Application');
} catch (Exception $e) { } catch (Exception $e) {
$this->setExecutionResultMessage('WITH ERRORS', 'error'); $this->setExecutionResultMessage('WITH ERRORS', 'error');
eprintln(" '-" . $e->getMessage(), 'red'); if ($this->asynchronous === false) {
eprintln(" '-" . $e->getMessage(), 'red');
}
$this->saveLog('calculateDurationByApp', 'error', 'Error Calculating Duration: ' . $e->getMessage()); $this->saveLog('calculateDurationByApp', 'error', 'Error Calculating Duration: ' . $e->getMessage());
} }
}; };
$this->runTask($job); $this->runTask($job);
} }
/**
* Clean unused records in tables related to the Self-Service Value Based feature.
*/
public function cleanSelfServiceTables()
{
$job = function() {
try {
// Start message
$this->setExecutionMessage("Clean unused records for Self-Service Value Based feature");
// Get Propel connection
$cnn = Propel::getConnection(AppAssignSelfServiceValueGroupPeer::DATABASE_NAME);
// Delete related rows and missing relations, criteria don't execute delete with joins
$cnn->begin();
$stmt = $cnn->createStatement();
$stmt->executeQuery("DELETE " . AppAssignSelfServiceValueGroupPeer::TABLE_NAME . "
FROM " . AppAssignSelfServiceValueGroupPeer::TABLE_NAME . "
LEFT JOIN " . AppAssignSelfServiceValuePeer::TABLE_NAME . "
ON (" . AppAssignSelfServiceValueGroupPeer::ID . " = " . AppAssignSelfServiceValuePeer::ID . ")
WHERE " . AppAssignSelfServiceValuePeer::ID . " IS NULL");
$cnn->commit();
// Success message
$this->setExecutionResultMessage("DONE");
} catch (Exception $e) {
$cnn->rollback();
$this->setExecutionResultMessage("WITH ERRORS", "error");
if ($this->asynchronous === false) {
eprintln(" '-" . $e->getMessage(), "red");
}
$this->saveLog("ExecuteCleanSelfServiceTables", "error", "Error when try to clean self-service tables " . $e->getMessage());
}
};
$this->runTask($job);
}
} }