Queue.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2021 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免费声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  13. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  14. // +----------------------------------------------------------------------
  15. namespace app\admin\controller;
  16. use app\admin\model\SystemQueue;
  17. use think\admin\Controller;
  18. use think\admin\helper\QueryHelper;
  19. use think\admin\service\AdminService;
  20. use think\admin\service\ProcessService;
  21. use think\admin\service\QueueService;
  22. use think\exception\HttpResponseException;
  23. /**
  24. * 系统任务管理
  25. * Class Queue
  26. * @package app\admin\controller
  27. */
  28. class Queue extends Controller
  29. {
  30. /**
  31. * 绑定数据表
  32. * @var string
  33. */
  34. private $table = 'SystemQueue';
  35. /**
  36. * 系统任务管理
  37. * @auth true
  38. * @menu true
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public function index()
  44. {
  45. $this->_query(SystemQueue::class)->layTable(function () {
  46. $this->title = '系统任务管理';
  47. $this->iswin = ProcessService::instance()->iswin();
  48. // 超级管理面板
  49. if ($this->isSuper = AdminService::instance()->isSuper()) {
  50. $process = ProcessService::instance();
  51. if ($process->iswin() || empty($_SERVER['USER'])) {
  52. $this->command = $process->think('xadmin:queue start');
  53. } else {
  54. $this->command = "sudo -u {$_SERVER['USER']} {$process->think('xadmin:queue start')}";
  55. }
  56. }
  57. // 任务状态统计
  58. $this->total = ['dos' => 0, 'pre' => 0, 'oks' => 0, 'ers' => 0];
  59. $query = $this->app->db->name($this->table)->field('status,count(1) count');
  60. $query->group('status')->select()->map(function ($item) {
  61. if ($item['status'] === 1) $this->total['pre'] = $item['count'];
  62. if ($item['status'] === 2) $this->total['dos'] = $item['count'];
  63. if ($item['status'] === 3) $this->total['oks'] = $item['count'];
  64. if ($item['status'] === 4) $this->total['ers'] = $item['count'];
  65. });
  66. }, function (QueryHelper $query) {
  67. $query->equal('status')->like('code,title,command');
  68. $query->timeBetween('enter_time,exec_time')->dateBetween('create_at');
  69. });
  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(bool $state)
  93. {
  94. if ($state) {
  95. $this->success('重启任务成功!');
  96. }
  97. }
  98. /**
  99. * 清理运行数据
  100. * @auth true
  101. */
  102. public function clean()
  103. {
  104. $this->_queue('定时清理系统运行数据', "xadmin:queue clean", 0, [], 0, 3600);
  105. }
  106. /**
  107. * 删除系统任务
  108. * @auth true
  109. * @throws \think\db\exception\DbException
  110. */
  111. public function remove()
  112. {
  113. $this->_delete($this->table);
  114. }
  115. }