aligent/async-events

一个基于事件驱动的灵活的异步事件模块,允许您异步处理任何事件。

安装次数: 6,657

依赖项: 1

建议者: 0

安全: 0

星标: 19

关注者: 7

分支: 4

开放问题: 1

类型:magento2-module

v3.1.6 2024-06-25 00:24 UTC

README

Integration Test REST

用于可靠处理异步事件的 Magento 框架。

  • 异步:该模块使用 RabbitMQ(或 DB 队列)以利用异步消息传递。
  • 灵活:解耦事件和派发,提供更灵活的消息建模。
  • 可扩展:自动处理背压并提供异步故障转移模型。

支持

安装

composer require aligent/async-events

使用

定义异步事件

在模块的 etc/ 目录下创建一个新的 async_events.xml

<?xml version="1.0"?>
<config
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:module:Aligent_AsyncEvents:etc/async_events.xsd"
>
    <async_event name="sales.order.created">
        <service class="Magento\Sales\Api\OrderRepositoryInterface" method="get"/>
    </async_event>
</config>

创建订阅

HTTP 订阅

curl --location --request POST 'https://m2.dev.aligent.consulting:44356/rest/V1/async_event' \
--header 'Authorization: Bearer TOKEN' \
--header 'Content-Type: application/json' \
--data-raw '{
    "asyncEvent": {
        "event_name": "sales.order.created",
        "recipient_url": "https://example.com/order_created",
        "verification_token": "fD03@NpYbXYg",
        "metadata": "http"
    }
}'

Amazon EventBridge 订阅

需要 EventBridge Notifier

curl --location --request POST 'https://m2.dev.aligent.consulting:44356/rest/V1/async_event' \
--header 'Authorization: Bearer TOKEN' \
--header 'Content-Type: application/json' \
--data-raw '{
    "asyncEvent": {
        "event_name": "sales.order.created",
        "recipient_url": "arn:aws:events:ap-southeast-2:005158166381:rule/Test.EventBridge.Rule",
        "verification_token": "aIW0G9n3*9wN",
        "metadata": "event_bridge"
    }
}'

派发异步事件

public function execute(Observer $observer): void
{
    /** @var Order $object */
    $object = $observer->getEvent()->getData('order');

    // arguments are the inputs required by the service class in the asynchronous
    // event definition in async_events.xml
    // e.g: Magento\Sales\Api\OrderRepositoryInterface::get
    $arguments = ['id' => $object->getId()];
    $data = ['sales.order.created', $this->json->serialize($arguments)];

    $this->publisher->publish(
        QueueMetadataInterface::EVENT_QUEUE,
        $data
    );
}

确保以下消费者正在运行

bin/magento queue:consumer:start event.trigger.consumer
bin/magento queue:consumer:start event.retry.consumer

高级使用

请参阅Wiki

功能

跟踪

所有事件都使用 UUID 在单独的订阅级别进行记录。

从第一次投递尝试到最新尝试的所有信息都作为跟踪表呈现。事件有效负载也可用于调查目的。

Event Trace Page

重试

事件会自动使用指数退避进行重试。默认重试限制为 5。最大退避时间为 60 秒。

指数退避的计算方式为 min(60, pow($deathCount, 2));

要更改默认重试限制,请访问 Admin > Stores > Settings > Configuration > Advanced > System > Async Events 并更新 Maximum Deaths

Retry Limit Config Page

重放

事件可以独立于其状态进行重放。这在耗尽所有重试后用于调试或重放事件时很有用。

重放启动新的投递尝试链,如果失败则将遵循相同的重试机制。

Lucene 查询语法

默认情况下,所有事件都由 Elasticsearch 索引。这允许您搜索事件,包括事件有效负载!

该模块支持Lucene 查询语法以查询事件数据,如属性。

以下属性适用于所有异步事件。

log_id
uuid
event_name
success
created

以下属性在不同类型的异步事件之间存在差异。

data

示例

假设您已配置以下事件

customer.created
customer.updated
customer.deleted
sales.order.created
sales.invoice.created
shipment.created
shipment.updated
shipment.deleted

您可以使用通配符如 event_name: customer.* 来查询所有客户事件,这将匹配以下事件

customer.created
customer.updated
customer.deleted

您可以使用 *.created 来查询所有创建的事件,这将匹配以下事件

customer.created
sales.order.created
sales.invoice.created
shipment.created

您可以使用其他可用的属性(如状态或 uuid)进一步缩小搜索范围。

以下查询返回所有失败的客户事件:customer.* AND success: false

您可以将复杂的 Lucene 查询组合起来以获取事件历史记录,然后通过管理员网格以 CSV 格式导出。

在事件有效负载中搜索

搜索事件有效负载取决于您搜索的事件。

以下示例事件负载中,四个属性被索引为属性。因此,您可以在 data.customer_emaildata.customer_firstnamedata.customer_lastnamedata.increment_id 上进行查询。任何级别的数组内的属性不可搜索。

{
    "data": {
        "customer_email": "roni_cost@example.com",
        "customer_firstname": "Veronica",
        "customer_lastname": "Costello",
        "increment_id": "CK00000001",
        "payment_additional_info": [
            {
                "key": "method_title",
                "value": "Check / Money order"
            }
        ]
    }
}

搜索所有客户邮箱为 roni_cost@example.com 的事件

data.data.customer_email: roni_cost@example.com

Lucene Example 1

搜索所有订单增量ID以“CK”开头且状态为成功的事件

data.data.increment_id: CK* AND success: true

Lucene Example 2

要关闭异步事件索引,请访问管理 > 商店 > 设置 > 配置 > 高级 > 系统 > 异步事件,并禁用 启用异步事件索引