From d5e09d71525bac4925185a2dbbf3f22dc1d21e71 Mon Sep 17 00:00:00 2001 From: Nick Zeng Date: Tue, 24 Feb 2026 15:53:07 +0800 Subject: [PATCH] update --- backend/app/Command/TestErrorQueueCommand.php | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 backend/app/Command/TestErrorQueueCommand.php diff --git a/backend/app/Command/TestErrorQueueCommand.php b/backend/app/Command/TestErrorQueueCommand.php new file mode 100644 index 0000000..bbd0b6d --- /dev/null +++ b/backend/app/Command/TestErrorQueueCommand.php @@ -0,0 +1,130 @@ +setDescription('测试向 errors.queue 发送消息,验证 consumer 权限'); + } + + public function handle() + { + $producer = ApplicationContext::getContainer()->get(Producer::class); + + $testMessage = [ + 'test_id' => uniqid('test_', true), + 'message' => 'This is a test error message', + 'timestamp' => date('c'), + ]; + + $this->line("======================================"); + $this->line("测试 1: 使用空 exchange (默认 exchange) 发送到 errors.queue"); + $this->line("======================================"); + + try { + $producer->produce( + new class($testMessage) extends \Hyperf\Amqp\Message\ProducerMessage { + protected string $exchange = ''; + protected string|array $routingKey = 'errors.queue'; + protected string $poolName = 'default_consumer'; + protected array $properties = [ + 'delivery_mode' => 2, + ]; + + public function __construct(array $data) + { + $this->payload = $data; + } + } + ); + $this->info("✓ 测试 1 成功: 消息已发送到 errors.queue"); + } catch (\Throwable $e) { + $this->error("✗ 测试 1 失败: " . $e->getMessage()); + } + + $this->line(""); + $this->line("======================================"); + $this->line("测试 2: 使用通用 errors.exchange 发送到 errors.queue"); + $this->line("======================================"); + + try { + $producer->produce( + new class($testMessage) extends \Hyperf\Amqp\Message\ProducerMessage { + protected string $exchange = 'errors.exchange'; + protected string|array $routingKey = 'error.test'; + protected string $poolName = 'default_consumer'; + protected array $properties = [ + 'delivery_mode' => 2, + ]; + + public function __construct(array $data) + { + $this->payload = $data; + } + } + ); + $this->info("✓ 测试 2 成功: 消息已通过 errors.exchange 发送"); + } catch (\Throwable $e) { + $this->error("✗ 测试 2 失败: " . $e->getMessage()); + } + + $this->line(""); + $this->line("======================================"); + $this->line("测试 3: 使用 ErrorProducer 类发送到 errors.queue"); + $this->line("======================================"); + + try { + // 模拟一个原始消息 + $originalData = [ + 'message_id' => 'test_msg_123', + 'timestamp' => date('c'), + 'platform' => 'tmall', + 'data_type' => 'order', + 'meta' => [ + 'platform_id' => 2, + 'company_id' => 1, + 'store_id' => 10, + ], + 'data' => ['test' => 'data'], + ]; + + $amqpMessage = new AMQPMessage(json_encode($originalData)); + $testException = new Exception('Test error for ErrorProducer'); + + $errorProducer = new ErrorProducer($amqpMessage, $testException, 3); + $producer->produce($errorProducer); + + $this->info("✓ 测试 3 成功: 使用 ErrorProducer 类发送消息"); + } catch (\Throwable $e) { + $this->error("✗ 测试 3 失败: " . $e->getMessage()); + } + + $this->line(""); + $this->line("现在请检查 errors.queue 中是否有消息:"); + $this->line("php bin/hyperf.php app:mq:status"); + + return 0; + } +}