Queue.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\api;
  16. use Exception;
  17. use think\admin\Controller;
  18. use think\admin\service\AdminService;
  19. use think\admin\service\QueueService;
  20. use think\exception\HttpResponseException;
  21. /**
  22. * 后台任务通用接口
  23. * Class Queue
  24. * @package app\admin\controller\api
  25. */
  26. class Queue extends Controller
  27. {
  28. /**
  29. * 任务进度查询
  30. * @login true
  31. * @throws \think\admin\Exception
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\DbException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. */
  36. public function progress()
  37. {
  38. $input = $this->_vali(['code.require' => '任务编号不能为空!']);
  39. $queue = QueueService::instance()->initialize($input['code']);
  40. $this->success('获取任务进度成功!', $queue->progress());
  41. }
  42. /**
  43. * WIN停止监听进程
  44. * @login true
  45. */
  46. public function stop()
  47. {
  48. try {
  49. $message = $this->app->console->call('xadmin:queue', ['stop'])->fetch();
  50. if (stripos($message, 'sent end signal to process')) {
  51. sysoplog('系统运维管理', '尝试停止后台服务主进程');
  52. $this->success('停止后台服务主进程成功!');
  53. } elseif (stripos($message, 'processes to stop')) {
  54. $this->success('没有找到需要停止的进程!');
  55. } else {
  56. $this->error(nl2br($message));
  57. }
  58. } catch (HttpResponseException $exception) {
  59. throw $exception;
  60. } catch (Exception $exception) {
  61. $this->error($exception->getMessage());
  62. }
  63. }
  64. /**
  65. * WIN创建监听进程
  66. * @login true
  67. */
  68. public function start()
  69. {
  70. try {
  71. $message = $this->app->console->call('xadmin:queue', ['start'])->fetch();
  72. if (stripos($message, 'daemons started successfully for pid')) {
  73. sysoplog('系统运维管理', '尝试启动后台服务主进程');
  74. $this->success('后台服务主进程启动成功!');
  75. } elseif (stripos($message, 'daemons already exist for pid')) {
  76. $this->success('后台服务主进程已经存在!');
  77. } else {
  78. $this->error(nl2br($message));
  79. }
  80. } catch (HttpResponseException $exception) {
  81. throw $exception;
  82. } catch (Exception $exception) {
  83. $this->error($exception->getMessage());
  84. }
  85. }
  86. /**
  87. * 检查任务状态
  88. * @login true
  89. */
  90. public function status()
  91. {
  92. if (AdminService::isSuper()) try {
  93. $message = $this->app->console->call('xadmin:queue', ['status'])->fetch();
  94. if (preg_match('/process.*?\d+.*?running/', $message)) {
  95. echo "<span class='color-green pointer' data-tips-text='{$message}'>已启动</span>";
  96. } else {
  97. echo "<span class='color-red pointer' data-tips-text='{$message}'>未启动</span>";
  98. }
  99. } catch (Exception $exception) {
  100. echo "<span class='color-red pointer' data-tips-text='{$exception->getMessage()}'>异 常</span>";
  101. } else {
  102. echo "<span class='color-red pointer' data-tips-text='只有超级管理员才能操作!'>无权限</span>";
  103. }
  104. }
  105. }