Queue.php 3.8 KB

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