Situation:
I’m creating a Magento 2 module that listens to specific event observers, gets the ID’s associated with those events, and sends them to a third-party Node app. This Node app will receive those ID’s, and then use the Magento Rest API to get the data related to those ID’s. For instance, I’ll listen to the sales_order_save_after
event, POST the order ID to the third-party Node app, and then make an authenticated call to the Magento Rest API to GET that order data. The order data is then transformed and stored for use elsewhere.
Problem:
The observers I’m listening to are:
customer_address_save_after
admin_sales_order_address_update
customer_save_after_data_object
sales_order_save_after
The first 3 observers work perfectly, and the 4th (sales_order_save_after
) fires, but it hangs on waiting for the third-party Node app to finish. The Node app takes a while though because, despite having the correct order ID, when it uses the Magento Rest API to fetch the order, it returns an error: The entity that was requested doesn't exist. Verify the entity and try again.
While the module is waiting for this Node app call to resolve, it shows the spinning loader on the checkout page. My thinking is that while the order is indeed created (otherwise, how would we have the correct order ID?) in the database, it’s not accessible via the Rest API yet. If I replay the payload once I’m on the order confirmation page, the Rest API returns the correct order as expected.
So it’s able to get the ID from the observer, it’s able to send that ID to the Node app, but it hangs while the Rest API tries multiple times to get that order and fails to do so. Ultimately the Node app gives up and resolves the request, rendering the order confirmation page, at which point the event can be retried, and the Node app will successfully get the order from the Rest API.
What I’ve tried to fix this:
- I’ve tried having the Node app sleep for 5-10s before making the Rest API call, but it returns the same error.
- I’ve tried using another observer,
checkout_submit_all_after
, which works perfectly. But this observer doesn’t respond to changes in the order after it’s created (invoices, shipments, status changes, credit memos, etc). - I’ve tried using exponential backoff on the Rest API call and this returns the same error every time, while Magento shows the loading spinner on the checkout page. Once exponential backoff ultimately fails for the last time, it resolves the network call back to the Magento module and shows the order confirmation page.
- I’ve tried switching to using sockets so that I can “fire and forget”. This would allow the order to resolve immediately, but implementing sockets on my Node app would be a lot of work and I feel like there’s an easier solution within Magento that I’m not seeing yet.
Observer code:
events.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="customer_address_save_after">
<observer name="myorg_customer_address_save_after" instance="MyOrgMyModuleObserverAddress" />
</event>
<event name="admin_sales_order_address_update">
<observer name="myorg_admin_sales_order_address_update" instance="MyOrgMyModuleObserverOrderAddress" />
</event>
<event name="customer_save_after_data_object">
<observer name="myorg_customer_save_after_data_object" instance="MyOrgMyModuleObserverCustomer" />
</event>
<event name="sales_order_save_after">
<observer name="myorg_sales_order_save_after" instance="MyOrgMyModuleObserverOrder" />
</event>
</config>
Order.php
<?php
namespace MyOrgMyModuleObserver;
use MagentoFrameworkEventObserver;
use MagentoFrameworkEventObserverInterface;
use MyOrgMyModuleHelperData;
class Order implements ObserverInterface
{
/**
* Constructor
* @param Data $helper
*/
public function __construct(Data $helper)
{
$this->helper = $helper;
}
public function execute(Observer $observer)
{
// Get the data
$order = $observer->getOrder();
// Create the payload data
$data = [
'orders' => [$order->getId()],
];
// Create the payload event
$event = [
'name' => $observer->getEvent()->getName(),
'type' => 'order',
];
// Create the payload
$payload = [
'data' => $data,
'event' => $event,
];
// Send the data to the Node app
$this->helper->send($payload);
}
}
I don’t want to post the send()
method in the Data.php
helper file because it’s proprietary. But just know it basically sends a cURL request – nothing fancy – and it’s been fully tested with other observers and works perfectly. This isn’t the problem.
Questions:
I see two ways this can be solved by answering either:
- Why is the
sales_order_save_after
observer blocked by network requests when other observers don’t have this behavior? Is there a way to “save” the order, allowing it to continue to the confirmation page before firing the request to the Node app? - Why is the REST API for Magento unable to find an order that it has the ID of? Why is it able to find addresses and customers using other observers that are virtually identical, but orders seem to not be available yet?
If I’m getting the correct order ID in the observer, then that order must be stored in the DB. But if it’s stored in the DB, why can’t the Rest API get it?
Any help would be greatly appreciated – thanks!