From 8126665acda9db590bd55906bb54bca3e4624d5b Mon Sep 17 00:00:00 2001 From: Paolo Date: Tue, 23 Jun 2026 12:33:44 +0200 Subject: [PATCH] Improvements and updated documentation --- application/config/lrt.php | 2 +- .../controllers/jobs/LongRunTaskExecJob.php | 6 +++- application/controllers/lrts/LRTDummy.php | 6 ++-- application/core/LRT_Controller.php | 34 +++++++------------ application/libraries/LongRunTaskLib.php | 28 ++++++++++++--- 5 files changed, 46 insertions(+), 30 deletions(-) diff --git a/application/config/lrt.php b/application/config/lrt.php index 64367a3da..42081550d 100644 --- a/application/config/lrt.php +++ b/application/config/lrt.php @@ -13,7 +13,7 @@ $config['lrt_max_number_single_user'] = 10; $config['lrt_max_number_system'] = 100; // Maximum running time in hours for a single LRT before killing it -$config['lrt_max_run_timeout'] = 48; +$config['lrt_max_run_timeout'] = 24; // List of existing LRT types $config['lrt_types'] = array('LRTDummy'); diff --git a/application/controllers/jobs/LongRunTaskExecJob.php b/application/controllers/jobs/LongRunTaskExecJob.php index 629e1418f..3541876de 100644 --- a/application/controllers/jobs/LongRunTaskExecJob.php +++ b/application/controllers/jobs/LongRunTaskExecJob.php @@ -9,7 +9,7 @@ if (!defined("BASEPATH")) exit("No direct script access allowed"); * - This is a Job Queue Worker that gets scheduled LRTs from the queue and executes them * - Once all the LRTs have been started checks if there are LRTs that are running for too long and kills them */ -class LongRunTaskExecJob extends JQW_Controller +class LongRunTaskExecJob extends JOB_Controller { /** * Constructor @@ -72,6 +72,10 @@ class LongRunTaskExecJob extends JQW_Controller { // Kill the process with a SIGKILL exec('kill -9 '.$lrt->pid.' > /dev/null 2>&1'); + + // Set the LRT as failed + $lrtExecFailedResult = $this->longruntasklib->lrtExecFailed($lrt->jobid); + if (isError($lrtExecFailedResult)) $this->logError(getError(lrtExecFailedResult)); } } diff --git a/application/controllers/lrts/LRTDummy.php b/application/controllers/lrts/LRTDummy.php index fdbfe7a93..cb918ee62 100644 --- a/application/controllers/lrts/LRTDummy.php +++ b/application/controllers/lrts/LRTDummy.php @@ -21,7 +21,7 @@ class LRTDummy extends LRT_Controller $this->_jobid = $jobid; // Get the LRT record related to the provided jobid - $lrtResult = $this->getLrt($jobid); + $lrtResult = $this->getLrt(); // If an error occurred or the record has not been found if (isError($lrtResult) || !hasData($lrtResult)) @@ -48,7 +48,7 @@ class LRTDummy extends LRT_Controller { sleep(1); // Set the progress - $setProgressResult = $this->setProgress($jobid, (($i + 1) / (int)$input->sleep) * 100); + $setProgressResult = $this->setProgress((($i + 1) / (int)$input->sleep) * 100); if (isError($setProgressResult)) { $this->logError($setProgressResult); @@ -62,7 +62,7 @@ class LRTDummy extends LRT_Controller $this->logInfo('The user '.$lrt->uid.' slept for '.$input->sleep.' seconds'); // Set the output - $setOutputResult = $this->setOutput($jobid, 'The user '.$lrt->uid.' slept for '.$input->sleep.' seconds'); + $setOutputResult = $this->setOutput('The user '.$lrt->uid.' slept for '.$input->sleep.' seconds'); if (isError($setOutputResult)) { $this->logError($setOutputResult); diff --git a/application/core/LRT_Controller.php b/application/core/LRT_Controller.php index 357f981b9..fe964945c 100644 --- a/application/core/LRT_Controller.php +++ b/application/core/LRT_Controller.php @@ -13,7 +13,7 @@ if (!defined("BASEPATH")) exit("No direct script access allowed"); * - Any implementation of a Long Run Task should extends this class to * properly operate with the LRT system */ -abstract class LRT_Controller extends JQW_Controller +abstract class LRT_Controller extends JOB_Controller { protected $_jobid; // record id for this LRT @@ -43,20 +43,12 @@ abstract class LRT_Controller extends JQW_Controller */ public function __destruct() { - // Check if the property has been set - if ($this->_jobid == null) - { - $this->logError( - 'The method '.get_class($this).'->run must assign the value of the jobid parameter to the property _jobid at the very beginning: $this->_jobid = $jobid;' - ); - } - else // If set then - { - // Set the status of this LRT as done - $setStatusResult = $this->longruntasklib->setStatus($this->_jobid, LongRunTaskLib::STATUS_DONE); - // If an error occurred then log it - if (isError($setStatusResult)) $this->logError($setStatusResult); - } + // Sends email to the user + + // Set the status and the endtime of this LRT as done + $lrtExecOverResult = $this->longruntasklib->lrtExecOver($this->_jobid); + // If an error occurred then log it + if (isError($lrtExecOverResult)) $this->logError($lrtExecOverResult); } //------------------------------------------------------------------------------------------------------------------ @@ -70,25 +62,25 @@ abstract class LRT_Controller extends JQW_Controller /** * */ - protected function getLrt($jobid) + protected function getLrt() { - return $this->longruntasklib->getLrt($jobid); + return $this->longruntasklib->getLrt($this->_jobid); } /** * */ - protected function setProgress($jobid, $progress) + protected function setProgress($progress) { - return $this->longruntasklib->setProgress($jobid, $progress); + return $this->longruntasklib->setProgress($this->_jobid, $progress); } /** * */ - protected function setOutput($jobid, $output) + protected function setOutput($output) { - return $this->longruntasklib->setOutuput($jobid, $output); + return $this->longruntasklib->setOutuput($this->_jobid, $output); } } diff --git a/application/libraries/LongRunTaskLib.php b/application/libraries/LongRunTaskLib.php index 1ead62349..46e1f5a97 100644 --- a/application/libraries/LongRunTaskLib.php +++ b/application/libraries/LongRunTaskLib.php @@ -52,7 +52,7 @@ class LongRunTaskLib extends JobsQueueLib } /** - * + * Get all the LRT that are running more then CFG_LRT_MAX_RUN hours */ public function getHangingLRTs() { @@ -109,6 +109,20 @@ class LongRunTaskLib extends JobsQueueLib if (isError($updateLrtResult)) return $updateLrtResult; } + /** + * Set the LRT in the queue as failed + */ + public function lrtExecFailed($jobid) + { + return $this->_ci->JobsQueueModel->update( + $jobid, + array( + 'endtime' => 'NOW()', + 'status' => self::STATUS_FAILED + ) + ); + } + //------------------------------------------------------------------------------------------------------------------ // Public methods used by the front end applications (standard controllers/end points, ex. controllers/system/LRTTest.php) @@ -209,11 +223,17 @@ class LongRunTaskLib extends JobsQueueLib } /** - * + * Set the LRT in the queue as successfully ended */ - public function setStatus($jobid, $status) + public function lrtExecOver($jobid) { - return $this->_ci->JobsQueueModel->update($jobid, array('status' => $status)); + return $this->_ci->JobsQueueModel->update( + $jobid, + array( + 'endtime' => 'NOW()', + 'status' => self::STATUS_DONE + ) + ); } }