Queue.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.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\ProcessService;
  17. use think\exception\HttpResponseException;
  18. /**
  19. * 系统任务管理
  20. * Class Queue
  21. * @package app\admin\controller
  22. */
  23. class Queue extends Controller
  24. {
  25. /**
  26. * 绑定数据表
  27. * @var string
  28. */
  29. protected $table = 'SystemQueue';
  30. /**
  31. * 系统任务管理
  32. * @auth true
  33. * @menu true
  34. * @throws \think\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\DbException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. */
  38. public function index()
  39. {
  40. if ($this->app->session->get('user.username') === 'admin') {
  41. try {
  42. $this->command = ProcessService::instance()->think('xtask:start');
  43. $this->message = $this->app->console->call('xtask:state')->fetch();
  44. $this->listen = preg_match('/主进程\d+/', $this->message, $attr);
  45. } catch (\Exception $exception) {
  46. $this->message = $exception->getMessage();
  47. }
  48. }
  49. $this->title = '系统任务管理';
  50. $this->iswin = ProcessService::instance()->iswin();
  51. // 任务列表查询分页处理
  52. $query = $this->_query($this->table)->dateBetween('create_at')->timeBetween('enter_time,outer_time');
  53. $query->like('title,command')->equal('status')->order('id desc')->page();
  54. }
  55. /**
  56. * 重启系统任务
  57. * @auth true
  58. * @throws \think\db\exception\DbException
  59. */
  60. public function redo()
  61. {
  62. $this->_save($this->table, ['status' => '1']);
  63. }
  64. /**
  65. * 重启任务结果处理
  66. * @param boolean $state
  67. */
  68. protected function _redo_save_result($state)
  69. {
  70. if ($state) {
  71. $this->success('重启任务成功!');
  72. }
  73. }
  74. /**
  75. * WIN创建监听进程
  76. * @auth true
  77. */
  78. public function start()
  79. {
  80. try {
  81. $message = nl2br($this->app->console->call('xtask:start')->fetch());
  82. preg_match('/主进程\d+/', $message, $attr) ? $this->success($message) : $this->error($message);
  83. } catch (HttpResponseException $exception) {
  84. throw $exception;
  85. } catch (\Exception $e) {
  86. $this->error($e->getMessage());
  87. }
  88. }
  89. /**
  90. * WIN停止监听进程
  91. * @auth true
  92. */
  93. public function stop()
  94. {
  95. try {
  96. $message = nl2br($this->app->console->call('xtask:stop')->fetch());
  97. stripos($message, '成功') !== false ? $this->success($message) : $this->error($message);
  98. } catch (HttpResponseException $exception) {
  99. throw $exception;
  100. } catch (\Exception $e) {
  101. $this->error($e->getMessage());
  102. }
  103. }
  104. /**
  105. * 删除系统任务
  106. * @auth true
  107. * @throws \think\db\exception\DbException
  108. */
  109. public function remove()
  110. {
  111. $this->_delete($this->table);
  112. }
  113. }