Task.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Created by PhpStorm
  4. * User Julyssn
  5. * Date 2022/12/14 17:24
  6. */
  7. namespace app\manage\controller;
  8. use app\BaseController;
  9. use easyTask\Terminal;
  10. use think\App;
  11. use think\facade\Console;
  12. use think\Response;
  13. class Task extends BaseController
  14. {
  15. /**
  16. * 项目根目录
  17. * @var string
  18. */
  19. protected $rootPath;
  20. protected $taskNames = [
  21. 'schedule' => '计划任务',
  22. 'queue' => '消息队列',
  23. 'worker' => '消息推送',
  24. 'clearStd' => '清理日志',
  25. ];
  26. public function __construct(App $app)
  27. {
  28. parent::__construct($app);
  29. $this->rootPath = root_path();
  30. chdir($this->rootPath);
  31. }
  32. /**
  33. * 任务列表
  34. * @return Response
  35. */
  36. public function getTaskList()
  37. {
  38. $data = $this->taskMsg();
  39. if (!count($data)) {
  40. return warning('');
  41. }
  42. foreach ($data as &$datum) {
  43. $expName = explode('_', $datum['name']);
  44. $datum['remark'] = $this->taskNames[$expName[count($expName) - 1]] ?? "未知任务";
  45. }
  46. unset($datum);
  47. return success('', $data);
  48. }
  49. /**
  50. * 启动全部进程
  51. * @return Response
  52. */
  53. public function startTask()
  54. {
  55. if(strpos(strtolower(PHP_OS), 'win') === 0)
  56. {
  57. return warning("windows启动请运行根目录下的:start_for_win.bat");
  58. }
  59. if (count($this->taskMsg())) {
  60. return warning('进程已启动');
  61. }
  62. // 启动
  63. $out = Terminal::instance(2)->exec('php think task start');
  64. if (!count($this->analysisMsg($out))) {
  65. return warning('启动失败');
  66. }
  67. return success('启动成功');
  68. }
  69. /**
  70. * 强制停止全部进程
  71. * @return Response
  72. */
  73. public function stopTask()
  74. {
  75. if (!count($this->taskMsg())) {
  76. return warning('进程未启动');
  77. }
  78. // 强制停止
  79. Terminal::instance(2)->exec('php think task stop force');
  80. return success('停止成功');
  81. }
  82. /**
  83. * 获取单个任务日志
  84. * @return Response
  85. */
  86. public function getTaskLog()
  87. {
  88. $name = $this->request->param('name');
  89. $path = $this->rootPath . 'runtime' . DIRECTORY_SEPARATOR . 'easy_task' . DIRECTORY_SEPARATOR . 'Std' . DIRECTORY_SEPARATOR;
  90. if (!file_exists($path . 'exec_' . $name . '.std')) {
  91. $expName = explode('_', $name);
  92. $name = $expName[count($expName) - 1];
  93. if (!file_exists($path . 'exec_' . $name . '.std')) {
  94. return warning('日志不存在');
  95. }
  96. }
  97. return success('', file_get_contents($path . 'exec_' . $name . '.std'));
  98. }
  99. /**
  100. * 清理单个任务日志
  101. * @return Response
  102. */
  103. public function clearTaskLog()
  104. {
  105. $name = $this->request->param('name');
  106. $path = $this->rootPath . 'runtime' . DIRECTORY_SEPARATOR . 'easy_task' . DIRECTORY_SEPARATOR . 'Std' . DIRECTORY_SEPARATOR;
  107. if (!file_exists($path . 'exec_' . $name . '.std')) {
  108. $expName = explode('_', $name);
  109. $name = $expName[count($expName) - 1];
  110. if (!file_exists($path . 'exec_' . $name . '.std')) {
  111. return warning('日志不存在');
  112. }
  113. }
  114. file_put_contents($path . 'exec_' . $name . '.std', '');
  115. return success('清理成功');
  116. }
  117. /**
  118. * 获取运行状态
  119. * @return array
  120. */
  121. private function taskMsg()
  122. {
  123. $out = Terminal::instance(2)->exec('php think task status');
  124. return $this->analysisMsg($out);
  125. }
  126. /**
  127. * 解析数据
  128. * @param string $out 带解析数据
  129. * @return array
  130. */
  131. private function analysisMsg(string $out)
  132. {
  133. $re = '/│ *([\w+]+) *│ *([\w+]+)[ ]*│ *([\w+]+|[0-9- :]+) *│ *([\w+]+) *│ *([\w+]+) *│ *([\w+]+) *│/m';
  134. preg_match_all($re, $out, $matches, PREG_SET_ORDER, 0);
  135. if (!count($matches)) {
  136. return [];
  137. }
  138. $data = [];
  139. $names = $matches[0];
  140. unset($names[0]);
  141. $names = array_values($names);
  142. unset($matches[0]);
  143. foreach ($matches as $match) {
  144. $temp = [];
  145. foreach ($match as $key => $item) {
  146. if ($key !== 0) {
  147. $temp[$names[$key - 1]] = $item;
  148. }
  149. }
  150. $data[] = $temp;
  151. }
  152. return $data;
  153. }
  154. }