Queue.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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::instance()->iswin();
  44. if ($this->super = AdminService::instance()->isSuper()) {
  45. $process = ProcessService::instance();
  46. if ($process->iswin() || empty($_SERVER['USER'])) {
  47. $this->command = $process->think('xadmin:queue start');
  48. } else {
  49. $this->command = "sudo -u {$_SERVER['USER']} {$process->think('xadmin:queue start')}";
  50. }
  51. }
  52. }, function (QueryHelper $query) {
  53. $query->equal('status')->like('code|title#title,command');
  54. $query->timeBetween('enter_time,exec_time')->dateBetween('create_at');
  55. });
  56. }
  57. /**
  58. * 分页数据回调处理
  59. * @param array $data
  60. * @param array $result
  61. * @return void
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\DbException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. */
  66. protected function _index_page_filter(array $data, array &$result)
  67. {
  68. $result['extra'] = ['dos' => 0, 'pre' => 0, 'oks' => 0, 'ers' => 0];
  69. SystemQueue::mk()->field('status,count(1) count')->group('status')->select()->map(function ($item) use (&$result) {
  70. if ($item['status'] === 1) $result['extra']['pre'] = $item['count'];
  71. if ($item['status'] === 2) $result['extra']['dos'] = $item['count'];
  72. if ($item['status'] === 3) $result['extra']['oks'] = $item['count'];
  73. if ($item['status'] === 4) $result['extra']['ers'] = $item['count'];
  74. });
  75. }
  76. /**
  77. * 重启系统任务
  78. * @auth true
  79. */
  80. public function redo()
  81. {
  82. try {
  83. $data = $this->_vali(['code.require' => '任务编号不能为空!']);
  84. $queue = QueueService::instance()->initialize($data['code'])->reset();
  85. $queue->progress(1, '>>> 任务重置成功 <<<', 0.00);
  86. $this->success('任务重置成功!', $queue->code);
  87. } catch (HttpResponseException $exception) {
  88. throw $exception;
  89. } catch (Exception $exception) {
  90. $this->error($exception->getMessage());
  91. }
  92. }
  93. /**
  94. * 清理运行数据
  95. * @auth true
  96. */
  97. public function clean()
  98. {
  99. $this->_queue('定时清理系统运行数据', "xadmin:queue clean", 0, [], 0, 3600);
  100. }
  101. /**
  102. * 删除系统任务
  103. * @auth true
  104. */
  105. public function remove()
  106. {
  107. SystemQueue::mDelete();
  108. }
  109. }