From aba680cd5216f245bde2fb6cd74231f36cab4fb8 Mon Sep 17 00:00:00 2001 From: Paolo Date: Tue, 28 Apr 2026 16:53:25 +0200 Subject: [PATCH] Long Run Tasks first draft --- application/config/lrt.php | 20 +++++ application/core/LRT_Controller.php | 73 +++++++++++++++++++ application/libraries/JobsQueueLib.php | 6 +- application/libraries/LongRunTaskLib.php | 66 +++++++++++++++++ system/dbupdate_3.4.php | 3 +- .../dbupdate_3.4/76203_Asynchrone_Tasks.php | 59 +++++++++++++++ 6 files changed, 224 insertions(+), 3 deletions(-) create mode 100644 application/config/lrt.php create mode 100644 application/core/LRT_Controller.php create mode 100644 application/libraries/LongRunTaskLib.php create mode 100644 system/dbupdate_3.4/76203_Asynchrone_Tasks.php diff --git a/application/config/lrt.php b/application/config/lrt.php new file mode 100644 index 000000000..504f02d9a --- /dev/null +++ b/application/config/lrt.php @@ -0,0 +1,20 @@ +LogLibJob->setConfigs( + array( + 'dbExecuteUser' => 'LRTs queue system', + 'requestId' => 'LTR' + ) + ); + + // Loads LongRunTaskLib library + $this->load->library('LongRunTaskLib'); + } + + //------------------------------------------------------------------------------------------------------------------ + // Protected methods + + /** + * Get the oldest added LRTs to the queue having status = new + */ + protected function getLRTs() + { + $lrts = $this->longruntasklib->getLRTs(); + + // If an error occurred then log it in database + if (isError($lrts)) $this->logError(getError($lrts)); + + return $lrts; + } + + /** + * + */ + protected function addNewLRTsToQueue($type, $lrts) + { + $result = $this->longruntasklib->addNewLRTsToQueue($type, $lrts); + + // If an error occurred then log it in database + if (isError($result)) $this->logError(getError($result), $type); + + return $result; + } + + /** + * Utility method to generate a job with the given parameters and return it inside an array + * ready to be used by addNewJobsToQueue and updateJobsQueue + */ + protected function generateJobs($status, $input) + { + return JobsQueueLib::generateJobs($status, $input); + } +} + diff --git a/application/libraries/JobsQueueLib.php b/application/libraries/JobsQueueLib.php index 5871a3767..128a08994 100644 --- a/application/libraries/JobsQueueLib.php +++ b/application/libraries/JobsQueueLib.php @@ -2,6 +2,8 @@ if (!defined('BASEPATH')) exit('No direct script access allowed'); +use \stdClass as stdClass; + /** * Library that contains all the needed functionalities to operate with the Jobs Queue System */ @@ -24,12 +26,12 @@ class JobsQueueLib const PROPERTY_END_TIME = 'endtime'; const PROPERTY_ERROR = 'error'; - private $_ci; // CI instance + protected $_ci; // CI instance /** * Constructor */ - public function __construct($authenticate = true) + public function __construct() { // Gets CI instance $this->_ci =& get_instance(); diff --git a/application/libraries/LongRunTaskLib.php b/application/libraries/LongRunTaskLib.php new file mode 100644 index 000000000..4dded363b --- /dev/null +++ b/application/libraries/LongRunTaskLib.php @@ -0,0 +1,66 @@ +_ci->config->load('lrt'); + } + + //------------------------------------------------------------------------------------------------------------------ + // Public methods + + /** + * Get the oldest added LRTs to the queue having status = new + * The maximum number of returned queued LRTs is limited by: + * number of currently running LRTs - maximum allowed number of LRTs for the system + */ + public function getLRTs() + { + // Get all the running LRTs + $runningLrtsResult = $this->getJobsByTypeStatus($this->_ci->config->item(self::CFG_LRT_TYPES)), JobsQueueLib::STATUS_RUNNING); + + if (isError($runningLrtsResult)) return $runningLrtsResult; + + // The number of the currently running LRTs - the maximum LRTs for the system + $max_number_of_lrts = $this->_ci->config->item(self::CFG_LRT_MAX_NUMBER_SYSTEM)) - count(getData($runningLrtsResult)); + + // Get the oldest LRTs added to the queue + return $this->getOldestJobs($this->_ci->config->item(self::CFG_LRT_TYPES)), $max_number_of_lrts); + } + + /** + * + */ + public function addNewLRTsToQueue($type, $lrts) + { + return $this->addNewJobsToQueue($type, $lrts); + } + + //------------------------------------------------------------------------------------------------------------------ + // Public static methods + + /** + * Utility method to generate a LTR with the given parameters and return it inside an array + * ready to be used by addNewLRTsToQueue + */ + public static function generateLtrs($status, $input) + { + return self::generateJobs($status, $input); + } +} + diff --git a/system/dbupdate_3.4.php b/system/dbupdate_3.4.php index 8b6af9f2d..292f9409e 100644 --- a/system/dbupdate_3.4.php +++ b/system/dbupdate_3.4.php @@ -94,6 +94,7 @@ require_once('dbupdate_3.4/71399_dashboard_update_widget_paths.php'); require_once('dbupdate_3.4/71645_studvw_messagetab_ladezeit.php'); require_once('dbupdate_3.4/71566_studienordnungsdokument_neuer_organisationseinheitstyp_programm.php'); require_once('dbupdate_3.4/70376_lohnguide.php'); +require_once('dbupdate_3.4/76203_Asynchrone_Tasks.php'); // *** Pruefung und hinzufuegen der neuen Attribute und Tabellen echo '

Pruefe Tabellen und Attribute!

'; @@ -429,7 +430,7 @@ $tabellen=array( "system.tbl_log" => array("log_id","person_id","zeitpunkt","app","oe_kurzbz","logtype_kurzbz","logdata","insertvon","taetigkeit_kurzbz"), "system.tbl_logtype" => array("logtype_kurzbz", "data_schema"), "system.tbl_filters" => array("filter_id","app","dataset_name","filter_kurzbz","person_id","description","sort","default_filter","filter","oe_kurzbz","statistik_kurzbz"), - "system.tbl_jobsqueue" => array("jobid", "type", "creationtime", "status", "input", "output", "starttime", "endtime", "insertvon", "insertamum"), + "system.tbl_jobsqueue" => array("jobid", "type", "creationtime", "status", "input", "output", "starttime", "endtime", "insertvon", "insertamum", "pid", "uid", "progress"), "system.tbl_jobstatuses" => array("status"), "system.tbl_jobtriggers" => array("type", "status", "following_type"), "system.tbl_jobtypes" => array("type", "description"), diff --git a/system/dbupdate_3.4/76203_Asynchrone_Tasks.php b/system/dbupdate_3.4/76203_Asynchrone_Tasks.php new file mode 100644 index 000000000..67a358b87 --- /dev/null +++ b/system/dbupdate_3.4/76203_Asynchrone_Tasks.php @@ -0,0 +1,59 @@ +db_query('SELECT "pid" FROM "system"."tbl_jobsqueue" LIMIT 1')) +{ + $qry = 'ALTER TABLE "system"."tbl_jobsqueue" ADD "pid" INT NULL DEFAULT 0;'; + if (!$db->db_query($qry)) + echo 'system.tbl_jobsqueue: '.$db->db_last_error().'
'; + else + echo '
Added column pid to table system.tbl_jobsqueue'; +} + +// Add column uid to system.tbl_jobsqueue +if (!$result = @$db->db_query('SELECT "uid" FROM "system"."tbl_jobsqueue" LIMIT 1')) +{ + $qry = 'ALTER TABLE "system"."tbl_jobsqueue" ADD "uid" VARCHAR(32) NULL DEFAULT NULL;'; + if (!$db->db_query($qry)) + echo 'system.tbl_jobsqueue: '.$db->db_last_error().'
'; + else + echo '
Added column uid to table system.tbl_jobsqueue'; +} + +// Add column progress to system.tbl_jobsqueue +if (!$result = @$db->db_query('SELECT "progress" FROM "system"."tbl_jobsqueue" LIMIT 1')) +{ + $qry = 'ALTER TABLE "system"."tbl_jobsqueue" ADD "progress" NUMERIC(2,1) NULL DEFAULT 0;'; + if (!$db->db_query($qry)) + echo 'system.tbl_jobsqueue: '.$db->db_last_error().'
'; + else + echo '
Added column progress to table system.tbl_jobsqueue'; +} + +// Add foreign key fk_jobsqueue_benutzer_uid on system.tbl_jobsqueue.uid with public.tbl_benutzer.uid +if ($result = $db->db_query("SELECT conname FROM pg_constraint WHERE conname = 'fk_jobsqueue_benutzer_uid'")) +{ + if ($db->db_num_rows($result) == 0) + { + $qry = 'ALTER TABLE "system"."tbl_jobsqueue" ADD CONSTRAINT "fk_jobsqueue_benutzer_uid" FOREIGN KEY ("uid") REFERENCES "public"."tbl_benutzer" ("uid") ON DELETE RESTRICT ON UPDATE CASCADE;'; + if (!$db->db_query($qry)) + echo 'system.tbl_jobsqueue: '.$db->db_last_error().'
'; + else + echo '
Created foreign key fk_jobsqueue_benutzer_uid'; + } +} + +// Add new webservice type in system.tbl_webservicetyp +if ($result = @$db->db_query("SELECT 1 FROM system.tbl_webservicetyp WHERE webservicetyp_kurzbz = 'lrt';")) +{ + if ($db->db_num_rows($result) == 0) + { + $qry = "INSERT INTO system.tbl_webservicetyp(webservicetyp_kurzbz, beschreibung) VALUES('lrt', 'Long Run Task');"; + + if (!$db->db_query($qry)) + echo 'system.tbl_webservicetyp '.$db->db_last_error().'
'; + else + echo ' system.tbl_webservicetyp: Added webservice type "lrt"
'; + } +} +