| Conditions | 10 | 
| Paths | 50 | 
| Total Lines | 64 | 
| Code Lines | 34 | 
| Lines | 0 | 
| Ratio | 0 % | 
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | #!/bin/php | ||
| 67 | function monitorProcessWatch($parentProcessId){ | ||
| 68 | |||
| 69 | $m = new _MeasurePerformance(); | ||
| 70 | $mysql = new _MySQL(); | ||
| 71 | $mysql->connect(Setup::$connectionArray); | ||
| 72 | |||
| 73 | $parallelProcessesMonitors = Setup::$settings['max_monitor_processes']; | ||
| 74 | |||
| 75 | $monitorProcesses = array(); | ||
| 76 | $processCountMonitors = 0; | ||
| 77 | |||
| 78 | $ipDomain = false; | ||
| 79 | |||
| 80 | 	while (true) { | ||
| 81 | // are we still running? | ||
| 82 | 		if(!Utilities::is_process_running($parentProcessId)){ | ||
| 83 | 				_Logging::appLog("Parent Stopped - monitorStartWatch exited"); | ||
| 84 | exit(); | ||
| 85 | } | ||
| 86 | |||
| 87 | $processCountMonitors = count($monitorProcesses); | ||
| 88 | |||
| 89 | 		if ($processCountMonitors < $parallelProcessesMonitors){ | ||
| 90 | $ipDomain = Utilities::getNextMonitor($mysql); | ||
| 91 | 				if ($ipDomain!==false) { | ||
| 92 | // start it | ||
| 93 | $cmd = 'php '.dirname(__FILE__).'/monitorJob.php -h '.escapeshellarg($ipDomain); | ||
| 94 | $pid = Utilities::run_in_background($cmd); | ||
| 95 | $m->work(1); | ||
| 96 | $monitorProcesses[] = $pid; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | // was there any work? | ||
| 101 | 		if($ipDomain===false){ | ||
| 102 | sleep(10);//10 seconds | ||
| 103 | 		}else{ | ||
| 104 | usleep(10000);//ideal time 10ms | ||
| 105 | } | ||
| 106 | |||
| 107 | // delete finished processes | ||
| 108 | 		for ($x = 0; $x < $processCountMonitors; $x++) { | ||
| 109 | 				if(isset($monitorProcesses[$x])){ | ||
| 110 | 						if(!Utilities::is_process_running($monitorProcesses[$x])){ | ||
| 111 | unset($monitorProcesses[$x]); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | // fix array index | ||
| 117 | $monitorProcesses = array_values($monitorProcesses); | ||
| 118 | |||
| 119 | $processCountMonitors = count($monitorProcesses); | ||
| 120 | |||
| 121 | //randomly reset counter every now and then | ||
| 122 | 		if(mt_rand(1,2000)==1){ | ||
| 123 | $m->endWork(); | ||
| 124 | 			_Logging::appLog("App Avg Hosts/sec: {$m->avgPerformance}\tMonitor Threads: $processCountMonitors/$parallelProcessesMonitors"); | ||
| 125 | $m = new _MeasurePerformance(); | ||
| 126 | } | ||
| 127 | |||
| 128 | } | ||
| 129 | |||
| 130 | } | ||
| 131 |