Process.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace easyTask\Process;
  3. use easyTask\Command;
  4. use easyTask\Env;
  5. use easyTask\Error;
  6. use easyTask\Helper;
  7. use easyTask\Terminal;
  8. use \Event as Event;
  9. use \EventBase as EventBase;
  10. use \EventConfig as EventConfig;
  11. use \Exception as Exception;
  12. use \Throwable as Throwable;
  13. /**
  14. * Class Process
  15. * @package easyTask\Process
  16. */
  17. abstract class Process
  18. {
  19. /**
  20. * 进程启动时间
  21. * @var int
  22. */
  23. protected $startTime;
  24. /**
  25. * 任务总数
  26. * @var int
  27. */
  28. protected $taskCount;
  29. /**
  30. * 任务列表
  31. * @var array
  32. */
  33. protected $taskList;
  34. /**
  35. * 进程命令管理
  36. * @var Command
  37. */
  38. protected $commander;
  39. /**
  40. * 构造函数
  41. * @param array $taskList
  42. */
  43. public function __construct($taskList)
  44. {
  45. $this->startTime = time();
  46. $this->taskList = $taskList;
  47. $this->setTaskCount();
  48. $this->commander = new Command();
  49. }
  50. /**
  51. * 开始运行
  52. */
  53. abstract public function start();
  54. /**
  55. * 运行状态
  56. */
  57. public function status()
  58. {
  59. //发送命令
  60. $this->commander->send([
  61. 'type' => 'status',
  62. 'msgType' => 2
  63. ]);
  64. $this->masterWaitExit();
  65. }
  66. /**
  67. * 停止运行
  68. * @param bool $force 是否强制
  69. */
  70. public function stop($force = false)
  71. {
  72. //发送命令
  73. $force = $force ?: true;
  74. $this->commander->send([
  75. 'type' => 'stop',
  76. 'force' => $force,
  77. 'msgType' => 2
  78. ]);
  79. }
  80. /**
  81. * 初始化任务数量
  82. */
  83. protected function setTaskCount()
  84. {
  85. $count = 0;
  86. foreach ($this->taskList as $key => $item) {
  87. $count += (int)$item['used'];
  88. }
  89. $this->taskCount = $count;
  90. }
  91. /**
  92. * 检查是否可写标准输出日志
  93. * @return bool
  94. */
  95. protected function canWriteStd()
  96. {
  97. return Env::get('daemon') && !Env::get('closeStdOutLog');
  98. }
  99. /**
  100. * 执行任务代码
  101. * @param array $item
  102. * @throws
  103. */
  104. protected function execute($item)
  105. {
  106. //根据任务类型执行
  107. $daemon = Env::get('daemon');
  108. //Std_Start
  109. if ($this->canWriteStd()) ob_start();
  110. try {
  111. $type = $item['type'];
  112. switch ($type) {
  113. case 1:
  114. $func = $item['func'];
  115. $func();
  116. break;
  117. case 2:
  118. call_user_func([$item['class'], $item['func']]);
  119. break;
  120. case 3:
  121. $object = new $item['class']();
  122. call_user_func([$object, $item['func']]);
  123. break;
  124. default:
  125. // 原始代码保留
  126. // $result = shell_exec($item['command']);
  127. // if ($result) {
  128. // echo $result . PHP_EOL;
  129. // Helper::output($result);
  130. // }
  131. // if ($result === false) {
  132. // $errorResult = 'failed to execute ' . $item['alas'] . ' task' . PHP_EOL;
  133. // Helper::output($errorResult);
  134. // }
  135. // 修改运行方式 为Terminal
  136. Terminal::instance(1, $item['alas'])->exec($item['command']);
  137. }
  138. } catch (Exception $exception) {
  139. if (Helper::isWin()) {
  140. Helper::showException($exception, 'exception', !$daemon);
  141. } else {
  142. if (!$daemon) throw $exception;
  143. Helper::writeLog(Helper::formatException($exception));
  144. }
  145. } catch (Throwable $exception) {
  146. if (Helper::isWin()) {
  147. Helper::showException($exception, 'exception', !$daemon);
  148. } else {
  149. if (!$daemon) throw $exception;
  150. Helper::writeLog(Helper::formatException($exception));
  151. }
  152. }
  153. //Std_End
  154. if ($this->canWriteStd()) {
  155. $stdChar = ob_get_contents();
  156. if ($stdChar) Helper::saveStdChar($stdChar);
  157. ob_end_clean();
  158. }
  159. //检查常驻进程存活
  160. $this->checkDaemonForExit($item);
  161. }
  162. /**
  163. * 执行任务
  164. * @param array $item
  165. * @throws Throwable
  166. */
  167. protected function executeInvoker($item)
  168. {
  169. if ($item['time'] === 0) {
  170. $this->invokerByDirect($item);
  171. } else {
  172. Env::get('canEvent') ? $this->invokeByEvent($item) : $this->invokeByDefault($item);
  173. }
  174. }
  175. /**
  176. * 通过Event事件执行
  177. * @param array $item
  178. */
  179. protected function invokeByEvent($item)
  180. {
  181. //创建Event事件
  182. $eventConfig = new EventConfig();
  183. $eventBase = new EventBase($eventConfig);
  184. $event = new Event($eventBase, -1, Event::TIMEOUT | Event::PERSIST, function () use ($item) {
  185. try {
  186. $this->execute($item);
  187. } catch (Throwable $exception) {
  188. $type = 'exception';
  189. Error::report($type, $exception);
  190. $this->checkDaemonForExit($item);
  191. }
  192. });
  193. //添加事件
  194. $event->add($item['time']);
  195. //事件循环
  196. $eventBase->loop();
  197. }
  198. /**
  199. * 普通执行
  200. * @param array $item
  201. * @throws Throwable
  202. */
  203. protected function invokerByDirect($item)
  204. {
  205. $this->execute($item);
  206. exit;
  207. }
  208. /**
  209. * 主进程等待结束退出
  210. */
  211. protected function masterWaitExit()
  212. {
  213. $i = $this->taskCount + 3;
  214. while ($i--) {
  215. //接收汇报
  216. $this->commander->waitCommandForExecute(1, function ($report) {
  217. if ($report['type'] == 'status' && $report['status']) {
  218. Helper::showTable($report['status']);
  219. }
  220. }, $this->startTime);
  221. //CPU休息
  222. Helper::sleep(1);
  223. }
  224. Helper::showInfo('this cpu is too busy,please use status command try again');
  225. exit;
  226. }
  227. }