CronManager.php_old 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\CommandLine;
  3. use ModulesGarden\ProxmoxAddon\Core\FileReader\Validator;
  4. use ModulesGarden\ProxmoxAddon\Core\ModuleConstants;
  5. use ModulesGarden\ProxmoxAddon\Core\DependencyInjection;
  6. use ModulesGarden\ProxmoxAddon\Core\FileReader\File;
  7. use ModulesGarden\ProxmoxAddon\Core\CommandLine\ReaderCronTask;
  8. use ModulesGarden\ProxmoxAddon\Core\Models\CronTabs;
  9. /**
  10. * Description of CronManager
  11. *
  12. * @author Rafał
  13. */
  14. class CronManager {
  15. const AVAILABLE_MODES = ['cli', 'cgi-fcgi'];
  16. protected $arguments = [];
  17. protected $appCronPath = '';
  18. protected $coreCronPath = '';
  19. protected $parentUId;
  20. protected $parentGId;
  21. protected $cronPids = '';
  22. protected $isParent = true;
  23. protected $parentId;
  24. protected $childId;
  25. /**
  26. * @var Validator
  27. */
  28. protected $fileValidator;
  29. public function __construct(Validator $fileValidator) {
  30. $this->fileValidator = $fileValidator;
  31. $this->appCronPath = ModuleConstants::getModuleRootDir() . DS . 'app' . DS . 'Cron' . DS;
  32. $this->coreCronPath = ModuleConstants::getModuleRootDir() . DS . 'core' . DS . 'Cron' . DS;
  33. $this->cronPidsPath = ModuleConstants::getModuleRootDir() . DS . 'storage' . DS . 'crons' . DS;
  34. if($this->fileValidator->isExist($this->cronPidsPath) === false)
  35. {
  36. mkdir($this->cronPidsPath);
  37. File::setPermission($this->cronPidsPath);
  38. }
  39. if (self::isThread())
  40. {
  41. $this->appCronPath .= "Thread" . DS;
  42. $this->coreCronPath .= "Thread" . DS;
  43. }
  44. else
  45. {
  46. $this->appCronPath .= "WithoutThread" . DS;
  47. $this->coreCronPath .= "WithoutThread" . DS;
  48. }
  49. $this->parentGId = getmygid();
  50. $this->parentUId = getmyuid();
  51. }
  52. public function setArgv($arguments)
  53. {
  54. $this->arguments = $arguments;
  55. return $this;
  56. }
  57. public function execute()
  58. {
  59. $this->loadArguments();
  60. if($this->isInCliMode() === false)
  61. {
  62. return $this;
  63. }
  64. if (count($this->arguments) == 0)
  65. {
  66. $this->arguments = ReaderCronTask::get();
  67. }
  68. $this->loadDefaultCronManager();
  69. foreach ($this->arguments as $className)
  70. {
  71. $className = trim(trim($className, '--'), '-');
  72. $class = ModuleConstants::getRootNamespace() . "\\";
  73. if ($this->fileValidator->isExist($this->appCronPath . $className . ".php"))
  74. {
  75. $class .= "App\Cron\\" . $this->getType() . "\\" . $className;
  76. $this->runTask($class, $className);
  77. }
  78. elseif ($this->fileValidator->isExist($this->coreCronPath . $className . ".php"))
  79. {
  80. $class .= "Core\Cron\\" . $this->getType() . "\\" . $className;
  81. $this->runTask($class, $className);
  82. }
  83. }
  84. return $this;
  85. }
  86. protected function runTask($class, $className)
  87. {
  88. if ($this->classExist($class) === true && $this->isCronRunning($className) === false)
  89. {
  90. if ($this->beforeRunTask($className) === false)
  91. {
  92. return;
  93. }
  94. $instant = DependencyInjection::create($class, null, (class_exists(\Thread::class)?false:true));
  95. $instant->setCronManager($this);
  96. $instant->setClassName($className);
  97. if (self::isThread() === false)
  98. {
  99. $instant->ischild(!$this->isParent)
  100. ->setChildId($this->childId)
  101. ->setParentId($this->parentId)
  102. ->setParentGroupId($this->parentGId)
  103. ->setParentUserId($this->parentUId);
  104. }
  105. $instant->start();
  106. unset($instant);
  107. $this->afterRunTask($className);
  108. }
  109. }
  110. protected function getType()
  111. {
  112. return (self::isThread()?'Thread':'WithoutThread');
  113. }
  114. public static function isThread()
  115. {
  116. return class_exists(\Thread::class);
  117. }
  118. protected function loadArguments()
  119. {
  120. $deleteClass = [];
  121. foreach ($this->arguments as $key => $name) {
  122. if (strpos($name, 'cron.php') !== false)
  123. {
  124. $deleteClass[] = $key;
  125. }
  126. elseif ($name === '--parent')
  127. {
  128. $this->parentId = $this->arguments[$key + 1];
  129. $this->isParent = false;
  130. $deleteClass[] = $key;
  131. $deleteClass[] = $key + 1;
  132. }
  133. elseif ($name === '--child')
  134. {
  135. $this->childId = $this->arguments[$key + 1];
  136. $this->isParent = false;
  137. $deleteClass[] = $key;
  138. $deleteClass[] = $key + 1;
  139. }
  140. elseif ($name === '--parentuid')
  141. {
  142. $this->parentUId = $this->arguments[$key + 1];
  143. $deleteClass[] = $key;
  144. $deleteClass[] = $key + 1;
  145. }
  146. elseif ($name === '--parentgid')
  147. {
  148. $this->parentGId = $this->arguments[$key + 1];
  149. $deleteClass[] = $key;
  150. $deleteClass[] = $key + 1;
  151. }
  152. }
  153. foreach ($deleteClass as $clasKey)
  154. {
  155. unset($this->arguments[$clasKey]);
  156. }
  157. return $this;
  158. }
  159. protected function classExist($name)
  160. {
  161. return class_exists($name);
  162. }
  163. public static function updatePids($name, $id = '')
  164. {
  165. touch(ModuleConstants::getModuleRootDir() . DS . 'storage' . DS . 'crons' . DS . $name. 'Pid' . $id . '.php');
  166. }
  167. public static function removePids($name, $id = '')
  168. {
  169. unlink(ModuleConstants::getModuleRootDir() . DS . 'storage' . DS . 'crons' . DS . $name. 'Pid' . $id . '.php');
  170. }
  171. public static function existPids($name, $id = '')
  172. {
  173. return file_exists(ModuleConstants::getModuleRootDir() . DS . 'storage' . DS . 'crons' . DS . $name. 'Pid' . $id . '.php');
  174. }
  175. public function createPids($file)
  176. {
  177. return file_put_contents($file, '<?php die(); '.getmypid());
  178. }
  179. public function isChildRunning($name, $id)
  180. {
  181. $file = $this->cronPidsPath . $name. 'Pid' . $id . '.php';
  182. if($this->fileValidator->isExist($file) && filemtime($file) > time() - 7200)
  183. {
  184. return true;
  185. }
  186. return false;
  187. }
  188. private function isCronRunning($name)
  189. {
  190. if ($this->isParent === false)
  191. {
  192. $file = $this->cronPidsPath . $name. 'Pid' . $this->childId . '.php';
  193. if($this->fileValidator->isExist($file) && filemtime($file) > time() - 7200)
  194. {
  195. return true;
  196. }
  197. if($this->createPids($file) === false)
  198. {
  199. return true;
  200. }
  201. }
  202. return false;
  203. /**
  204. if(PHP_OS == "Linux")
  205. {
  206. $out = null;
  207. exec("ps aux | grep 'php -q.*modules/addons/AdvancedBilling/cron/cron.php$' | grep -v 'grep'", $out);
  208. if(count($out) > 1)
  209. {
  210. return true;
  211. }
  212. }
  213. **/
  214. }
  215. private function isInCliMode()
  216. {
  217. if (in_array(php_sapi_name(), self::AVAILABLE_MODES))
  218. {
  219. return true;
  220. }
  221. else
  222. {
  223. return false;
  224. }
  225. }
  226. protected function loadDefaultCronManager()
  227. {
  228. $defaultCron = CronTabs::ifDefaultCronManager()->first();
  229. if ($this->isParent && $defaultCron && $defaultCron->status != 'start')
  230. {
  231. CronTabs::ifDefaultCronManager()->updateStatus('start');
  232. unset($defaultCron);
  233. }
  234. elseif ($this->isParent && !$defaultCron)
  235. {
  236. CronTabs::create([
  237. 'name' => CronTabs::DEFAULT_CRON_MANAGER_NAME,
  238. 'status' => 'start',
  239. 'params' => [],
  240. 'errors' => []
  241. ]);
  242. }
  243. return $this;
  244. }
  245. protected function beforeRunTask($className)
  246. {
  247. if ($this->isParent)
  248. {
  249. if (CronTabs::ifDefaultCronManager()->getQuery()->first()->status == 'stop')
  250. {
  251. exit;
  252. }
  253. $cronTask = CronTabs::ifName($className)->getQuery()->first();
  254. if ($cronTask && $cronTask->status != 'stop')
  255. {
  256. return false;
  257. }
  258. $params = [
  259. 'parentId' => getmypid(),
  260. 'parentUId' => $this->parentUId,
  261. 'parentGId' => $this->parentGId
  262. ];
  263. if ($cronTask)
  264. {
  265. CronTabs::ifName($className)->updateStatus('start', $params);
  266. }
  267. else
  268. {
  269. CronTabs::create([
  270. 'name' => $className,
  271. 'status' => 'start',
  272. 'params' => $params,
  273. 'errors' => []
  274. ]);
  275. }
  276. usleep(500000);
  277. $cronTask = (new CronTabs())->ifName($className)->getQuery()->first();
  278. if ($this->isParent && json_decode($cronTask->params)->parentId != getmypid())
  279. {
  280. return false;
  281. }
  282. }
  283. return true;
  284. }
  285. protected function afterRunTask($className)
  286. {
  287. if ($this->isParent)
  288. {
  289. CronTabs::ifName($className)->updateStatus();
  290. }
  291. return $this;
  292. }
  293. }