Queue.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | framework
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2018 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://framework.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/framework
  12. // +----------------------------------------------------------------------
  13. namespace app\admin\controller;
  14. use library\Controller;
  15. use think\Console;
  16. use think\Db;
  17. /**
  18. * 系统消息任务
  19. * Class Queue
  20. * @package app\admin\controller
  21. */
  22. class Queue extends Controller
  23. {
  24. /**
  25. * 绑定数据表
  26. * @var string
  27. */
  28. protected $table = 'SystemJobsLog';
  29. /**
  30. * 系统消息任务
  31. * @throws \think\Exception
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. * @throws \think\exception\DbException
  35. * @throws \think\exception\PDOException
  36. */
  37. public function index()
  38. {
  39. $this->title = '消息任务管理';
  40. $this->cmd = 'php ' . env('root_path') . 'think xtask:start';
  41. $this->message = Console::call('xtask:state')->fetch();
  42. $this->uris = Db::name($this->table)->distinct(true)->column('uri');
  43. $query = $this->_query($this->table)->dateBetween('create_at,status_at');
  44. $query->equal('status,title,uri')->order('id desc')->page();
  45. }
  46. /**
  47. * 重置失败的任务
  48. */
  49. public function redo()
  50. {
  51. try {
  52. $where = ['id' => $this->request->post('id')];
  53. $info = Db::name($this->table)->where($where)->find();
  54. if (empty($info)) $this->error('需要重置的任务获取异常!');
  55. $data = isset($info['data']) ? json_decode($info['data'], true) : '[]';
  56. \app\admin\service\QueueService::add($info['title'], $info['uri'], $info['later'], $data, $info['double'], $info['desc']);
  57. $this->success('任务重置成功!', url('@admin') . '#' . url('@admin/queue/index'));
  58. } catch (\think\exception\HttpResponseException $exception) {
  59. throw $exception;
  60. } catch (\Exception $e) {
  61. $this->error("任务重置失败,请稍候再试!<br> {$e->getMessage()}");
  62. }
  63. }
  64. /**
  65. * 删除消息任务
  66. */
  67. public function remove()
  68. {
  69. try {
  70. $isNot = false;
  71. foreach (explode(',', $this->request->post('id', '0')) as $id) {
  72. if (!\app\admin\service\QueueService::del($id)) $isNot = true;
  73. }
  74. if (empty($isNot)) $this->_delete($this->table);
  75. $this->success($isNot ? '部分任务删除成功!' : '任务删除成功!');
  76. } catch (\think\exception\HttpResponseException $exception) {
  77. throw $exception;
  78. } catch (\Exception $e) {
  79. $this->error("任务删除失败,请稍候再试!<br> {$e->getMessage()}");
  80. }
  81. }
  82. }