/mls-project/php/Standardization/DeliveryScheduler.php
PHP | 306 lines | 178 code | 22 blank | 106 comment | 29 complexity | a952508b2253080c06bd2b829f9dfdcc MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.0
- <?php
- /**
- * MLS Factory Data Standardization
- * Class DeliveryScheduler
- * Schedule different rets server class for delivery
- * @category Data_Standardization
- * @package Data_Standardization_DeliveryScheduler
- * @version 1.0
- * @author Amrutha Viswanath <incubator319@hotmail.com>
- * @date 22-NOV-2016
- */
- Class DeliveryScheduler
- {
- /*
- * Database connection variable
- * @var object
- */
- private $_dbCron;
-
- /*
- * Maximum number of php extraction scripts allowed
- * @var integer
- */
- private $_maxNoOfProcesses;
-
- /*
- * Mimimum amount of RAM needed to run a php extraction script
- * @var integer
- */
- private $_minRam;
-
- /*
- * GMT time when the scheduler started
- * @var datetime
- */
- private $_schedulerStartedgmtTime;
-
- /*
- * Maximum waiting time for the scheduler in minutes
- * @var integer
- */
- private $_waitimeTimeLimitMinutes;
-
- /*
- * Already scheduled crons array
- * @var array
- */
- private $_ranServerCrons = array(0);
-
- /*
- * Already scheduled classes array
- * @var array
- */
- private $_ranServerClasses = array(0);
-
- /*
- * Mysql where condition to apply the cron flag filter
- * @var String
- */
- private $_sqlWhereCondition;
-
- /**
- * DeliveryScheduler construct
- * Constructor, Initilizes variables _dbCron
- * @param Integer $cronflag Flag to identify the servers to run in that particular cron
- * @author Sanjay <incubator228@hotmail.com>
- * @date 06-AUG-2014
- * @modified Amrutha Viswanath <incubator319@hotmail.com>
- * @date 22-NOV-2016
- */
- public function __construct($cronflag = 0)
- {
- Common::displayMessages(' + Scheduler started');
- Common::displayMessages(" + Establishing database(cron) connection");
- $configArr = parse_ini_file("config/config.ini", true);
- $this->_dbCron = new Db('cron');
- Common::displayMessages(' + Established DB connection');
- if ($cronflag) {
- $this->_sqlWhereCondition = " AND s.incremental_server_flag = '$cronflag' ";
- }
- $this->_minRam = defined('MIN_RAM') ? MIN_RAM : 20480;
- $this->_maxNoOfProcesses = defined('MAX_NO_PROCESSES') ? MAX_NO_PROCESSES : 50;
- $this->_waitimeTimeLimitMinutes = defined('SCHEDULER_MAX_WAITING_TIME') ? SCHEDULER_MAX_WAITING_TIME : 14;
- $this->_schedulerStartedgmtTime = gmdate('Y-m-d H:i:s');
- $this->_schedulerStartedgmtTimeObj = new DateTime($this->_schedulerStartedgmtTime);
- $this->updateAllStoppedProcesses();
- //exit;
- $this->runScheduler();
- $this->_dbCron = null;
- }
-
- /**
- * DeliveryScheduler updateAllStoppedProcesses
- * update all stopped processes's cron status to "cron updated" so that it will taken for next delivery
- * @author Amrutha Viswanath <incubator319@hotmail.com>
- * @date 22-NOV-2016
- */
- private function updateAllStoppedProcesses()
- {
- $notExistProcessesStatusIdArr = array();
-
- $sql = "SELECT s3.id as monitorId, s3.process_id
- FROM s3_monitor s3
- INNER JOIN tbl_server s
- ON s3.flag = 'UPLOAD STARTED'
- AND s3.server_id = s.id
- $this->_sqlWhereCondition";
- if ($result = $this->_dbCron->executeQuery($sql)) {
- while ($row = mysqli_fetch_assoc($result)) {
- if (!file_exists("/proc/{$row['process_id']}")) { // if process not exists
- $notExistProcessesStatusIdArr[] = $row['monitorId'];
- }
- } // end while
- if (!empty ($notExistProcessesStatusIdArr)) {
- echo "updated " . count($notExistProcessesStatusIdArr) . " classes cron status as cron updated\n";
- $sqlUpdateStatus = "UPDATE s3_monitor
- SET flag = 'CRON UPDATED'
- WHERE id IN (". implode(',', $notExistProcessesStatusIdArr) .")";
- $this->_dbCron->executeNonQuery($sqlUpdateStatus);
- } // end if
- } // end if
- }
-
- /**
- * DeliveryScheduler runScheduler
- * Schedules the active rets server classes which is ready to run
- * @author Sanjay <incubator228@hotmail.com>
- * @date 06-AUG-2014
- */
- public function runScheduler()
- {
- $showMsg = true;
- $iterator = 1;
- $firstTime = true;
- while (true) { // inifinite loop
- if (true === $this->isWaitingTimeExeededLimit()) {
- Common::logger("\tDate:". gmdate('Y-m-d H:i:s') . "none of the scripts ran", 'scheduler_log.txt');
- exit;
- }
- if ($this->getAvailableFreeMemory() > $this->_minRam) { //starting time checks for the available RAM
- Common::displayMessages(" + Free Memory: {$this->getAvailableFreeMemory()} KB");
- if ($resultScheduleServerClasses = $this->getServerClassesForScheduling()) {
- $firstTime = false;
- Common::logger("Date:". gmdate('Y-m-d H:i:s') . "scheduler classes count: ".mysqli_num_rows($resultScheduleServerClasses), 'scheduler_log.txt');
- while ($rowServerClass = mysqli_fetch_assoc($resultScheduleServerClasses)) {
- if ($rowServerClass['server_name'] && $rowServerClass['class_name']) {
- $showMsg = true;
- while ($this->getPhpProcessesCount() >= $this->_maxNoOfProcesses || $this->getAvailableFreeMemory() < $this->_minRam) {
- // first time shows the message
- if (true === $showMsg) {
- $this->showWaitingMsg();
- $showMsg = false;
- }
- // if serverScheduler is taking more than the specified limit, exit
- if (true === $this->isWaitingTimeExeededLimit()) {
- Common::logger("\tDate:". gmdate('Y-m-d H:i:s') . "processed crons count: ". count($this->_ranServerClasses), 'scheduler_log.txt');
- Common::displayMessages(" + Scheduler is taking more than {$this->_waitimeTimeLimitMinutes}Mins 30Sec...");
- exit;
- }
- Common::displayMessages("\t + Waiting for 10s");
- sleep(10); // 10 sec delay
- }
- if ($this->getPhpProcessesCount() < $this->_maxNoOfProcesses) {
- $command = "php Scripts/iCanDeliveryLive_new.php '{$rowServerClass['server_name']}' '{$rowServerClass['class_name']}' '{$rowServerClass['provider_name']}' '{$rowServerClass['customer_name']}'";
- //Log data extraction details and errors
- $logFile = __DIR__."/log/delivery_logs/{$rowServerClass['server_name']}/{$rowServerClass['class_name']}/{$rowServerClass['customer_name']}/". str_replace(' ', 'T', $this->_schedulerStartedgmtTime) ."_log.txt";
- mkdir(dirname($logFile), "0777", true);
- chmod(dirname($logFile), 0777);
- Common::displayMessages("\t + $command \n\t $logFile");
- dirname($logFile) ? exec("$command > $logFile &") : exec("$command");
- Common::displayMessages(" + ".$iterator++ . ". {$rowServerClass['server_name']} => {$rowServerClass['class_name']} ({$rowServerClass['provider_name']}) - {$rowServerClass['cronId']}", 'bold_purple');
- //$this->_ranServerCrons[] = $rowServerClass['cronId'];
- $this->_ranServerClasses[] = $rowServerClass['class_id'];
- }
- } // end if
- } // end while
- } else {
- if ($firstTime) {
- Common::displayMessages(" + No classes to schedule", 'bold_red');
- } else {
- Common::displayMessages(" + All active server classes scheduled for extraction", 'bold_cyan');
- }
- Common::freeUserDefinedVariablesSpace();
- exit;
- } // end if else
- } else {
- if (true === $showMsg) {
- $this->showWaitingMsg();
- $showMsg = false;
- }
- } // end if else
- } // end while
-
- }
-
- /**
- * DeliveryScheduler showWaitingMsg
- * Shows the message why the scheduler is waiting
- * @author Sanjay <incubator228@hotmail.com>
- * @date 15-SEP-2014
- */
- private function showWaitingMsg()
- {
- $phpProcessCount = $this->getPhpProcessesCount();
- $ramAvailable = $this->getAvailableFreeMemory();
- if ($phpProcessCount >= $this->_maxNoOfProcesses) {
- $warningsArr[] = "Max number of processes ($phpProcessCount/$this->_maxNoOfProcesses) reached";
- $warningsArr[] = "Waiting for processor to finish any of the currently running processes";
- }
- if ($ramAvailable < $this->_minRam) {
- $warningsArr[] = "Not enough memory (Min RAM needed: $this->_minRam KB, RAM Avialble: $ramAvailable KB)";
- $warningsArr[] = "Waiting to get the required amount of RAM";
- }
- if (!empty($warningsArr)) {
- Common::displayMessages($warningsArr, 'bold_red', '', '-');
- }
- }
-
- /**
- * DeliveryScheduler isWaitingTimeExeededLimit
- * Returns wheather the scheduler script waiting time is exeeded
- * @return Boolean wheather the scheduler script waiting time is exeeded a particluar limit
- * @author Sanjay <incubator228@hotmail.com>
- * @date 26-AUG-2014
- */
- private function isWaitingTimeExeededLimit()
- {
- $currentTime = new DateTime(gmdate("Y-m-d H:i:s"));
- $interval = $this->_schedulerStartedgmtTimeObj->diff($currentTime);
- if ($interval->i >= $this->_waitimeTimeLimitMinutes) {
- return true;
- }
- return false;
- }
- /**
- * DeliveryScheduler getAvailableFreeMemory
- * Returns the free memory available in the machine
- * @return Int freeMem free memory available (KB) in the machine
- * @author Sanjay <incubator228@hotmail.com>
- * @date 06-AUG-2014
- */
- private function getAvailableFreeMemory()
- {
- $freeMem = shell_exec("cat /proc/meminfo");
- $freeMem = explode('MemFree:', $freeMem);
- $freeMem = explode('kB', $freeMem[1]);
- $freeMem = trim($freeMem[0]);
- return $freeMem;
- }
- /**
- * DeliveryScheduler getServerClassesForScheduling
- * Returns the object which contains the servers which is active and
- * corresponding classes
- * whose upload script is not still running
- * whose upload not done yet by checking(falg IN ('STDZN_COMPLETED','CRON UPDATED'))
- * @return Object Database object contains the servers and classes which are ready to run
- * @author Amrutha Viswanath <incubator319@hotmail.com>
- * @date 22-NOV-2016
- */
- private function getServerClassesForScheduling()
- {
- $sql = "SELECT s.server_name, s.customer_id, c.class_name, c.id as class_id, p.provider_name, cu.customer_name
- FROM tbl_server s
- INNER JOIN tbl_server_class c
- ON (s.server_status = 'Active'
- $this->_sqlWhereCondition
- AND s.is_delivery_enabled = 'Enable')
- AND c.tbl_server_id = s.id
- INNER JOIN tbl_customer cu
- ON cu.id = s.customer_id
- INNER JOIN tbl_server_providers p
- ON p.id = s.tbl_server_providers_id
- AND (SELECT s3.class_id FROM s3_monitor s3
- WHERE s3.server_id = s.id
- AND s3.class_id = c.id
- AND s3.flag IN ('STDZN_COMPLETED','CRON UPDATED')
- AND s3.class_id NOT IN (". implode(',', $this->_ranServerClasses) .")
- limit 1)";
-
- return $this->_dbCron->executeQuery($sql);
- }
-
- /**
- * DeliveryScheduler getPhpProcessesCount
- * Returns the number of currently running php delivery scripts
- * @return Int the number of currently running php delivery scripts
- * @author Sanjay <incubator228@hotmail.com>
- * @date 07-AUG-2014
- */
- private function getPhpProcessesCount()
- {
- return substr_count(shell_exec("ps aux | grep php"), 'php index.php');
- }
-
- }
- chdir(__DIR__.'/'); // Setting the project root directory
- //require_once("config/config.php");
- include 'Modules/General/GeneralArrays.php';
- include 'Modules/General/Common.php';
- include '../SiFrameworks/Db.php';
- $scheduler = new DeliveryScheduler($argv[1]);