AbstractCronControllerWithoutThread.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxVps\Core\CommandLine;
  3. use \ModulesGarden\Servers\ProxmoxVps\Core\ModuleConstants;
  4. use \ModulesGarden\Servers\ProxmoxVps\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. if ($this->isStart() === false)
  113. {
  114. if ($this->child > 0 && $this->isChild === false)
  115. {
  116. while ($this->canRunScript())
  117. {
  118. if ($this->isChildrenCreate())
  119. {
  120. $parentId = getmypid();
  121. $parentUId = $this->parentUserId ? $this->parentUserId : getmyuid();
  122. $parentGId = $this->parentGroupId ? $this->parentGroupId : getmygid();
  123. foreach ($this->getIndexElements() as $key)
  124. {
  125. if ($this->cronManager->isChildRunning($this->className, $key))
  126. {
  127. continue;
  128. }
  129. $phpInterpreter = \PHP_BINARY ?: 'php';
  130. $internalCronDumpFile = ModuleConstants::getFullPath('storage', 'crons', 'cronLog');
  131. exec($phpInterpreter . " " . ModuleConstants::getModuleRootDir() . DS . 'cron' . DS . 'cron.php ' . $this->className
  132. . " --parent {$parentId} --parentuid {$parentUId} --parentgid {$parentGId} --child {$key} >> {$internalCronDumpFile} &");
  133. }
  134. }
  135. $this->wait(4000000);
  136. }
  137. }
  138. elseif ($this->child === 0 && $this->isChild === false)
  139. {
  140. $this->exit = false;
  141. $this->run();
  142. }
  143. elseif ($this->isChild === true)
  144. {
  145. $this->exit = false;
  146. $this->run();
  147. $this->removePid();
  148. exit;
  149. }
  150. }
  151. }
  152. catch (\Exception $ex)
  153. {
  154. $this->error = $ex->getMessage();
  155. }
  156. }
  157. public function setParentGroupId($parentGroupId)
  158. {
  159. $this->parentGroupId = $parentGroupId;
  160. return $this;
  161. }
  162. public function setParentUserId($parentUserId)
  163. {
  164. $this->parentUserId = $parentUserId;
  165. return $this;
  166. }
  167. public function isStart()
  168. {
  169. return ($this->exit === false);
  170. }
  171. public function wait($seconds = 500000)
  172. {
  173. usleep($seconds);
  174. }
  175. function __destruct()
  176. {
  177. if ($this->error)
  178. {
  179. CronTabs::ifName($this->className)->addError($this->error);
  180. }
  181. }
  182. }