Refactored main components

This commit is contained in:
Paolo
2026-06-23 14:59:34 +02:00
parent 8126665acd
commit c40ee917df
4 changed files with 209 additions and 101 deletions
+61 -56
View File
@@ -9,69 +9,74 @@ if (!defined("BASEPATH")) exit("No direct script access allowed");
class LRTDummy extends LRT_Controller
{
/**
* Loops on the number of seconds provided by the LRT input
* Sleeps every time 1 sec
* Writes the progress
* Writes the output
* This method must be implemented!
*/
public function run($jobid)
{
$this->logInfo('Long run tasks '.get_class($this).' started');
$this->_jobid = $jobid;
// Get the LRT record related to the provided jobid
$lrtResult = $this->getLrt();
// If an error occurred or the record has not been found
if (isError($lrtResult) || !hasData($lrtResult))
{
$this->logError($lrtResult);
}
else
{
// Get the record
$lrt = getData($lrtResult)[0];
// Get and check the input
$input = json_decode($lrt->input);
if ($input == null)
{
$this->logError('LRT input is not a valid json');
}
else
{
$error = false; // be optimistic
// Operation
for ($i = 0; $i < (int)$input->sleep; $i++)
{
sleep(1);
// Set the progress
$setProgressResult = $this->setProgress((($i + 1) / (int)$input->sleep) * 100);
if (isError($setProgressResult))
{
$this->logError($setProgressResult);
$error = true;
}
}
// If no errors
if (!$error)
{
$this->logInfo('The user '.$lrt->uid.' slept for '.$input->sleep.' seconds');
// Set the output
$setOutputResult = $this->setOutput('The user '.$lrt->uid.' slept for '.$input->sleep.' seconds');
if (isError($setOutputResult))
{
$this->logError($setOutputResult);
}
}
}
}
$this->_doIt($jobid);
$this->logInfo('Long run tasks '.get_class($this).' ended');
}
/**
* Loops on the number of seconds provided by the LRT input
* Sleeps every time 1 sec
* Writes the progress
* Writes the output
*/
private function _doIt($jobid)
{
// Get the LRT record related to the provided jobid
$lrtResult = $this->getLrt($jobid);
// If an error occurred or the record has not been found
if (isError($lrtResult))
{
$this->logError($lrtResult);
return;
}
if (!hasData($lrtResult))
{
$this->logError('LRT not found in database');
return;
}
// Get the record
$lrt = getData($lrtResult)[0];
// Get and check the input
$input = json_decode($lrt->{LongRunTaskLib::PROPERTY_INPUT});
if ($input == null)
{
$this->logError('LRT input is not a valid json');
return;
}
// Operation
for ($i = 0; $i < (int)$input->sleep; $i++)
{
sleep(1);
// Set the progress
$setProgressResult = $this->setProgress($jobid, (($i + 1) / (int)$input->sleep) * 100);
if (isError($setProgressResult))
{
$this->logError($setProgressResult);
return;
}
}
$sleepMsg = 'The user '.$lrt->{LongRunTaskLib::PROPERTY_UID}.' slept for '.$input->sleep.' seconds';
$this->logInfo($sleepMsg);
// Set the output
$setOutputResult = $this->setOutput($jobid, $sleepMsg);
if (isError($setOutputResult))
{
$this->logError($setOutputResult);
}
}
}