Queue.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 think\admin\Controller;
  17. use think\admin\service\AdminService;
  18. use think\admin\service\ProcessService;
  19. use think\admin\service\QueueService;
  20. use think\exception\HttpResponseException;
  21. /**
  22. * 系统任务管理
  23. * Class Queue
  24. * @package app\admin\controller
  25. */
  26. class Queue extends Controller
  27. {
  28. /**
  29. * 绑定数据表
  30. * @var string
  31. */
  32. private $table = 'SystemQueue';
  33. /**
  34. * 系统任务管理
  35. * @auth true
  36. * @menu true
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public function index()
  42. {
  43. if ($this->isSuper = AdminService::instance()->isSuper()) {
  44. $process = ProcessService::instance();
  45. if ($process->iswin() || empty($_SERVER['USER'])) {
  46. $this->command = $process->think('xadmin:queue start');
  47. } else {
  48. $this->command = "sudo -u {$_SERVER['USER']} {$process->think('xadmin:queue start')}";
  49. }
  50. }
  51. // 任务状态统计
  52. $this->total = ['dos' => 0, 'pre' => 0, 'oks' => 0, 'ers' => 0];
  53. $query = $this->app->db->name($this->table)->field('status,count(1) count');
  54. foreach ($query->group('status')->select()->toArray() as $item) {
  55. if ($item['status'] === 1) $this->total['pre'] = $item['count'];
  56. if ($item['status'] === 2) $this->total['dos'] = $item['count'];
  57. if ($item['status'] === 3) $this->total['oks'] = $item['count'];
  58. if ($item['status'] === 4) $this->total['ers'] = $item['count'];
  59. }
  60. $this->title = '系统任务管理';
  61. $this->iswin = ProcessService::instance()->iswin();
  62. // 任务列表查询及分页
  63. $query = $this->_query($this->table)->dateBetween('create_at')->timeBetween('enter_time,exec_time');
  64. $query->like('code,title,command')->equal('status')->order('loops_time desc,id desc')->page();
  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. * @param boolean $state
  86. */
  87. protected function _redo_save_result(bool $state)
  88. {
  89. if ($state) {
  90. $this->success('重启任务成功!');
  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. * @throws \think\db\exception\DbException
  105. */
  106. public function remove()
  107. {
  108. $this->_delete($this->table);
  109. }
  110. }