Queue.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\SystemQueue;
  4. use think\admin\Controller;
  5. use think\admin\helper\QueryHelper;
  6. use think\admin\service\AdminService;
  7. use think\admin\service\ProcessService;
  8. use think\admin\service\QueueService;
  9. use think\exception\HttpResponseException;
  10. /**
  11. * 系统任务管理
  12. * Class Queue
  13. * @package app\admin\controller
  14. */
  15. class Queue extends Controller
  16. {
  17. /**
  18. * 绑定数据表
  19. * @var string
  20. */
  21. private $table = 'SystemQueue';
  22. /**
  23. * 系统任务管理
  24. * @auth true
  25. * @menu true
  26. * @throws \think\db\exception\DataNotFoundException
  27. * @throws \think\db\exception\DbException
  28. * @throws \think\db\exception\ModelNotFoundException
  29. */
  30. public function index()
  31. {
  32. $this->_query(SystemQueue::class)->layTable(function () {
  33. $this->title = '系统任务管理';
  34. $this->iswin = ProcessService::instance()->iswin();
  35. // 超级管理面板
  36. if ($this->isSuper = AdminService::instance()->isSuper()) {
  37. $process = ProcessService::instance();
  38. if ($process->iswin() || empty($_SERVER['USER'])) {
  39. $this->command = $process->think('xadmin:queue start');
  40. } else {
  41. $this->command = "sudo -u {$_SERVER['USER']} {$process->think('xadmin:queue start')}";
  42. }
  43. }
  44. // 任务状态统计
  45. $this->total = ['dos' => 0, 'pre' => 0, 'oks' => 0, 'ers' => 0];
  46. $query = $this->app->db->name($this->table)->field('status,count(1) count');
  47. $query->group('status')->select()->map(function ($item) {
  48. if ($item['status'] === 1) $this->total['pre'] = $item['count'];
  49. if ($item['status'] === 2) $this->total['dos'] = $item['count'];
  50. if ($item['status'] === 3) $this->total['oks'] = $item['count'];
  51. if ($item['status'] === 4) $this->total['ers'] = $item['count'];
  52. });
  53. }, function (QueryHelper $query) {
  54. $query->equal('status')->like('code,title,command');
  55. $query->timeBetween('enter_time,exec_time')->dateBetween('create_at');
  56. });
  57. }
  58. /**
  59. * 重启系统任务
  60. * @auth true
  61. */
  62. public function redo()
  63. {
  64. try {
  65. $data = $this->_vali(['code.require' => '任务编号不能为空!']);
  66. $queue = QueueService::instance()->initialize($data['code'])->reset();
  67. $queue->progress(1, '>>> 任务重置成功 <<<', 0.00);
  68. $this->success('任务重置成功!', $queue->code);
  69. } catch (HttpResponseException $exception) {
  70. throw $exception;
  71. } catch (\Exception $exception) {
  72. $this->error($exception->getMessage());
  73. }
  74. }
  75. /**
  76. * 重启任务结果处理
  77. * @param boolean $state
  78. */
  79. protected function _redo_save_result(bool $state)
  80. {
  81. if ($state) {
  82. $this->success('重启任务成功!');
  83. }
  84. }
  85. /**
  86. * 清理运行数据
  87. * @auth true
  88. */
  89. public function clean()
  90. {
  91. $this->_queue('定时清理系统运行数据', "xadmin:queue clean", 0, [], 0, 3600);
  92. }
  93. /**
  94. * 删除系统任务
  95. * @auth true
  96. * @throws \think\db\exception\DbException
  97. */
  98. public function remove()
  99. {
  100. $this->_delete($this->table);
  101. }
  102. }