From be7ea871be8959da83763e8df05f6a6c0fb0e471 Mon Sep 17 00:00:00 2001 From: Paolo Date: Wed, 22 Jul 2020 00:56:52 +0200 Subject: [PATCH] - Added new config entry job_schedulers_list to config/jqm.php - Library JobsQueueLib: - Added constant JOB_SCHEDULERS_LIST - Constructor now loads the jqm configs - Added new public method schedule --- application/config/jqm.php | 9 +++ .../system/jq/JobsQueueScheduler.php | 47 +++++++++++++ application/core/JQW_Controller.php | 1 + application/libraries/JobsQueueLib.php | 69 +++++++++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 application/controllers/system/jq/JobsQueueScheduler.php diff --git a/application/config/jqm.php b/application/config/jqm.php index 77d9bb35d..d7e0a3525 100644 --- a/application/config/jqm.php +++ b/application/config/jqm.php @@ -11,3 +11,12 @@ $config['job_type_permissions_white_list'] = array( 'OEHPayment' => 'developer:rw', 'SAPPayment' => 'developer:rw' ); + +// List of schedulers that can be run by the scheduler +$config['job_schedulers_list'] = array( // Controllers + 'extensions/FHC-Core-SAP/JBMScheduler' => array( // Methods + 'newUsers', + 'updateUsers' + ) +); + diff --git a/application/controllers/system/jq/JobsQueueScheduler.php b/application/controllers/system/jq/JobsQueueScheduler.php new file mode 100644 index 000000000..157b8979f --- /dev/null +++ b/application/controllers/system/jq/JobsQueueScheduler.php @@ -0,0 +1,47 @@ +logInfo('Scheduler started'); + + $schedulerResult = $this->jobsqueuelib->schedule(); + + // If error occurred then log it + if (isError($schedulerResult)) $this->logError(getError($schedulerResult)); + + // If non blocking errors occurred log them + if (hasData($schedulerResult) && !isEmptyArray(getData($schedulerResult))) + { + foreach (getData($schedulerResult) as $nonBlockingError) + { + $this->logWarning($nonBlockingError); + } + } + + $this->logInfo('Scheduler ended'); + } +} + diff --git a/application/core/JQW_Controller.php b/application/core/JQW_Controller.php index c6d2362e3..01ae99805 100644 --- a/application/core/JQW_Controller.php +++ b/application/core/JQW_Controller.php @@ -75,3 +75,4 @@ abstract class JQW_Controller extends JOB_Controller return $result; } } + diff --git a/application/libraries/JobsQueueLib.php b/application/libraries/JobsQueueLib.php index 2213315f8..0e74f6605 100644 --- a/application/libraries/JobsQueueLib.php +++ b/application/libraries/JobsQueueLib.php @@ -24,6 +24,9 @@ class JobsQueueLib const PROPERTY_END_TIME = 'endtime'; const PROPERTY_ERROR = 'error'; + // Config entries + const JOB_SCHEDULERS_LIST = 'job_schedulers_list'; + private $_ci; // CI instance /** @@ -39,6 +42,9 @@ class JobsQueueLib $this->_ci->load->model('system/JobTypes_model', 'JobTypesModel'); $this->_ci->load->model('system/JobStatuses_model', 'JobStatusesModel'); $this->_ci->load->model('system/JobTriggers_model', 'JobTriggersModel'); + + // Loads configs + $this->_ci->config->load('jqm'); } //------------------------------------------------------------------------------------------------------------------ @@ -196,6 +202,68 @@ class JobsQueueLib return success($results); // otherwise return results in a success object } + /** + * Checks the job object structure when needed for insert + */ + public function schedule() + { + // An array of non blocking errors + $nonBlockingErrorsArray = array(); + + // Get the all the configured schedulers + $schedulers = $this->_ci->config->item(self::JOB_SCHEDULERS_LIST); + + // If is it empty then non blocking error + if (isEmptyArray($schedulers)) return success('No schedulers where configured'); + + // For each scheduler + foreach ($schedulers as $controller => $methods) + { + // If the specified controller does not exist + if (!file_exists(APPPATH.'controllers/'.$controller.'.php')) + { + $nonBlockingErrorsArray[] = 'The controller '.$controller.' does not exist'; + } + else // try to call it + { + $arrayMethods = array(); + + // If methods is a string + if (is_string($methods)) + { + $arrayMethods[] = $methods; + } + elseif (is_array($methods)) // otherwise if is an array + { + $arrayMethods = $methods; + } + + // For eache configured method + foreach ($arrayMethods as $method) + { + // Generate command string + $command = sprintf( + 'php %s/../index.ci.php %s %s >> %s/logs/scheduler-%s-%s.log 2>>%s/logs/scheduler-error-%s-%s.log &', + APPPATH, + $controller, + $method, + APPPATH, + str_replace('/', '-', $controller), + date('Y-m-d'), + APPPATH, + str_replace('/', '-', $controller), + date('Y-m-d') + ); + + // Execute command string + @shell_exec($command); + } + } + } + + return success($nonBlockingErrorsArray); + } + //------------------------------------------------------------------------------------------------------------------ // Private methods @@ -355,3 +423,4 @@ class JobsQueueLib return success(); // if here then it was a success! } } +