Hypervisor.php 5.0 KB

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