Start.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\queue\task;
  15. use library\command\Task;
  16. use think\console\Input;
  17. use think\console\Output;
  18. use think\Db;
  19. /**
  20. * 检查并创建异步任务监听主进程
  21. * Class Start
  22. * @package app\admin\queue\task
  23. */
  24. class Start extends Task
  25. {
  26. /**
  27. * 指令属性配置
  28. */
  29. protected function configure()
  30. {
  31. $this->setName('xtask:start')->setDescription('[控制]创建异步任务守护监听主进程');
  32. }
  33. /**
  34. * 执行启动操作
  35. * @param Input $input
  36. * @param Output $output
  37. */
  38. protected function execute(Input $input, Output $output)
  39. {
  40. Db::name('SystemQueue')->count();
  41. $this->setBaseProcess();
  42. if (($pid = $this->checkProcess()) > 0) {
  43. $output->info("异步任务监听主进程{$pid}已经启动!");
  44. } else {
  45. $this->setWinProcess();
  46. $this->createProcess();
  47. $this->setBaseProcess();
  48. sleep(1);
  49. if (($pid = $this->checkProcess()) > 0) {
  50. $output->info("异步任务监听主进程{$pid}启动成功!");
  51. } else {
  52. $output->error('异步任务监听主进程创建失败!');
  53. }
  54. }
  55. }
  56. private function setBaseProcess()
  57. {
  58. $this->cmd = "{$this->bin} xtask:listen";
  59. }
  60. private function setWinProcess()
  61. {
  62. if ($this->isWin()) {
  63. $this->cmd = __DIR__ . DIRECTORY_SEPARATOR . "bin" . DIRECTORY_SEPARATOR . "ThinkAdmin.exe {$this->bin} xtask:listen";
  64. }
  65. }
  66. }