RabbitMQWorker.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use PhpAmqpLib\Message\AMQPMessage;
  5. use App\Jobs\ProcessAITranslateJob;
  6. use App\Jobs\BaseRabbitMQJob;
  7. use Illuminate\Support\Facades\Log;
  8. use PhpAmqpLib\Exception\AMQPTimeoutException;
  9. use PhpAmqpLib\Wire\AMQPTable;
  10. use App\Services\RabbitMQService;
  11. class RabbitMQWorker extends Command
  12. {
  13. /**
  14. * The name and signature of the console command.
  15. * php -d memory_limit=128M artisan rabbitmq:consume ai_translate
  16. * @var string
  17. */
  18. protected $signature = 'rabbitmq:consume {queue} {--reset-loop-count}';
  19. protected $description = '消费 RabbitMQ 队列消息';
  20. private $connection;
  21. private $channel;
  22. private $processedCount = 0;
  23. private $maxLoopCount = 0;
  24. private $queueName;
  25. private $queueConfig;
  26. private $shouldStop = false;
  27. private $timeout = 15;
  28. private $job = null;
  29. public function handle(RabbitMQService $consume)
  30. {
  31. if (\App\Tools\Tools::isStop()) {
  32. return 0;
  33. }
  34. $this->queueName = $this->argument('queue');
  35. $this->queueConfig = config("mint.rabbitmq.queues.{$this->queueName}");
  36. if (!$this->queueConfig) {
  37. $this->error("队列 {$this->queueName} 的配置不存在");
  38. return 1;
  39. }
  40. $this->maxLoopCount = $this->queueConfig['max_loop_count'];
  41. $this->info("启动 RabbitMQ Worker");
  42. $this->info("队列: {$this->queueName}");
  43. $this->info("最大循环次数: {$this->maxLoopCount}");
  44. $this->info("重试次数: {$this->queueConfig['retry_times']}");
  45. try {
  46. $consume->setupQueue($this->queueName);
  47. $this->channel = $consume->getChannel();
  48. $this->startConsuming();
  49. } catch (\Exception $e) {
  50. $this->error("Worker 启动失败: " . $e->getMessage());
  51. Log::error("RabbitMQ Worker 启动失败", [
  52. 'queue' => $this->queueName,
  53. 'error' => $e->getMessage()
  54. ]);
  55. return 1;
  56. } finally {
  57. $this->cleanup();
  58. }
  59. return 0;
  60. }
  61. private function startConsuming()
  62. {
  63. $callback = function (AMQPMessage $msg) {
  64. $this->processMessage($msg);
  65. };
  66. $this->channel->basic_consume(
  67. $this->queueName,
  68. '', // consumer_tag
  69. false, // no_local
  70. false, // no_ack
  71. false, // exclusive
  72. false, // nowait
  73. $callback
  74. );
  75. $this->info("开始消费消息... 按 Ctrl+C 退出");
  76. // 设置信号处理
  77. if (extension_loaded('pcntl')) {
  78. pcntl_signal(SIGTERM, [$this, 'handleSignal']);
  79. pcntl_signal(SIGINT, [$this, 'handleSignal']);
  80. }
  81. while ($this->channel->is_consuming() && !$this->shouldStop) {
  82. try {
  83. $this->channel->wait(null, false, $this->timeout);
  84. } catch (AMQPTimeoutException $e) {
  85. // ignore it
  86. }
  87. if (extension_loaded('pcntl')) {
  88. pcntl_signal_dispatch();
  89. }
  90. // 检查是否达到最大循环次数
  91. if ($this->processedCount >= $this->maxLoopCount) {
  92. $this->info("达到最大循环次数 ({$this->maxLoopCount}),Worker 自动退出");
  93. break;
  94. }
  95. if (\App\Tools\Tools::isStop()) {
  96. //检测到停止标记
  97. break;
  98. }
  99. }
  100. }
  101. private function processMessage(AMQPMessage $msg)
  102. {
  103. try {
  104. Log::info('processMessage start', ['message_id' => $msg->get('message_id')]);
  105. $data = json_decode($msg->getBody());
  106. $this->info("processMessage start " . $msg->get('message_id') . '[' . count($data) . ']');
  107. if (json_last_error() !== JSON_ERROR_NONE) {
  108. throw new \Exception("JSON 解析失败: " . json_last_error_msg());
  109. }
  110. // 获取重试次数(从消息头中获取)
  111. $retryCount = 0;
  112. if ($msg->has('application_headers')) {
  113. $headers = $msg->get('application_headers')->getNativeData();
  114. $retryCount = $headers['retry_count'] ?? 0;
  115. }
  116. // 根据队列类型创建对应的 Job
  117. $this->job = $this->createJob($msg->get('message_id'), $data, $retryCount);
  118. try {
  119. // 执行业务逻辑
  120. $successful = $this->job->handle();
  121. if ($successful) {
  122. // 成功处理,确认消息
  123. $msg->ack();
  124. }
  125. $this->processedCount++;
  126. $this->info("消息处理成功 [{$this->processedCount}/{$this->maxLoopCount}]");
  127. } catch (\Exception $e) {
  128. $this->handleJobException($msg, $data, $retryCount, $e);
  129. }
  130. } catch (\Exception $e) {
  131. $this->error("消息处理异常: " . $e->getMessage());
  132. Log::error("RabbitMQ 消息处理异常", [
  133. 'queue' => $this->queueName,
  134. 'error' => $e->getMessage(),
  135. 'message_body' => $msg->getBody()
  136. ]);
  137. // 拒绝消息并发送到死信队列
  138. //$msg->nack(false, false);
  139. $this->sendToDeadLetterQueue($data, $e);
  140. $msg->ack(); // 确认原消息以避免重复
  141. $this->error("已发送到死信队列");
  142. $this->processedCount++;
  143. }
  144. }
  145. private function createJob(string $messageId, array $data, int $retryCount): BaseRabbitMQJob
  146. {
  147. // 根据队列名称创建对应的 Job 实例
  148. switch ($this->queueName) {
  149. case 'ai_translate':
  150. return new ProcessAITranslateJob($this->queueName, $messageId, $data, $retryCount);
  151. // 可以添加更多队列类型
  152. default:
  153. throw new \Exception("未知的队列类型: {$this->queueName}");
  154. }
  155. }
  156. private function handleJobException(AMQPMessage $msg, array $data, int $retryCount, \Exception $e)
  157. {
  158. $maxRetries = $this->queueConfig['retry_times'];
  159. if ($retryCount < $maxRetries - 1) {
  160. // 还有重试机会,重新入队
  161. $this->requeueMessage($msg, $data, $retryCount + 1);
  162. $this->info("消息重新入队,重试次数: " . ($retryCount + 1) . "/{$maxRetries}");
  163. } else {
  164. // 超过重试次数,发送到死信队列
  165. $this->sendToDeadLetterQueue($data, $e);
  166. $msg->ack(); // 确认原消息以避免重复
  167. $this->error("消息超过最大重试次数,已发送到死信队列 ");
  168. Log::error("消息超过最大重试次数,已发送到死信队列 message_id=" . $msg->get('message_id'));
  169. }
  170. $this->processedCount++;
  171. }
  172. private function requeueMessage(AMQPMessage $msg, array $data, int $newRetryCount)
  173. {
  174. // 添加重试计数到消息头
  175. // 使用 AMQPTable 包装头部数据
  176. $headers = new AMQPTable([
  177. 'retry_count' => $newRetryCount,
  178. 'original_queue' => $this->queueName,
  179. 'retry_timestamp' => time()
  180. ]);
  181. $newMsg = new AMQPMessage(
  182. json_encode($data, JSON_UNESCAPED_UNICODE),
  183. [
  184. 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT,
  185. 'timestamp' => time(),
  186. 'message_id' => $msg->get('message_id'),
  187. 'application_headers' => $headers,
  188. "content_type" => 'application/json; charset=utf-8'
  189. ]
  190. );
  191. // 发布到同一队列
  192. $this->channel->basic_publish($newMsg, '', $this->queueName);
  193. // 确认原消息
  194. $msg->ack();
  195. }
  196. private function sendToDeadLetterQueue(array $data, \Exception $e)
  197. {
  198. $dlqName = $this->queueConfig['dead_letter_queue'];
  199. $dlqData = [
  200. 'original_message' => $data,
  201. 'failure_reason' => $e->getMessage(),
  202. 'failed_at' => date('Y-m-d H:i:s'),
  203. 'queue' => $this->queueName,
  204. 'max_retries' => $this->queueConfig['retry_times']
  205. ];
  206. $dlqMsg = new AMQPMessage(
  207. json_encode($dlqData),
  208. ['delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT]
  209. );
  210. $this->channel->basic_publish($dlqMsg, '', $dlqName);
  211. Log::error("消息发送到死信队列", [
  212. 'original_queue' => $this->queueName,
  213. 'dead_letter_queue' => $dlqName,
  214. 'error' => $e->getMessage()
  215. ]);
  216. }
  217. public function handleSignal($signal)
  218. {
  219. $this->info("接收到退出信号,正在优雅关闭...");
  220. $this->shouldStop = true;
  221. if ($this->job) {
  222. $this->job->stop();
  223. }
  224. if ($this->channel && $this->channel->is_consuming()) {
  225. //$this->channel->basic_cancel_on_shutdown(true);
  226. $this->channel->basic_cancel('');
  227. }
  228. }
  229. private function cleanup()
  230. {
  231. try {
  232. if ($this->channel) {
  233. $this->channel->close();
  234. }
  235. if ($this->connection) {
  236. $this->connection->close();
  237. }
  238. $this->info("连接已关闭,处理了 {$this->processedCount} 条消息");
  239. } catch (\Exception $e) {
  240. $this->error("清理资源时出错: " . $e->getMessage());
  241. }
  242. }
  243. }