Command.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Library for ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2021 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://gitee.com/zoujingli/ThinkLibrary
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 仓库地址 :https://gitee.com/zoujingli/ThinkLibrary
  12. // | github 仓库地址 :https://github.com/zoujingli/ThinkLibrary
  13. // +----------------------------------------------------------------------
  14. declare (strict_types=1);
  15. namespace think\admin;
  16. use think\admin\service\ProcessService;
  17. use think\admin\service\QueueService;
  18. use think\console\Input;
  19. use think\console\Output;
  20. use think\db\exception\DataNotFoundException;
  21. use think\db\exception\DbException;
  22. use think\db\exception\ModelNotFoundException;
  23. /**
  24. * 自定义指令基类
  25. * Class Command
  26. * @package think\admin
  27. */
  28. abstract class Command extends \think\console\Command
  29. {
  30. /**
  31. * 任务控制服务
  32. * @var QueueService
  33. */
  34. protected $queue;
  35. /**
  36. * 进程控制服务
  37. * @var ProcessService
  38. */
  39. protected $process;
  40. /**
  41. * 初始化指令变量
  42. * @param Input $input
  43. * @param Output $output
  44. * @return static
  45. * @throws Exception
  46. * @throws DataNotFoundException
  47. * @throws DbException
  48. * @throws ModelNotFoundException
  49. */
  50. protected function initialize(Input $input, Output $output): Command
  51. {
  52. $this->queue = QueueService::instance();
  53. $this->process = ProcessService::instance();
  54. if (defined('WorkQueueCode')) {
  55. if (!$this->queue instanceof QueueService) {
  56. $this->queue = QueueService::instance();
  57. }
  58. if ($this->queue->code !== WorkQueueCode) {
  59. $this->queue->initialize(WorkQueueCode);
  60. }
  61. }
  62. return $this;
  63. }
  64. /**
  65. * 设置失败消息并结束进程
  66. * @param string $message 消息内容
  67. * @return static
  68. * @throws Exception
  69. */
  70. protected function setQueueError(string $message): Command
  71. {
  72. if (defined('WorkQueueCode')) {
  73. $this->queue->error($message);
  74. } elseif (is_string($message)) {
  75. $this->output->writeln($message);
  76. exit("\r\n");
  77. }
  78. return $this;
  79. }
  80. /**
  81. * 设置成功消息并结束进程
  82. * @param string $message 消息内容
  83. * @return static
  84. * @throws Exception
  85. */
  86. protected function setQueueSuccess(string $message): Command
  87. {
  88. if (defined('WorkQueueCode')) {
  89. $this->queue->success($message);
  90. } elseif (is_string($message)) {
  91. $this->output->writeln($message);
  92. exit("\r\n");
  93. }
  94. return $this;
  95. }
  96. /**
  97. * 设置进度消息并继续执行
  98. * @param null|string $message 进度消息
  99. * @param null|string $progress 进度数值
  100. * @param integer $backline 回退行数
  101. * @return static
  102. */
  103. protected function setQueueProgress(?string $message = null, ?string $progress = null, int $backline = 0): Command
  104. {
  105. if (defined('WorkQueueCode')) {
  106. $this->queue->progress(2, $message, $progress, $backline);
  107. } elseif (is_string($message)) {
  108. $this->output->writeln($message);
  109. }
  110. return $this;
  111. }
  112. /**
  113. * 更新任务进度
  114. * @param integer $total 记录总和
  115. * @param integer $count 当前记录
  116. * @param string $message 文字描述
  117. * @param integer $backline 回退行数
  118. * @return static
  119. */
  120. public function setQueueMessage(int $total, int $count, string $message = '', int $backline = 0): Command
  121. {
  122. $total = $total < 1 ? 1 : $total;
  123. $prefix = str_pad("{$count}", strlen("{$total}"), '0', STR_PAD_LEFT);
  124. return $this->setQueueProgress("[{$prefix}/{$total}] {$message}", sprintf("%.2f", $count / $total * 100), $backline);
  125. }
  126. }