AbstractCronControllerWithoutThread.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\CommandLine;
  3. use \ModulesGarden\ProxmoxAddon\Core\ModuleConstants;
  4. use \ModulesGarden\ProxmoxAddon\Core\Models\CronTabs;
  5. /**
  6. * Description of AbstractCronController
  7. *
  8. * @author Rafał
  9. */
  10. abstract class AbstractCronControllerWithoutThread
  11. {
  12. protected $exit = true;
  13. protected $error = false;
  14. protected $isChild = false;
  15. protected $className;
  16. protected $child = 0;
  17. protected $childId;
  18. protected $parentId;
  19. protected $cronManager;
  20. protected $parentUserId;
  21. protected $parentGroupId;
  22. public function setParentId($parentId)
  23. {
  24. $this->parentId = $parentId;
  25. return $this;
  26. }
  27. public function setChildId($childId)
  28. {
  29. $this->childId = $childId;
  30. return $this;
  31. }
  32. public function ischild($isChild)
  33. {
  34. $this->isChild = $isChild;
  35. return $this;
  36. }
  37. public function setClassName($className)
  38. {
  39. $this->className = $className;
  40. return $this;
  41. }
  42. protected function canRunScript()
  43. {
  44. $run = true;
  45. foreach (CronTabs::get() as $cronTab)
  46. {
  47. if ($cronTab->name == CronTabs::DEFAULT_CRON_MANAGER_NAME && $cronTab->status == 'stop')
  48. {
  49. $run = false;
  50. }
  51. elseif ($cronTab->name == $this->className && $cronTab->status == 'stop')
  52. {
  53. $run = false;
  54. }
  55. }
  56. return $run;
  57. }
  58. protected function updatePid()
  59. {
  60. if ($this->isChild)
  61. {
  62. CronManager::updatePids($this->className, $this->childId);
  63. if ($this->parentGroupId === getmygid() && $this->parentUserId === getmyuid())
  64. {
  65. $this->removePid();
  66. }
  67. }
  68. return $this;
  69. }
  70. protected function isExistPid()
  71. {
  72. if ($this->isChild)
  73. {
  74. return CronManager::existPids($this->className, $this->childId);
  75. }
  76. return $this->canRunScript();
  77. }
  78. protected function removePid()
  79. {
  80. if ($this->isChild)
  81. {
  82. CronManager::removePids($this->className, $this->childId);
  83. }
  84. return $this;
  85. }
  86. abstract public function run();
  87. public function isChildrenCreate()
  88. {
  89. return false;
  90. }
  91. public function getChildrenCount()
  92. {
  93. return $this->child;
  94. }
  95. public function setCronManager($cronManager)
  96. {
  97. $this->cronManager = $cronManager;
  98. return $this;
  99. }
  100. public function getIndexElements()
  101. {
  102. $childrenListIds = [];
  103. for ($key = 1; $key <= $this->getChildrenCount(); $key++)
  104. {
  105. $childrenListIds[] = $key;
  106. }
  107. return $childrenListIds;
  108. }
  109. public function start()
  110. {
  111. try
  112. {
  113. if ($this->isStart() === false)
  114. {
  115. if ($this->child > 0 && $this->isChild === false)
  116. {
  117. while ($this->canRunScript())
  118. {
  119. if ($this->isChildrenCreate())
  120. {
  121. $parentId = getmypid();
  122. $parentUId = $this->parentUserId ? $this->parentUserId : getmyuid();
  123. $parentGId = $this->parentGroupId ? $this->parentGroupId : getmygid();
  124. foreach ($this->getIndexElements() as $key)
  125. {
  126. if ($this->cronManager->isChildRunning($this->className, $key))
  127. {
  128. continue;
  129. }
  130. $phpInterpreter = \PHP_BINARY ?: 'php';
  131. $internalCronDumpFile = ModuleConstants::getFullPath('storage', 'crons', 'cronLog');
  132. exec($phpInterpreter . " " . ModuleConstants::getModuleRootDir() . DS . 'cron' . DS . 'cron.php ' . $this->className
  133. . " --parent {$parentId} --parentuid {$parentUId} --parentgid {$parentGId} --child {$key} >> {$internalCronDumpFile} &");
  134. }
  135. }
  136. $this->wait(4000000);
  137. }
  138. }
  139. elseif ($this->child === 0 && $this->isChild === false)
  140. {
  141. $this->exit = false;
  142. $this->run();
  143. }
  144. elseif ($this->isChild === true)
  145. {
  146. $this->exit = false;
  147. $this->run();
  148. $this->removePid();
  149. exit;
  150. }
  151. }
  152. }
  153. catch (\Exception $ex)
  154. {
  155. $this->error = $ex->getMessage();
  156. }
  157. }
  158. public function setParentGroupId($parentGroupId)
  159. {
  160. $this->parentGroupId = $parentGroupId;
  161. return $this;
  162. }
  163. public function setParentUserId($parentUserId)
  164. {
  165. $this->parentUserId = $parentUserId;
  166. return $this;
  167. }
  168. public function isStart()
  169. {
  170. return ($this->exit === false);
  171. }
  172. public function wait($seconds = 500000)
  173. {
  174. usleep($seconds);
  175. }
  176. function __destruct()
  177. {
  178. if ($this->error)
  179. {
  180. CronTabs::ifName($this->className)->addError($this->error);
  181. }
  182. }
  183. }