Queue.php 3.2 KB

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