PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/mls-project/php/Standardization/DeliveryScheduler.php

https://bitbucket.org/amruthaviswanath/mls-qa-tool
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
  1. <?php
  2. /**
  3. * MLS Factory Data Standardization
  4. * Class DeliveryScheduler
  5. * Schedule different rets server class for delivery
  6. * @category Data_Standardization
  7. * @package Data_Standardization_DeliveryScheduler
  8. * @version 1.0
  9. * @author Amrutha Viswanath <incubator319@hotmail.com>
  10. * @date 22-NOV-2016
  11. */
  12. Class DeliveryScheduler
  13. {
  14. /*
  15. * Database connection variable
  16. * @var object
  17. */
  18. private $_dbCron;
  19. /*
  20. * Maximum number of php extraction scripts allowed
  21. * @var integer
  22. */
  23. private $_maxNoOfProcesses;
  24. /*
  25. * Mimimum amount of RAM needed to run a php extraction script
  26. * @var integer
  27. */
  28. private $_minRam;
  29. /*
  30. * GMT time when the scheduler started
  31. * @var datetime
  32. */
  33. private $_schedulerStartedgmtTime;
  34. /*
  35. * Maximum waiting time for the scheduler in minutes
  36. * @var integer
  37. */
  38. private $_waitimeTimeLimitMinutes;
  39. /*
  40. * Already scheduled crons array
  41. * @var array
  42. */
  43. private $_ranServerCrons = array(0);
  44. /*
  45. * Already scheduled classes array
  46. * @var array
  47. */
  48. private $_ranServerClasses = array(0);
  49. /*
  50. * Mysql where condition to apply the cron flag filter
  51. * @var String
  52. */
  53. private $_sqlWhereCondition;
  54. /**
  55. * DeliveryScheduler construct
  56. * Constructor, Initilizes variables _dbCron
  57. * @param Integer $cronflag Flag to identify the servers to run in that particular cron
  58. * @author Sanjay <incubator228@hotmail.com>
  59. * @date 06-AUG-2014
  60. * @modified Amrutha Viswanath <incubator319@hotmail.com>
  61. * @date 22-NOV-2016
  62. */
  63. public function __construct($cronflag = 0)
  64. {
  65. Common::displayMessages(' + Scheduler started');
  66. Common::displayMessages(" + Establishing database(cron) connection");
  67. $configArr = parse_ini_file("config/config.ini", true);
  68. $this->_dbCron = new Db('cron');
  69. Common::displayMessages(' + Established DB connection');
  70. if ($cronflag) {
  71. $this->_sqlWhereCondition = " AND s.incremental_server_flag = '$cronflag' ";
  72. }
  73. $this->_minRam = defined('MIN_RAM') ? MIN_RAM : 20480;
  74. $this->_maxNoOfProcesses = defined('MAX_NO_PROCESSES') ? MAX_NO_PROCESSES : 50;
  75. $this->_waitimeTimeLimitMinutes = defined('SCHEDULER_MAX_WAITING_TIME') ? SCHEDULER_MAX_WAITING_TIME : 14;
  76. $this->_schedulerStartedgmtTime = gmdate('Y-m-d H:i:s');
  77. $this->_schedulerStartedgmtTimeObj = new DateTime($this->_schedulerStartedgmtTime);
  78. $this->updateAllStoppedProcesses();
  79. //exit;
  80. $this->runScheduler();
  81. $this->_dbCron = null;
  82. }
  83. /**
  84. * DeliveryScheduler updateAllStoppedProcesses
  85. * update all stopped processes's cron status to "cron updated" so that it will taken for next delivery
  86. * @author Amrutha Viswanath <incubator319@hotmail.com>
  87. * @date 22-NOV-2016
  88. */
  89. private function updateAllStoppedProcesses()
  90. {
  91. $notExistProcessesStatusIdArr = array();
  92. $sql = "SELECT s3.id as monitorId, s3.process_id
  93. FROM s3_monitor s3
  94. INNER JOIN tbl_server s
  95. ON s3.flag = 'UPLOAD STARTED'
  96. AND s3.server_id = s.id
  97. $this->_sqlWhereCondition";
  98. if ($result = $this->_dbCron->executeQuery($sql)) {
  99. while ($row = mysqli_fetch_assoc($result)) {
  100. if (!file_exists("/proc/{$row['process_id']}")) { // if process not exists
  101. $notExistProcessesStatusIdArr[] = $row['monitorId'];
  102. }
  103. } // end while
  104. if (!empty ($notExistProcessesStatusIdArr)) {
  105. echo "updated " . count($notExistProcessesStatusIdArr) . " classes cron status as cron updated\n";
  106. $sqlUpdateStatus = "UPDATE s3_monitor
  107. SET flag = 'CRON UPDATED'
  108. WHERE id IN (". implode(',', $notExistProcessesStatusIdArr) .")";
  109. $this->_dbCron->executeNonQuery($sqlUpdateStatus);
  110. } // end if
  111. } // end if
  112. }
  113. /**
  114. * DeliveryScheduler runScheduler
  115. * Schedules the active rets server classes which is ready to run
  116. * @author Sanjay <incubator228@hotmail.com>
  117. * @date 06-AUG-2014
  118. */
  119. public function runScheduler()
  120. {
  121. $showMsg = true;
  122. $iterator = 1;
  123. $firstTime = true;
  124. while (true) { // inifinite loop
  125. if (true === $this->isWaitingTimeExeededLimit()) {
  126. Common::logger("\tDate:". gmdate('Y-m-d H:i:s') . "none of the scripts ran", 'scheduler_log.txt');
  127. exit;
  128. }
  129. if ($this->getAvailableFreeMemory() > $this->_minRam) { //starting time checks for the available RAM
  130. Common::displayMessages(" + Free Memory: {$this->getAvailableFreeMemory()} KB");
  131. if ($resultScheduleServerClasses = $this->getServerClassesForScheduling()) {
  132. $firstTime = false;
  133. Common::logger("Date:". gmdate('Y-m-d H:i:s') . "scheduler classes count: ".mysqli_num_rows($resultScheduleServerClasses), 'scheduler_log.txt');
  134. while ($rowServerClass = mysqli_fetch_assoc($resultScheduleServerClasses)) {
  135. if ($rowServerClass['server_name'] && $rowServerClass['class_name']) {
  136. $showMsg = true;
  137. while ($this->getPhpProcessesCount() >= $this->_maxNoOfProcesses || $this->getAvailableFreeMemory() < $this->_minRam) {
  138. // first time shows the message
  139. if (true === $showMsg) {
  140. $this->showWaitingMsg();
  141. $showMsg = false;
  142. }
  143. // if serverScheduler is taking more than the specified limit, exit
  144. if (true === $this->isWaitingTimeExeededLimit()) {
  145. Common::logger("\tDate:". gmdate('Y-m-d H:i:s') . "processed crons count: ". count($this->_ranServerClasses), 'scheduler_log.txt');
  146. Common::displayMessages(" + Scheduler is taking more than {$this->_waitimeTimeLimitMinutes}Mins 30Sec...");
  147. exit;
  148. }
  149. Common::displayMessages("\t + Waiting for 10s");
  150. sleep(10); // 10 sec delay
  151. }
  152. if ($this->getPhpProcessesCount() < $this->_maxNoOfProcesses) {
  153. $command = "php Scripts/iCanDeliveryLive_new.php '{$rowServerClass['server_name']}' '{$rowServerClass['class_name']}' '{$rowServerClass['provider_name']}' '{$rowServerClass['customer_name']}'";
  154. //Log data extraction details and errors
  155. $logFile = __DIR__."/log/delivery_logs/{$rowServerClass['server_name']}/{$rowServerClass['class_name']}/{$rowServerClass['customer_name']}/". str_replace(' ', 'T', $this->_schedulerStartedgmtTime) ."_log.txt";
  156. mkdir(dirname($logFile), "0777", true);
  157. chmod(dirname($logFile), 0777);
  158. Common::displayMessages("\t + $command \n\t $logFile");
  159. dirname($logFile) ? exec("$command > $logFile &") : exec("$command");
  160. Common::displayMessages(" + ".$iterator++ . ". {$rowServerClass['server_name']} => {$rowServerClass['class_name']} ({$rowServerClass['provider_name']}) - {$rowServerClass['cronId']}", 'bold_purple');
  161. //$this->_ranServerCrons[] = $rowServerClass['cronId'];
  162. $this->_ranServerClasses[] = $rowServerClass['class_id'];
  163. }
  164. } // end if
  165. } // end while
  166. } else {
  167. if ($firstTime) {
  168. Common::displayMessages(" + No classes to schedule", 'bold_red');
  169. } else {
  170. Common::displayMessages(" + All active server classes scheduled for extraction", 'bold_cyan');
  171. }
  172. Common::freeUserDefinedVariablesSpace();
  173. exit;
  174. } // end if else
  175. } else {
  176. if (true === $showMsg) {
  177. $this->showWaitingMsg();
  178. $showMsg = false;
  179. }
  180. } // end if else
  181. } // end while
  182. }
  183. /**
  184. * DeliveryScheduler showWaitingMsg
  185. * Shows the message why the scheduler is waiting
  186. * @author Sanjay <incubator228@hotmail.com>
  187. * @date 15-SEP-2014
  188. */
  189. private function showWaitingMsg()
  190. {
  191. $phpProcessCount = $this->getPhpProcessesCount();
  192. $ramAvailable = $this->getAvailableFreeMemory();
  193. if ($phpProcessCount >= $this->_maxNoOfProcesses) {
  194. $warningsArr[] = "Max number of processes ($phpProcessCount/$this->_maxNoOfProcesses) reached";
  195. $warningsArr[] = "Waiting for processor to finish any of the currently running processes";
  196. }
  197. if ($ramAvailable < $this->_minRam) {
  198. $warningsArr[] = "Not enough memory (Min RAM needed: $this->_minRam KB, RAM Avialble: $ramAvailable KB)";
  199. $warningsArr[] = "Waiting to get the required amount of RAM";
  200. }
  201. if (!empty($warningsArr)) {
  202. Common::displayMessages($warningsArr, 'bold_red', '', '-');
  203. }
  204. }
  205. /**
  206. * DeliveryScheduler isWaitingTimeExeededLimit
  207. * Returns wheather the scheduler script waiting time is exeeded
  208. * @return Boolean wheather the scheduler script waiting time is exeeded a particluar limit
  209. * @author Sanjay <incubator228@hotmail.com>
  210. * @date 26-AUG-2014
  211. */
  212. private function isWaitingTimeExeededLimit()
  213. {
  214. $currentTime = new DateTime(gmdate("Y-m-d H:i:s"));
  215. $interval = $this->_schedulerStartedgmtTimeObj->diff($currentTime);
  216. if ($interval->i >= $this->_waitimeTimeLimitMinutes) {
  217. return true;
  218. }
  219. return false;
  220. }
  221. /**
  222. * DeliveryScheduler getAvailableFreeMemory
  223. * Returns the free memory available in the machine
  224. * @return Int freeMem free memory available (KB) in the machine
  225. * @author Sanjay <incubator228@hotmail.com>
  226. * @date 06-AUG-2014
  227. */
  228. private function getAvailableFreeMemory()
  229. {
  230. $freeMem = shell_exec("cat /proc/meminfo");
  231. $freeMem = explode('MemFree:', $freeMem);
  232. $freeMem = explode('kB', $freeMem[1]);
  233. $freeMem = trim($freeMem[0]);
  234. return $freeMem;
  235. }
  236. /**
  237. * DeliveryScheduler getServerClassesForScheduling
  238. * Returns the object which contains the servers which is active and
  239. * corresponding classes
  240. * whose upload script is not still running
  241. * whose upload not done yet by checking(falg IN ('STDZN_COMPLETED','CRON UPDATED'))
  242. * @return Object Database object contains the servers and classes which are ready to run
  243. * @author Amrutha Viswanath <incubator319@hotmail.com>
  244. * @date 22-NOV-2016
  245. */
  246. private function getServerClassesForScheduling()
  247. {
  248. $sql = "SELECT s.server_name, s.customer_id, c.class_name, c.id as class_id, p.provider_name, cu.customer_name
  249. FROM tbl_server s
  250. INNER JOIN tbl_server_class c
  251. ON (s.server_status = 'Active'
  252. $this->_sqlWhereCondition
  253. AND s.is_delivery_enabled = 'Enable')
  254. AND c.tbl_server_id = s.id
  255. INNER JOIN tbl_customer cu
  256. ON cu.id = s.customer_id
  257. INNER JOIN tbl_server_providers p
  258. ON p.id = s.tbl_server_providers_id
  259. AND (SELECT s3.class_id FROM s3_monitor s3
  260. WHERE s3.server_id = s.id
  261. AND s3.class_id = c.id
  262. AND s3.flag IN ('STDZN_COMPLETED','CRON UPDATED')
  263. AND s3.class_id NOT IN (". implode(',', $this->_ranServerClasses) .")
  264. limit 1)";
  265. return $this->_dbCron->executeQuery($sql);
  266. }
  267. /**
  268. * DeliveryScheduler getPhpProcessesCount
  269. * Returns the number of currently running php delivery scripts
  270. * @return Int the number of currently running php delivery scripts
  271. * @author Sanjay <incubator228@hotmail.com>
  272. * @date 07-AUG-2014
  273. */
  274. private function getPhpProcessesCount()
  275. {
  276. return substr_count(shell_exec("ps aux | grep php"), 'php index.php');
  277. }
  278. }
  279. chdir(__DIR__.'/'); // Setting the project root directory
  280. //require_once("config/config.php");
  281. include 'Modules/General/GeneralArrays.php';
  282. include 'Modules/General/Common.php';
  283. include '../SiFrameworks/Db.php';
  284. $scheduler = new DeliveryScheduler($argv[1]);