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