Queue.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2022 广州楚才信息科技有限公司 [ 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 Exception;
  17. use think\admin\Controller;
  18. use think\admin\helper\QueryHelper;
  19. use think\admin\model\SystemQueue;
  20. use think\admin\service\AdminService;
  21. use think\admin\service\ProcessService;
  22. use think\admin\service\QueueService;
  23. use think\exception\HttpResponseException;
  24. /**
  25. * 系统任务管理
  26. * Class Queue
  27. * @package app\admin\controller
  28. */
  29. class Queue extends Controller
  30. {
  31. /**
  32. * 系统任务管理
  33. * @auth true
  34. * @menu true
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\DbException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. */
  39. public function index()
  40. {
  41. SystemQueue::mQuery()->layTable(function () {
  42. $this->title = '系统任务管理';
  43. $this->iswin = ProcessService::iswin();
  44. if ($this->super = AdminService::isSuper()) {
  45. $this->command = ProcessService::think('xadmin:queue start');
  46. if (!$this->iswin && !empty($_SERVER['USER'])) {
  47. $this->command = "sudo -u {$_SERVER['USER']} {$this->command}";
  48. }
  49. }
  50. }, function (QueryHelper $query) {
  51. $query->equal('status')->like('code|title#title,command');
  52. $query->timeBetween('enter_time,exec_time')->dateBetween('create_at');
  53. });
  54. }
  55. /**
  56. * 分页数据回调处理
  57. * @param array $data
  58. * @param array $result
  59. * @return void
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\DbException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. */
  64. protected function _index_page_filter(array $data, array &$result)
  65. {
  66. $result['extra'] = ['dos' => 0, 'pre' => 0, 'oks' => 0, 'ers' => 0];
  67. SystemQueue::mk()->field('status,count(1) count')->group('status')->select()->map(function ($item) use (&$result) {
  68. if ($item['status'] === 1) $result['extra']['pre'] = $item['count'];
  69. if ($item['status'] === 2) $result['extra']['dos'] = $item['count'];
  70. if ($item['status'] === 3) $result['extra']['oks'] = $item['count'];
  71. if ($item['status'] === 4) $result['extra']['ers'] = $item['count'];
  72. });
  73. }
  74. /**
  75. * 重启系统任务
  76. * @auth true
  77. */
  78. public function redo()
  79. {
  80. try {
  81. $data = $this->_vali(['code.require' => '任务编号不能为空!']);
  82. $queue = QueueService::instance()->initialize($data['code'])->reset();
  83. $queue->progress(1, '>>> 任务重置成功 <<<', 0.00);
  84. $this->success('任务重置成功!', $queue->code);
  85. } catch (HttpResponseException $exception) {
  86. throw $exception;
  87. } catch (Exception $exception) {
  88. $this->error($exception->getMessage());
  89. }
  90. }
  91. /**
  92. * 清理运行数据
  93. * @auth true
  94. */
  95. public function clean()
  96. {
  97. $this->_queue('定时清理系统运行数据', "xadmin:queue clean", 0, [], 0, 3600);
  98. }
  99. /**
  100. * 删除系统任务
  101. * @auth true
  102. */
  103. public function remove()
  104. {
  105. SystemQueue::mDelete();
  106. }
  107. }