AbstractPool.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Jenner
  5. * Date: 2015/11/3
  6. * Time: 14:37
  7. */
  8. namespace Jenner\SimpleFork;
  9. /**
  10. * processes' pool
  11. *
  12. * @package Jenner\SimpleFork
  13. */
  14. abstract class AbstractPool
  15. {
  16. /**
  17. * process list
  18. *
  19. * @var Process[]
  20. */
  21. protected $processes = array();
  22. /**
  23. * get process by pid
  24. *
  25. * @param $pid
  26. * @return null|Process
  27. */
  28. public function getProcessByPid($pid)
  29. {
  30. foreach ($this->processes as $process) {
  31. if ($process->getPid() == $pid) {
  32. return $process;
  33. }
  34. }
  35. return null;
  36. }
  37. /**
  38. * shutdown sub process and no wait. it is dangerous,
  39. * maybe the sub process is working.
  40. */
  41. public function shutdownForce()
  42. {
  43. $this->shutdown(SIGKILL);
  44. }
  45. /**
  46. * shutdown all process
  47. *
  48. * @param int $signal
  49. */
  50. public function shutdown($signal = SIGTERM)
  51. {
  52. foreach ($this->processes as $process) {
  53. if ($process->isRunning()) {
  54. $process->shutdown(true, $signal);
  55. }
  56. }
  57. }
  58. /**
  59. * if all processes are stopped
  60. *
  61. * @return bool
  62. */
  63. public function isFinished()
  64. {
  65. foreach ($this->processes as $process) {
  66. if (!$process->isStopped()) {
  67. return false;
  68. }
  69. }
  70. return true;
  71. }
  72. /**
  73. * waiting for the sub processes to exit
  74. *
  75. * @param bool|true $block if true the parent process will be blocked until all
  76. * sub processes exit. else it will check if there are processes that had been exited once and return.
  77. * @param int $sleep when $block is true, it will check sub processes every $sleep minute
  78. */
  79. public function wait($block = true, $sleep = 100)
  80. {
  81. do {
  82. foreach ($this->processes as $process) {
  83. if (!$process->isRunning()) {
  84. continue;
  85. }
  86. }
  87. usleep($sleep);
  88. } while ($block && $this->aliveCount() > 0);
  89. }
  90. /**
  91. * get the count of running processes
  92. *
  93. * @return int
  94. */
  95. public function aliveCount()
  96. {
  97. $count = 0;
  98. foreach ($this->processes as $process) {
  99. if ($process->isRunning()) {
  100. $count++;
  101. }
  102. }
  103. return $count;
  104. }
  105. /**
  106. * get process by name
  107. *
  108. * @param string $name process name
  109. * @return Process|null
  110. */
  111. public function getProcessByName($name)
  112. {
  113. foreach ($this->processes as $process) {
  114. if ($process->name() == $name) {
  115. return $process;
  116. }
  117. }
  118. return null;
  119. }
  120. /**
  121. * remove process by name
  122. *
  123. * @param string $name process name
  124. * @throws \RuntimeException
  125. */
  126. public function removeProcessByName($name)
  127. {
  128. foreach ($this->processes as $key => $process) {
  129. if ($process->name() == $name) {
  130. if ($process->isRunning()) {
  131. throw new \RuntimeException("can not remove a running process");
  132. }
  133. unset($this->processes[$key]);
  134. }
  135. }
  136. }
  137. /**
  138. * remove exited process
  139. */
  140. public function removeExitedProcess()
  141. {
  142. foreach ($this->processes as $key => $process) {
  143. if ($process->isStopped()) {
  144. unset($this->processes[$key]);
  145. }
  146. }
  147. }
  148. /**
  149. * return process count
  150. *
  151. * @return int
  152. */
  153. public function count()
  154. {
  155. return count($this->processes);
  156. }
  157. /**
  158. * get all processes
  159. *
  160. * @return Process[]
  161. */
  162. public function getProcesses()
  163. {
  164. return $this->processes;
  165. }
  166. }