Hypervisor.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. namespace ModulesGarden\Servers\ZimbraEmail\Core\CommandLine;
  3. use ModulesGarden\Servers\ZimbraEmail\Core\Models\Commands;
  4. /**
  5. * We need that for calculating interval between operations and for killing process for PHP < 7.1
  6. * Don't remove it!
  7. */
  8. declare(ticks = 1);
  9. /**
  10. * Class Hypervisor
  11. * @package ModulesGarden\Servers\ZimbraEmail\Core\CommandLine
  12. * @todo - clean up!
  13. */
  14. class Hypervisor
  15. {
  16. /**
  17. * command name
  18. * @var null
  19. */
  20. protected $name = null;
  21. /**
  22. * command params
  23. * @var null
  24. */
  25. protected $params = null;
  26. /**
  27. * @var int
  28. */
  29. protected $counter = 0;
  30. /**
  31. * @var \ModulesGarden\Servers\ZimbraEmail\Core\Models\Commands
  32. */
  33. protected $entity = null;
  34. protected $tickTime = 1;
  35. /**
  36. * @var int
  37. */
  38. protected $sleepInterval = 5;
  39. /**
  40. * Hypervisor constructor.
  41. * @param $name
  42. * @param $params
  43. */
  44. public function __construct($name, $params)
  45. {
  46. $this->name = $name;
  47. $this->params = $params;
  48. }
  49. /**
  50. * Lock script
  51. * @return $this
  52. * @throws \Exception
  53. */
  54. public function lock()
  55. {
  56. if($this->isActive())
  57. {
  58. throw new \Exception('Cannot create lock. Command already running');
  59. }
  60. $this->registerTickHandler();
  61. $this->registerSignalHandler();
  62. $this->getEntity()->setRunning();
  63. return $this;
  64. }
  65. /**
  66. * Unlock script
  67. * @return $this
  68. */
  69. public function unlock()
  70. {
  71. $this->getEntity()
  72. ->setStopped()
  73. ->clearAction();
  74. return $this;
  75. }
  76. /**
  77. * Update information about running command
  78. * @return $this
  79. */
  80. public function ping()
  81. {
  82. $this->getEntity()
  83. ->ping();
  84. return $this;
  85. }
  86. /**
  87. * @param $interval
  88. * @return $this
  89. */
  90. public function sleep($interval)
  91. {
  92. $this->getEntity()
  93. ->setSleeping();
  94. /**
  95. * We need run tick function during sleep, so we run many sleep function in loop
  96. */
  97. $counter = 0;
  98. while($counter < $interval)
  99. {
  100. if($interval <= $this->sleepInterval)
  101. {
  102. $time = $interval;
  103. }
  104. else
  105. {
  106. $time = $this->sleepInterval;
  107. }
  108. $counter += $time;
  109. sleep($time);
  110. }
  111. $this->getEntity()
  112. ->setRunning();
  113. return $this;
  114. }
  115. public function shouldBeStopped()
  116. {
  117. return $this->getEntity()
  118. ->shouldBeStopped();
  119. }
  120. /**
  121. * @return bool
  122. * @throws \Exception
  123. */
  124. public function checkIfRunning()
  125. {
  126. if(!$this->isRunning())
  127. {
  128. throw new \Exception('Script is not running');
  129. }
  130. return true;
  131. }
  132. /**
  133. * @return bool
  134. */
  135. public function isStopped()
  136. {
  137. $this->getEntity()
  138. ->shouldBeStopped();
  139. }
  140. /**
  141. * Returns true if command is running or sleeping
  142. * @return bool
  143. */
  144. public function isActive()
  145. {
  146. $entity = $this->getEntity();
  147. $diff = time() - $entity->updated_at->timestamp;
  148. if($entity->isSleeping() && $diff <= $this->sleepInterval)
  149. {
  150. return true;
  151. }
  152. if($entity->isRunning() && $entity->updated_at->timestamp + $this->tickTime > time())
  153. {
  154. return true;
  155. }
  156. return false;
  157. }
  158. /**
  159. * @param bool $force
  160. * @return ModulesGarden\Servers\ZimbraEmail\Core\Models\Commands|Commands
  161. */
  162. protected function getEntity($force = false)
  163. {
  164. if($this->entity && $force === false)
  165. {
  166. return $this->entity;
  167. }
  168. $this->entity = Commands::where('name', $this->name)->first();
  169. if(!$this->entity)
  170. {
  171. $this->entity = new Commands();
  172. $this->entity->name = $this->name;
  173. $this->entity->uuid = uniqid();
  174. $this->entity->save();
  175. }
  176. return $this->entity;
  177. }
  178. /**
  179. *
  180. */
  181. protected function registerTickHandler()
  182. {
  183. $tick = new Tick(function(){
  184. $this->ping();
  185. }, 10);
  186. $tick->start();
  187. }
  188. /**
  189. * Mark process as stopped and exit. This function will be called when you press ctrl c from console
  190. */
  191. protected function registerSignalHandler()
  192. {
  193. if(!function_exists('pcntl_signal'))
  194. {
  195. return;
  196. }
  197. //works from PHP 7.1
  198. if(function_exists('pcntl_async_signals'))
  199. {
  200. pcntl_async_signals(true);
  201. }
  202. $exit = function(){
  203. $this->getEntity()->setStopped();
  204. exit;
  205. };
  206. pcntl_signal(SIGINT, $exit);
  207. pcntl_signal(SIGHUP, $exit);
  208. pcntl_signal(SIGUSR1, $exit);
  209. }
  210. }