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,8 +57,14 @@ class TaskTest extends TestCase
$printing = ob_get_clean();
//assert if message is contained in output buffer
if ($asynchronous === false) {
$this->assertRegExp("/{$message}/", $printing);
}
//assert if not showing message
if ($asynchronous === true) {
$this->assertEmpty($printing);
}
}
/**
* This test verify the setExecutionResultMessage method.
@@ -76,20 +82,38 @@ class TaskTest extends TestCase
$task->setExecutionResultMessage($message, 'error');
$printing = ob_get_clean();
//assert if message is contained in output buffer
if ($asynchronous === false) {
$this->assertRegExp("/{$message}/", $printing);
}
//assert if not showing message
if ($asynchronous === true) {
$this->assertEmpty($printing);
}
ob_start();
$task->setExecutionResultMessage($message, 'info');
$printing = ob_get_clean();
//assert if message is contained in output buffer
if ($asynchronous === false) {
$this->assertRegExp("/{$message}/", $printing);
}
//assert if not showing message
if ($asynchronous === true) {
$this->assertEmpty($printing);
}
ob_start();
$task->setExecutionResultMessage($message, 'warning');
$printing = ob_get_clean();
//assert if message is contained in output buffer
if ($asynchronous === false) {
$this->assertRegExp("/{$message}/", $printing);
}
//assert if not showing message
if ($asynchronous === true) {
$this->assertEmpty($printing);
}
}
/**
* This test verify the saveLog method.
@@ -106,9 +130,15 @@ class TaskTest extends TestCase
$task->saveLog('', '', $description);
$file = PATH_DATA . "log/cron.log";
$this->assertFileExists($file);
if ($asynchronous === false) {
$contentLog = file_get_contents($file);
$this->assertRegExp("/{$description}/", $contentLog);
}
if ($asynchronous === true) {
$contentLog = file_get_contents($file);
$this->assertNotRegExp("/{$description}/", $contentLog);
}
}
/**
* This test verify the resendEmails activity method for synchronous and asynchronous execution.
@@ -222,4 +252,32 @@ class TaskTest extends TestCase
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();
executeUpdateAppTitle();
executeCaseSelfService();
cleanSelfServiceTables();
if (empty($argvx) || strpos($argvx, "clean-self-service-tables") !== false) {
$task->cleanSelfServiceTables();
}
executePlugins();
/*----------------------------------********---------------------------------*/
fillReportByUser();
@@ -824,46 +826,3 @@ function sendNotifications()
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;
use Application;
use AppAssignSelfServiceValueGroupPeer;
use AppAssignSelfServiceValuePeer;
use AppDelegation;
use App\Jobs\TaskScheduler;
use Bootstrap;
@@ -13,6 +15,7 @@ use Exception;
use G;
use Illuminate\Support\Facades\Log;
use ProcessMaker\Core\JobsManager;
use Propel;
use ResultSet;
use SpoolRun;
@@ -62,6 +65,7 @@ class Task
public function setExecutionMessage(string $message)
{
Log::channel('taskScheduler:taskScheduler')->info($message, Bootstrap::context());
if ($this->asynchronous === false) {
$len = strlen($message);
$linesize = 60;
$rOffset = $linesize - $len;
@@ -72,6 +76,7 @@ class Task
eprint('.');
}
}
}
/**
* Print result message in console.
@@ -93,8 +98,10 @@ class Task
$color = 'yellow';
Log::channel('taskScheduler:taskScheduler')->warning($message, Bootstrap::context());
}
if ($this->asynchronous === false) {
eprintln("[$message]", $color);
}
}
/**
* Save logs.
@@ -104,11 +111,14 @@ class Task
*/
public function saveLog(string $source, string $type, string $description)
{
if ($this->asynchronous === true) {
$context = [
'type' => $type,
'description' => $description
];
Log::channel('taskScheduler:taskScheduler')->info($source, Bootstrap::context($context));
}
if ($this->asynchronous === false) {
try {
G::verifyPath(PATH_DATA . "log" . PATH_SEP, true);
G::log("| $this->object | " . $source . " | $type | " . $description, PATH_DATA);
@@ -116,6 +126,7 @@ class Task
Log::channel('taskScheduler:taskScheduler')->error($e->getMessage(), Bootstrap::context($context));
}
}
}
/**
* This resend the emails.
@@ -174,11 +185,15 @@ class Task
if ($result->next()) {
$this->setExecutionResultMessage("WARNING", "warning");
$message = "Emails won't be sent, but the cron will continue its execution";
if ($this->asynchronous === false) {
eprintln(" '-" . $message, "yellow");
}
} else {
$this->setExecutionResultMessage("WITH ERRORS", "error");
if ($this->asynchronous === false) {
eprintln(" '-" . $e->getMessage(), "red");
}
}
$this->saveLog("resendEmails", "error", "Error Resending Emails: " . $e->getMessage());
}
@@ -202,7 +217,9 @@ class Task
$this->saveLog('unpauseApplications', 'action', 'Unpausing Applications');
} catch (Exception $e) {
$this->setExecutionResultMessage('WITH ERRORS', 'error');
if ($this->asynchronous === false) {
eprintln(" '-" . $e->getMessage(), 'red');
}
$this->saveLog('unpauseApplications', 'error', 'Error Unpausing Applications: ' . $e->getMessage());
}
};
@@ -223,7 +240,9 @@ class Task
$this->saveLog('calculateDuration', 'action', 'Calculating Duration');
} catch (Exception $e) {
$this->setExecutionResultMessage('WITH ERRORS', 'error');
if ($this->asynchronous === false) {
eprintln(" '-" . $e->getMessage(), 'red');
}
$this->saveLog('calculateDuration', 'error', 'Error Calculating Duration: ' . $e->getMessage());
}
};
@@ -244,10 +263,49 @@ class Task
$this->saveLog('calculateDurationByApp', 'action', 'Calculating Duration by Application');
} catch (Exception $e) {
$this->setExecutionResultMessage('WITH ERRORS', 'error');
if ($this->asynchronous === false) {
eprintln(" '-" . $e->getMessage(), 'red');
}
$this->saveLog('calculateDurationByApp', 'error', 'Error Calculating Duration: ' . $e->getMessage());
}
};
$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);
}
}