|
1 | 1 | # Magento Asynchronous Events
|
2 | 2 |
|
3 |
| -A framework for handling reliable and asynchronous events with Magento. |
| 3 | +[](https://github.com/aligent/magento-async-events/actions/workflows/integration-tests.yml) |
| 4 | + |
| 5 | +A framework for reliably handling asynchronous events with Magento. |
4 | 6 |
|
5 | 7 | * **Asynchronous**: The module uses RabbitMQ (or DB queues) to leverage asynchronous message delivery.
|
6 | 8 | * **Flexible**: Decoupling events and dispatches provide greater flexibility in message modelling.
|
7 | 9 | * **Scalable**: Handles back pressure and provides an asynchronous failover model automatically.
|
8 | 10 |
|
| 11 | +## Installation |
| 12 | + |
| 13 | +``` |
| 14 | +composer require aligent/async-events |
| 15 | +``` |
| 16 | + |
| 17 | +## Getting Started |
| 18 | + |
| 19 | +### Define an asynchronous event |
| 20 | + |
| 21 | +Create a new `async_events.xml` under a module's `etc/` directory. |
| 22 | + |
| 23 | +```xml |
| 24 | +<?xml version="1.0"?> |
| 25 | +<config |
| 26 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 27 | + xsi:noNamespaceSchemaLocation="urn:magento:module:Aligent_AsyncEvents:etc/async_events.xsd" |
| 28 | +> |
| 29 | + <async_event name="sales.order.created"> |
| 30 | + <service class="Magento\Sales\Api\OrderRepositoryInterface" method="get"/> |
| 31 | + </async_event> |
| 32 | +</config> |
| 33 | +``` |
| 34 | + |
| 35 | +### Create Subscription |
| 36 | + |
| 37 | +#### HTTP Subscription |
| 38 | +```shell |
| 39 | +curl --location --request POST 'https://m2.dev.aligent.consulting:44356/rest/V1/async_event' \ |
| 40 | +--header 'Authorization: Bearer TOKEN' \ |
| 41 | +--header 'Content-Type: application/json' \ |
| 42 | +--data-raw '{ |
| 43 | + "asyncEvent": { |
| 44 | + "event_name": "sales.order.created", |
| 45 | + "recipient_url": "https://example.com/order_created", |
| 46 | + "verification_token": "fD03@NpYbXYg", |
| 47 | + "metadata": "http" |
| 48 | + } |
| 49 | +}' |
| 50 | +``` |
| 51 | + |
| 52 | +#### Amazon EventBridge Subscription |
| 53 | +Requires the [EventBridge Notifier](https://github.com/aligent/magento2-eventbridge-notifier) |
| 54 | + |
| 55 | +```shell |
| 56 | +curl --location --request POST 'https://m2.dev.aligent.consulting:44356/rest/V1/async_event' \ |
| 57 | +--header 'Authorization: Bearer TOKEN' \ |
| 58 | +--header 'Content-Type: application/json' \ |
| 59 | +--data-raw '{ |
| 60 | + "asyncEvent": { |
| 61 | + "event_name": "sales.order.created", |
| 62 | + "recipient_url": "arn:aws:events:ap-southeast-2:005158166381:rule/Test.EventBridge.Rule", |
| 63 | + "verification_token": "aIW0G9n3*9wN", |
| 64 | + "metadata": "event_bridge" |
| 65 | + } |
| 66 | +}' |
| 67 | +``` |
| 68 | + |
| 69 | +### Dispatch an asynchronous event |
| 70 | +```php |
| 71 | +public function execute(Observer $observer): void |
| 72 | +{ |
| 73 | + /** @var Order $object */ |
| 74 | + $object = $observer->getEvent()->getData('order'); |
| 75 | + |
| 76 | + // arguments are the inputs required by the service class in the asynchronous |
| 77 | + // event definition in async_events.xml |
| 78 | + // e.g: Magento\Sales\Api\OrderRepositoryInterface::get |
| 79 | + $arguments = ['id' => $object->getId()]; |
| 80 | + $data = ['sales.order.created', $this->json->serialize($arguments)]; |
| 81 | + |
| 82 | + $this->publisher->publish( |
| 83 | + QueueMetadataInterface::EVENT_QUEUE, |
| 84 | + $data |
| 85 | + ); |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +Ensure the following consumers are running |
| 90 | + |
| 91 | +```shell |
| 92 | +bin/magento queue:consumer:start event.trigger.consumer |
| 93 | +bin/magento queue:consumer:start event.retry.consumer |
| 94 | +``` |
| 95 | + |
| 96 | +## Advanced Usage |
| 97 | +Refer to the [Wiki](https://github.com/aligent/magento-async-events/wiki) |
| 98 | + |
9 | 99 | ## Trace
|
10 | 100 |
|
11 | 101 | All events are logged at the individual subscription level with a UUID.
|
@@ -135,82 +225,3 @@ Search all events with the order increment id starting with `CK` and status succ
|
135 | 225 |
|
136 | 226 | To turn off asynchronous event indexing visit Admin > Stores > Settings > Configuration > Advanced > System >
|
137 | 227 | Async Events and disable `Enable Asynchronous Events Indexing`.
|
138 |
| - |
139 |
| -## Getting Started |
140 |
| - |
141 |
| -### Define an asynchronous event |
142 |
| -Create a new file under `etc/async_events.xml` |
143 |
| -```xml |
144 |
| -<?xml version="1.0"?> |
145 |
| -<config |
146 |
| - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
147 |
| - xsi:noNamespaceSchemaLocation="urn:magento:module:Aligent_AsyncEvents:etc/async_events.xsd" |
148 |
| -> |
149 |
| - <async_event name="sales.order.created"> |
150 |
| - <service class="Magento\Sales\Api\OrderRepositoryInterface" method="get"/> |
151 |
| - </async_event> |
152 |
| -</config> |
153 |
| -``` |
154 |
| - |
155 |
| -### Create Subscription |
156 |
| - |
157 |
| -#### HTTP Subscription |
158 |
| -```shell |
159 |
| -curl --location --request POST 'https://m2.dev.aligent.consulting:44356/rest/V1/async_event' \ |
160 |
| ---header 'Authorization: Bearer TOKEN' \ |
161 |
| ---header 'Content-Type: application/json' \ |
162 |
| ---data-raw '{ |
163 |
| - "asyncEvent": { |
164 |
| - "event_name": "sales.order.created", |
165 |
| - "recipient_url": "https://example.com/order_created", |
166 |
| - "verification_token": "fD03@NpYbXYg", |
167 |
| - "metadata": "http" |
168 |
| - } |
169 |
| -}' |
170 |
| -``` |
171 |
| - |
172 |
| -#### Amazon EventBridge Subscription |
173 |
| -Requires the [EventBridge Notifier](https://github.com/aligent/magento2-eventbridge-notifier) |
174 |
| - |
175 |
| -```shell |
176 |
| -curl --location --request POST 'https://m2.dev.aligent.consulting:44356/rest/V1/async_event' \ |
177 |
| ---header 'Authorization: Bearer TOKEN' \ |
178 |
| ---header 'Content-Type: application/json' \ |
179 |
| ---data-raw '{ |
180 |
| - "asyncEvent": { |
181 |
| - "event_name": "sales.order.created", |
182 |
| - "recipient_url": "arn:aws:events:ap-southeast-2:005158166381:rule/Test.EventBridge.Rule", |
183 |
| - "verification_token": "aIW0G9n3*9wN", |
184 |
| - "metadata": "event_bridge" |
185 |
| - } |
186 |
| -}' |
187 |
| -``` |
188 |
| - |
189 |
| -### Dispatch an asynchronous event |
190 |
| -```php |
191 |
| -public function execute(Observer $observer): void |
192 |
| -{ |
193 |
| - /** @var Order $object */ |
194 |
| - $object = $observer->getEvent()->getData('order'); |
195 |
| - |
196 |
| - // arguments are the inputs required by the service class in the asynchronous |
197 |
| - // event definition in async_events.xml |
198 |
| - // e.g: Magento\Sales\Api\OrderRepositoryInterface::get |
199 |
| - $arguments = ['id' => $object->getId()]; |
200 |
| - $data = ['sales.order.created', $this->json->serialize($arguments)]; |
201 |
| - |
202 |
| - $this->publisher->publish( |
203 |
| - QueueMetadataInterface::EVENT_QUEUE, |
204 |
| - $data |
205 |
| - ); |
206 |
| -} |
207 |
| -``` |
208 |
| -Ensure the following consumers are running |
209 |
| - |
210 |
| -```shell |
211 |
| -bin/magento queue:consumer:start event.trigger.consumer |
212 |
| -bin/magento queue:consumer:start event.retry.consumer |
213 |
| -``` |
214 |
| - |
215 |
| -## Advanced Usage |
216 |
| -Refer to the [Wiki](https://github.com/aligent/magento-async-events/wiki) |
|
0 commit comments