application/controllers/jobs/LongRunTaskExecJob.php now provides a way to kill hanging LRTs

This commit is contained in:
Paolo
2026-06-22 16:24:51 +02:00
parent 31410f0bb9
commit 4e9bf3676d
2 changed files with 56 additions and 6 deletions
+32 -6
View File
@@ -12,6 +12,7 @@ class LongRunTaskLib extends JobsQueueLib
// Config names
const CFG_LRT_MAX_NUMBER_SYSTEM = 'lrt_max_number_system';
const CFG_LRT_TYPES = 'lrt_types';
const CFG_LRT_MAX_RUN = 'lrt_max_run_timeout';
// LRT object properties
const PROPERTY_UID = 'uid';
@@ -49,6 +50,26 @@ class LongRunTaskLib extends JobsQueueLib
return $this->getOldestJobs($this->_ci->config->item(self::CFG_LRT_TYPES), $max_number_of_lrts);
}
/**
*
*/
public function getHangingLRTs()
{
// Return the result of the query
return $this->_ci->JobsQueueModel->execReadOnlyQuery('
SELECT jq.*
FROM system.tbl_jobsqueue jq
WHERE jq.type IN ?
AND jq.status = ?
AND jq.starttime < NOW() - INTERVAL \''.$this->_ci->config->item(self::CFG_LRT_MAX_RUN).' hours\'
ORDER BY jq.creationtime DESC',
array(
$this->_ci->config->item(self::CFG_LRT_TYPES),
JobsQueueLib::STATUS_RUNNING
)
);
}
/**
* Execute a LRT in background
* - Checks if the wanted LRT exists in the applcation/controllers/lrts directory
@@ -58,7 +79,6 @@ class LongRunTaskLib extends JobsQueueLib
public function executeLrt($lrt)
{
$output = array();
$return_var = -1;
// If does _not_ exist a LRT implementation for this LRT type, then return an error
if (!file_exists(APPPATH.'controllers/lrts/'.$lrt->{self::PROPERTY_TYPE}.'.php'))
@@ -70,16 +90,22 @@ class LongRunTaskLib extends JobsQueueLib
exec(
// Command
'/usr/bin/php '.APPPATH.'../index.ci.php lrts/'.$lrt->{self::PROPERTY_TYPE}.'/run '.$lrt->{self::PROPERTY_JOBID}.' > /dev/null 2>&1 & echo $!',
$output, // Here goes the output from the standard output and error
$return_var // Status of the command once executed (== 0 success, !=0 error)
$output // Here goes the output from the standard output and error
);
// If a pid has not been returned
if (isEmptyArray($output) || !is_numeric($output[0])) return error('Not a valid pid has been returned');
// Set the pid of this LRT into the database
$pidResult = $this->_ci->JobsQueueModel->update($lrt->{self::PROPERTY_JOBID}, array('pid' => $output[0]));
if (isError($pidResult)) return $pidResult;
// Set the pid, status and starttime of this LRT into the database
$updateLrtResult = $this->_ci->JobsQueueModel->update(
$lrt->{self::PROPERTY_JOBID},
array(
'pid' => $output[0],
'starttime' => 'NOW()',
'status' => self::STATUS_RUNNING
)
);
if (isError($updateLrtResult)) return $updateLrtResult;
}
//------------------------------------------------------------------------------------------------------------------