update mq controller
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller\Api\V1;
|
||||
|
||||
use App\Controller\AbstractDataController;
|
||||
use App\Middleware\AuthMiddleware;
|
||||
use App\Middleware\PermissionMiddleware;
|
||||
use App\Model\FailedMessage;
|
||||
use Hyperf\HttpServer\Annotation\Controller;
|
||||
use Hyperf\HttpServer\Annotation\Middleware;
|
||||
use Hyperf\HttpServer\Annotation\RequestMapping;
|
||||
use OpenApi\Attributes as OA;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
/**
|
||||
* 失败消息查看接口
|
||||
*
|
||||
* 仅 admin 角色可访问,展示 Consumer 处理失败的消息记录
|
||||
*/
|
||||
#[OA\Tag(name: 'Failed Messages', description: '失败消息查看')]
|
||||
#[Controller(prefix: "/api/v1/failed-messages")]
|
||||
#[Middleware(AuthMiddleware::class)]
|
||||
#[Middleware(PermissionMiddleware::class)]
|
||||
class FailedMessageController extends AbstractDataController
|
||||
{
|
||||
protected function getModelClass(): string
|
||||
{
|
||||
return FailedMessage::class;
|
||||
}
|
||||
|
||||
protected function getListFields(): array
|
||||
{
|
||||
return [
|
||||
'id', 'error_id', 'data_type', 'platform', 'platform_id',
|
||||
'company_id', 'store_id', 'error_type', 'error_message',
|
||||
'retry_count', 'message_id', 'failed_at', 'created_at',
|
||||
];
|
||||
}
|
||||
|
||||
protected function getDetailFields(): array
|
||||
{
|
||||
return [
|
||||
'id', 'error_id', 'data_type', 'platform', 'platform_id',
|
||||
'company_id', 'store_id', 'error_type', 'error_message',
|
||||
'error_code', 'error_trace', 'original_message',
|
||||
'retry_count', 'message_id', 'failed_at', 'created_at',
|
||||
];
|
||||
}
|
||||
|
||||
protected function getAllowedFilters(): array
|
||||
{
|
||||
return [
|
||||
'data_type' => 'exact',
|
||||
'platform_id' => 'exact',
|
||||
'error_type' => 'like',
|
||||
'failed_at_from' => 'date_from',
|
||||
'failed_at_to' => 'date_to',
|
||||
];
|
||||
}
|
||||
|
||||
protected function getDefaultSort(): string
|
||||
{
|
||||
return 'failed_at';
|
||||
}
|
||||
|
||||
/**
|
||||
* 失败消息列表
|
||||
*/
|
||||
#[OA\Get(
|
||||
path: '/api/v1/failed-messages',
|
||||
summary: '失败消息列表',
|
||||
description: '获取 Consumer 处理失败的消息列表,支持分页、按数据类型/平台/时间筛选。仅 admin 可访问。',
|
||||
security: [['bearerAuth' => []]],
|
||||
tags: ['Failed Messages'],
|
||||
parameters: [
|
||||
new OA\Parameter(name: 'page', in: 'query', required: false, schema: new OA\Schema(type: 'integer', default: 1)),
|
||||
new OA\Parameter(name: 'per_page', in: 'query', required: false, schema: new OA\Schema(type: 'integer', default: 15, maximum: 100)),
|
||||
new OA\Parameter(name: 'data_type', in: 'query', required: false, description: '数据类型精确筛选(order/product/refund)', schema: new OA\Schema(type: 'string', enum: ['order', 'product', 'refund'])),
|
||||
new OA\Parameter(name: 'platform_id', in: 'query', required: false, description: '平台 ID 精确筛选', schema: new OA\Schema(type: 'integer')),
|
||||
new OA\Parameter(name: 'error_type', in: 'query', required: false, description: '异常类名模糊搜索', schema: new OA\Schema(type: 'string')),
|
||||
new OA\Parameter(name: 'failed_at_from', in: 'query', required: false, description: '失败时间起始(含)', schema: new OA\Schema(type: 'string', format: 'date', example: '2026-01-01')),
|
||||
new OA\Parameter(name: 'failed_at_to', in: 'query', required: false, description: '失败时间截止(含)', schema: new OA\Schema(type: 'string', format: 'date', example: '2026-12-31')),
|
||||
],
|
||||
responses: [
|
||||
new OA\Response(
|
||||
response: 200,
|
||||
description: '获取成功',
|
||||
content: new OA\JsonContent(properties: [
|
||||
new OA\Property(property: 'code', type: 'integer', example: 0),
|
||||
new OA\Property(property: 'message', type: 'string', example: '获取成功'),
|
||||
new OA\Property(property: 'data', properties: [
|
||||
new OA\Property(property: 'items', type: 'array', items: new OA\Items(ref: '#/components/schemas/FailedMessageList')),
|
||||
new OA\Property(property: 'total', type: 'integer', example: 100),
|
||||
new OA\Property(property: 'page', type: 'integer', example: 1),
|
||||
new OA\Property(property: 'per_page', type: 'integer', example: 15),
|
||||
], type: 'object'),
|
||||
])
|
||||
),
|
||||
new OA\Response(response: 401, description: '未认证', content: new OA\JsonContent(ref: '#/components/schemas/ErrorResponse')),
|
||||
new OA\Response(response: 403, description: '无权限', content: new OA\JsonContent(ref: '#/components/schemas/ErrorResponse')),
|
||||
]
|
||||
)]
|
||||
#[RequestMapping(path: "", methods: "GET")]
|
||||
public function index(): ResponseInterface|array
|
||||
{
|
||||
return parent::index();
|
||||
}
|
||||
|
||||
/**
|
||||
* 失败消息详情
|
||||
*/
|
||||
#[OA\Get(
|
||||
path: '/api/v1/failed-messages/{id}',
|
||||
summary: '失败消息详情',
|
||||
description: '获取失败消息详情,含完整错误堆栈和原始消息体。仅 admin 可访问。',
|
||||
security: [['bearerAuth' => []]],
|
||||
tags: ['Failed Messages'],
|
||||
parameters: [
|
||||
new OA\Parameter(name: 'id', in: 'path', required: true, description: '失败消息 ID', schema: new OA\Schema(type: 'integer')),
|
||||
],
|
||||
responses: [
|
||||
new OA\Response(
|
||||
response: 200,
|
||||
description: '获取成功',
|
||||
content: new OA\JsonContent(properties: [
|
||||
new OA\Property(property: 'code', type: 'integer', example: 0),
|
||||
new OA\Property(property: 'message', type: 'string', example: '获取成功'),
|
||||
new OA\Property(property: 'data', ref: '#/components/schemas/FailedMessageDetail'),
|
||||
])
|
||||
),
|
||||
new OA\Response(response: 401, description: '未认证', content: new OA\JsonContent(ref: '#/components/schemas/ErrorResponse')),
|
||||
new OA\Response(response: 403, description: '无权限', content: new OA\JsonContent(ref: '#/components/schemas/ErrorResponse')),
|
||||
new OA\Response(response: 404, description: '数据不存在', content: new OA\JsonContent(ref: '#/components/schemas/ErrorResponse')),
|
||||
]
|
||||
)]
|
||||
#[RequestMapping(path: "{id}", methods: "GET")]
|
||||
public function show(int $id): ResponseInterface|array
|
||||
{
|
||||
return parent::show($id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
/**
|
||||
* 失败消息模型
|
||||
*
|
||||
* 记录 Consumer 处理失败后发送到错误队列的消息
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $error_id
|
||||
* @property string $data_type
|
||||
* @property string|null $platform
|
||||
* @property int|null $platform_id
|
||||
* @property int|null $company_id
|
||||
* @property int|null $store_id
|
||||
* @property string $error_type
|
||||
* @property string $error_message
|
||||
* @property int $error_code
|
||||
* @property string $error_trace
|
||||
* @property array $original_message
|
||||
* @property int $retry_count
|
||||
* @property string|null $message_id
|
||||
* @property \Carbon\Carbon $failed_at
|
||||
* @property \Carbon\Carbon $created_at
|
||||
*/
|
||||
class FailedMessage extends Model
|
||||
{
|
||||
protected ?string $table = 'failed_messages';
|
||||
|
||||
public bool $timestamps = false;
|
||||
|
||||
protected array $fillable = [
|
||||
'error_id', 'data_type', 'platform', 'platform_id',
|
||||
'company_id', 'store_id', 'error_type', 'error_message',
|
||||
'error_code', 'error_trace', 'original_message',
|
||||
'retry_count', 'message_id', 'failed_at', 'created_at',
|
||||
];
|
||||
|
||||
protected array $casts = [
|
||||
'id' => 'integer',
|
||||
'platform_id' => 'integer',
|
||||
'company_id' => 'integer',
|
||||
'store_id' => 'integer',
|
||||
'error_code' => 'integer',
|
||||
'original_message' => 'json',
|
||||
'retry_count' => 'integer',
|
||||
'failed_at' => 'datetime',
|
||||
'created_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
@@ -78,6 +78,49 @@ use OpenApi\Attributes as OA;
|
||||
new OA\Property(property: 'fetched_at', type: 'string', format: 'date-time', example: '2026-03-13 12:00:00', description: '数据获取时间'),
|
||||
]
|
||||
)]
|
||||
#[OA\Schema(
|
||||
schema: 'FailedMessageList',
|
||||
type: 'object',
|
||||
description: '失败消息列表项',
|
||||
properties: [
|
||||
new OA\Property(property: 'id', type: 'integer', example: 1),
|
||||
new OA\Property(property: 'error_id', type: 'string', example: 'err_67890abc'),
|
||||
new OA\Property(property: 'data_type', type: 'string', enum: ['order', 'product', 'refund'], example: 'order'),
|
||||
new OA\Property(property: 'platform', type: 'string', example: 'Tmall', nullable: true),
|
||||
new OA\Property(property: 'platform_id', type: 'integer', example: 1, nullable: true),
|
||||
new OA\Property(property: 'company_id', type: 'integer', example: 1, nullable: true),
|
||||
new OA\Property(property: 'store_id', type: 'integer', example: 1, nullable: true),
|
||||
new OA\Property(property: 'error_type', type: 'string', example: 'RuntimeException'),
|
||||
new OA\Property(property: 'error_message', type: 'string', example: 'Connection refused'),
|
||||
new OA\Property(property: 'retry_count', type: 'integer', example: 3),
|
||||
new OA\Property(property: 'message_id', type: 'string', example: 'msg_12345', nullable: true),
|
||||
new OA\Property(property: 'failed_at', type: 'string', format: 'date-time'),
|
||||
new OA\Property(property: 'created_at', type: 'string', format: 'date-time'),
|
||||
]
|
||||
)]
|
||||
#[OA\Schema(
|
||||
schema: 'FailedMessageDetail',
|
||||
type: 'object',
|
||||
description: '失败消息详情(含完整错误堆栈和原始消息体)',
|
||||
properties: [
|
||||
new OA\Property(property: 'id', type: 'integer', example: 1),
|
||||
new OA\Property(property: 'error_id', type: 'string', example: 'err_67890abc'),
|
||||
new OA\Property(property: 'data_type', type: 'string', enum: ['order', 'product', 'refund'], example: 'order'),
|
||||
new OA\Property(property: 'platform', type: 'string', example: 'Tmall', nullable: true),
|
||||
new OA\Property(property: 'platform_id', type: 'integer', example: 1, nullable: true),
|
||||
new OA\Property(property: 'company_id', type: 'integer', example: 1, nullable: true),
|
||||
new OA\Property(property: 'store_id', type: 'integer', example: 1, nullable: true),
|
||||
new OA\Property(property: 'error_type', type: 'string', example: 'RuntimeException'),
|
||||
new OA\Property(property: 'error_message', type: 'string', example: 'Connection refused'),
|
||||
new OA\Property(property: 'error_code', type: 'integer', example: 0),
|
||||
new OA\Property(property: 'error_trace', type: 'string', description: '完整异常堆栈'),
|
||||
new OA\Property(property: 'original_message', type: 'object', description: '原始消息体 JSON'),
|
||||
new OA\Property(property: 'retry_count', type: 'integer', example: 3),
|
||||
new OA\Property(property: 'message_id', type: 'string', example: 'msg_12345', nullable: true),
|
||||
new OA\Property(property: 'failed_at', type: 'string', format: 'date-time'),
|
||||
new OA\Property(property: 'created_at', type: 'string', format: 'date-time'),
|
||||
]
|
||||
)]
|
||||
class OpenApiSpec
|
||||
{
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace App\Platform;
|
||||
|
||||
use App\Entity\Parse\EntityParseFactory;
|
||||
use App\Model\FailedMessage;
|
||||
use App\Utils\Log;
|
||||
use Hyperf\Amqp\Annotation\Consumer;
|
||||
use Hyperf\Amqp\Builder\QueueBuilder;
|
||||
@@ -265,6 +266,9 @@ class OrderConsumer extends ConsumerMessage
|
||||
$error_producer = new ErrorProducer($message, $error, $retry_count);
|
||||
$producer->produce($error_producer);
|
||||
|
||||
// 同步写入 failed_messages 表
|
||||
$this->persistFailedMessage($error_producer->payload);
|
||||
|
||||
// 记录日志
|
||||
Log::get()->warning('Message sent to error queue after exceeding retry limit', [
|
||||
'error_id' => $error_producer->payload['error_id'] ?? 'unknown',
|
||||
@@ -453,4 +457,34 @@ class OrderConsumer extends ConsumerMessage
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 持久化失败消息到数据库
|
||||
*/
|
||||
protected function persistFailedMessage(array $payload): void
|
||||
{
|
||||
try {
|
||||
FailedMessage::query()->create([
|
||||
'error_id' => $payload['error_id'],
|
||||
'data_type' => $payload['metadata']['data_type'] ?? 'order',
|
||||
'platform' => $payload['metadata']['platform'] ?? null,
|
||||
'platform_id' => $payload['metadata']['platform_id'] ?? null,
|
||||
'company_id' => $payload['metadata']['company_id'] ?? null,
|
||||
'store_id' => $payload['metadata']['store_id'] ?? null,
|
||||
'error_type' => $payload['error']['type'] ?? 'Unknown',
|
||||
'error_message' => $payload['error']['message'] ?? '',
|
||||
'error_code' => $payload['error']['code'] ?? 0,
|
||||
'error_trace' => $payload['error']['trace'] ?? '',
|
||||
'original_message' => $payload['original_message'] ?? [],
|
||||
'retry_count' => $payload['metadata']['retry_count'] ?? 0,
|
||||
'message_id' => $payload['metadata']['message_id'] ?? null,
|
||||
'failed_at' => $payload['metadata']['failed_at'] ?? date('c'),
|
||||
]);
|
||||
} catch (Throwable $e) {
|
||||
Log::get()->error('Failed to persist failed message to database', [
|
||||
'error' => $e->getMessage(),
|
||||
'error_id' => $payload['error_id'] ?? 'unknown',
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace App\Platform;
|
||||
|
||||
use App\Entity\Parse\EntityParseFactory;
|
||||
use App\Model\FailedMessage;
|
||||
use App\Utils\Log;
|
||||
use Hyperf\Amqp\Annotation\Consumer;
|
||||
use Hyperf\Amqp\Builder\QueueBuilder;
|
||||
@@ -191,6 +192,9 @@ class ProductConsumer extends ConsumerMessage
|
||||
$error_producer = new ErrorProducer($message, $error, $retry_count);
|
||||
$producer->produce($error_producer);
|
||||
|
||||
// 同步写入 failed_messages 表
|
||||
$this->persistFailedMessage($error_producer->payload);
|
||||
|
||||
Log::get()->warning('Product message sent to error queue after exceeding retry limit', [
|
||||
'error_id' => $error_producer->payload['error_id'] ?? 'unknown',
|
||||
'retry_count' => $retry_count,
|
||||
@@ -204,4 +208,34 @@ class ProductConsumer extends ConsumerMessage
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 持久化失败消息到数据库
|
||||
*/
|
||||
protected function persistFailedMessage(array $payload): void
|
||||
{
|
||||
try {
|
||||
FailedMessage::query()->create([
|
||||
'error_id' => $payload['error_id'],
|
||||
'data_type' => $payload['metadata']['data_type'] ?? 'product',
|
||||
'platform' => $payload['metadata']['platform'] ?? null,
|
||||
'platform_id' => $payload['metadata']['platform_id'] ?? null,
|
||||
'company_id' => $payload['metadata']['company_id'] ?? null,
|
||||
'store_id' => $payload['metadata']['store_id'] ?? null,
|
||||
'error_type' => $payload['error']['type'] ?? 'Unknown',
|
||||
'error_message' => $payload['error']['message'] ?? '',
|
||||
'error_code' => $payload['error']['code'] ?? 0,
|
||||
'error_trace' => $payload['error']['trace'] ?? '',
|
||||
'original_message' => $payload['original_message'] ?? [],
|
||||
'retry_count' => $payload['metadata']['retry_count'] ?? 0,
|
||||
'message_id' => $payload['metadata']['message_id'] ?? null,
|
||||
'failed_at' => $payload['metadata']['failed_at'] ?? date('c'),
|
||||
]);
|
||||
} catch (Throwable $e) {
|
||||
Log::get()->error('Failed to persist failed message to database', [
|
||||
'error' => $e->getMessage(),
|
||||
'error_id' => $payload['error_id'] ?? 'unknown',
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ declare(strict_types=1);
|
||||
namespace App\Platform;
|
||||
|
||||
use App\Entity\Parse\EntityParseFactory;
|
||||
use App\Model\FailedMessage;
|
||||
use App\Model\RefundItem;
|
||||
use App\Utils\Log;
|
||||
use Hyperf\Amqp\Annotation\Consumer;
|
||||
use Hyperf\Amqp\Builder\QueueBuilder;
|
||||
@@ -13,7 +15,6 @@ use Hyperf\Amqp\Result;
|
||||
use Hyperf\Amqp\Producer;
|
||||
use PhpAmqpLib\Message\AMQPMessage;
|
||||
use Hyperf\DbConnection\Db;
|
||||
use App\Model\RefundItem;
|
||||
use Hyperf\Context\ApplicationContext;
|
||||
use Throwable;
|
||||
|
||||
@@ -233,6 +234,9 @@ class RefundConsumer extends ConsumerMessage
|
||||
$error_producer = new ErrorProducer($message, $error, $retry_count);
|
||||
$producer->produce($error_producer);
|
||||
|
||||
// 同步写入 failed_messages 表
|
||||
$this->persistFailedMessage($error_producer->payload);
|
||||
|
||||
Log::get()->warning('Refund message sent to error queue after exceeding retry limit', [
|
||||
'error_id' => $error_producer->payload['error_id'] ?? 'unknown',
|
||||
'retry_count' => $retry_count,
|
||||
@@ -247,6 +251,36 @@ class RefundConsumer extends ConsumerMessage
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 持久化失败消息到数据库
|
||||
*/
|
||||
protected function persistFailedMessage(array $payload): void
|
||||
{
|
||||
try {
|
||||
FailedMessage::query()->create([
|
||||
'error_id' => $payload['error_id'],
|
||||
'data_type' => $payload['metadata']['data_type'] ?? 'refund',
|
||||
'platform' => $payload['metadata']['platform'] ?? null,
|
||||
'platform_id' => $payload['metadata']['platform_id'] ?? null,
|
||||
'company_id' => $payload['metadata']['company_id'] ?? null,
|
||||
'store_id' => $payload['metadata']['store_id'] ?? null,
|
||||
'error_type' => $payload['error']['type'] ?? 'Unknown',
|
||||
'error_message' => $payload['error']['message'] ?? '',
|
||||
'error_code' => $payload['error']['code'] ?? 0,
|
||||
'error_trace' => $payload['error']['trace'] ?? '',
|
||||
'original_message' => $payload['original_message'] ?? [],
|
||||
'retry_count' => $payload['metadata']['retry_count'] ?? 0,
|
||||
'message_id' => $payload['metadata']['message_id'] ?? null,
|
||||
'failed_at' => $payload['metadata']['failed_at'] ?? date('c'),
|
||||
]);
|
||||
} catch (Throwable $e) {
|
||||
Log::get()->error('Failed to persist failed message to database', [
|
||||
'error' => $e->getMessage(),
|
||||
'error_id' => $payload['error_id'] ?? 'unknown',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理退款子项的批量同步
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user