Queue.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\controller;
  15. use think\admin\Controller;
  16. use think\admin\service\AdminService;
  17. use think\admin\service\ProcessService;
  18. use think\admin\service\QueueService;
  19. use think\exception\HttpResponseException;
  20. /**
  21. * 系统任务管理
  22. * Class Queue
  23. * @package app\admin\controller
  24. */
  25. class Queue extends Controller
  26. {
  27. /**
  28. * 绑定数据表
  29. * @var string
  30. */
  31. protected $table = 'SystemQueue';
  32. /**
  33. * 系统任务管理
  34. * @auth true
  35. * @menu true
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function index()
  41. {
  42. // 检查进程状态
  43. if (AdminService::instance()->isSuper()) try {
  44. $this->process = ProcessService::instance();
  45. if ($this->process->iswin() || empty($_SERVER['USER'])) {
  46. $this->command = $this->process->think('xadmin:queue start');
  47. } else {
  48. $this->command = "sudo -u {$_SERVER['USER']} {$this->process->think('xadmin:queue start')}";
  49. }
  50. $this->message = $this->app->console->call('xadmin:queue', ['status'])->fetch();
  51. $this->listen = preg_match('/process.*?\d+.*?running/', $this->message, $attr);
  52. } catch (\Exception $exception) {
  53. $this->listen = false;
  54. $this->message = $exception->getMessage();
  55. }
  56. // 任务状态统计
  57. $this->total = ['dos' => 0, 'pre' => 0, 'oks' => 0, 'ers' => 0];
  58. $query = $this->app->db->name($this->table)->field('status,count(1) count');
  59. foreach ($query->group('status')->select()->toArray() as $item) {
  60. if ($item['status'] === 1) $this->total['pre'] = $item['count'];
  61. if ($item['status'] === 2) $this->total['dos'] = $item['count'];
  62. if ($item['status'] === 3) $this->total['oks'] = $item['count'];
  63. if ($item['status'] === 4) $this->total['ers'] = $item['count'];
  64. }
  65. $this->title = '系统任务管理';
  66. $this->iswin = ProcessService::instance()->iswin();
  67. // 任务列表查询及分页
  68. $query = $this->_query($this->table)->dateBetween('create_at')->timeBetween('enter_time,exec_time');
  69. $query->like('code,title,command')->equal('status')->order('loops_time desc,id desc')->page();
  70. }
  71. /**
  72. * 重启系统任务
  73. * @auth true
  74. */
  75. public function redo()
  76. {
  77. try {
  78. $data = $this->_vali(['code.require' => '任务编号不能为空!']);
  79. $queue = QueueService::instance()->initialize($data['code'])->reset();
  80. $queue->progress(1, '>>> 任务重置成功 <<<', 0.00);
  81. $this->success('任务重置成功!', $queue->code);
  82. } catch (HttpResponseException $exception) {
  83. throw $exception;
  84. } catch (\Exception $exception) {
  85. $this->error($exception->getMessage());
  86. }
  87. }
  88. /**
  89. * 重启任务结果处理
  90. * @param boolean $state
  91. */
  92. protected function _redo_save_result($state)
  93. {
  94. if ($state) {
  95. $this->success('重启任务成功!');
  96. }
  97. }
  98. /**
  99. * WIN创建监听进程
  100. * @auth true
  101. */
  102. public function start()
  103. {
  104. try {
  105. $message = nl2br($this->app->console->call('xadmin:queue', ['start'])->fetch());
  106. if (stripos($message, 'daemons started successfully for pid')) {
  107. $this->success('任务监听主进程启动成功!');
  108. } elseif (stripos($message, 'daemons already exist for pid')) {
  109. $this->success('任务监听主进程已经存在!');
  110. } else {
  111. $this->error($message);
  112. }
  113. } catch (HttpResponseException $exception) {
  114. throw $exception;
  115. } catch (\Exception $exception) {
  116. $this->error($exception->getMessage());
  117. }
  118. }
  119. /**
  120. * WIN停止监听进程
  121. * @auth true
  122. */
  123. public function stop()
  124. {
  125. try {
  126. $message = nl2br($this->app->console->call('xadmin:queue', ['stop'])->fetch());
  127. if (stripos($message, 'sent end signal to process')) {
  128. $this->success('停止任务监听主进程成功!');
  129. } elseif (stripos($message, 'processes to stop')) {
  130. $this->success('没有找到需要停止的进程!');
  131. } else {
  132. $this->error($message);
  133. }
  134. } catch (HttpResponseException $exception) {
  135. throw $exception;
  136. } catch (\Exception $exception) {
  137. $this->error($exception->getMessage());
  138. }
  139. }
  140. /**
  141. * 创建记录清理任务
  142. * @auth true
  143. */
  144. public function clear()
  145. {
  146. try {
  147. QueueService::instance()->addCleanQueue();
  148. $this->success('创建清理任务成功!');
  149. } catch (HttpResponseException $exception) {
  150. throw $exception;
  151. } catch (\Exception $exception) {
  152. $this->error($exception->getMessage());
  153. }
  154. }
  155. /**
  156. * 删除系统任务
  157. * @auth true
  158. * @throws \think\db\exception\DbException
  159. */
  160. public function remove()
  161. {
  162. $this->_delete($this->table);
  163. }
  164. }