diff --git a/application/controllers/jobs/LongRunTaskExecJob.php b/application/controllers/jobs/LongRunTaskExecJob.php index 5493dc205..629e1418f 100644 --- a/application/controllers/jobs/LongRunTaskExecJob.php +++ b/application/controllers/jobs/LongRunTaskExecJob.php @@ -53,5 +53,29 @@ class LongRunTaskExecJob extends JQW_Controller $this->logInfo('Execute long run tasks ended'); } + + /** + * + */ + public function killHangingLRTs() + { + $this->logInfo('Kill hanging LRTs started'); + + // Get all the LRTs that is possible to execute now + $lrtsResult = $this->longruntasklib->getHangingLRTs(); + if (isError($lrtsResult)) return $lrtsResult; + + if (hasData($lrtsResult)) + { + // For each LRT + foreach (getData($lrtsResult) as $lrt) + { + // Kill the process with a SIGKILL + exec('kill -9 '.$lrt->pid.' > /dev/null 2>&1'); + } + } + + $this->logInfo('Kill hanging LRTs ended'); + } } diff --git a/application/libraries/LongRunTaskLib.php b/application/libraries/LongRunTaskLib.php index 1827bfb25..da4814217 100644 --- a/application/libraries/LongRunTaskLib.php +++ b/application/libraries/LongRunTaskLib.php @@ -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; } //------------------------------------------------------------------------------------------------------------------