cancel
Showing results for 
Search instead for 
Did you mean: 

Avoiding the cyclic event loop in Magento 2

SOLVED

Avoiding the cyclic event loop in Magento 2

Hi,

 

the documentation best practice talks about avoiding the cyclic event loop.

https://devdocs.magento.com/guides/v2.3/ext-best-practices/extension-coding/observers-bp.html

 

Can someone please help how to solve the below problem:

I am synchronising customer information in Magento (M2) with an external system (let's say X). The flows that should be in place are:

 

M2 => X  (by implementing the event observers on save and delete on customer, and, then call the rest server on X to send the data)

X => M2 (system X calls existing REST API for Magento)

 

How do I solution so that the save event is not triggered back to X for an update coming from X (via the rest API)?

 

Example scenario is:

In M2 - I have 2 observers that will listen to customer 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Avoiding the cyclic event loop in Magento 2

If the same event is used for both directions of data flow, in that case in your plugin or observer you can have small logic to check controller, action, router etc. 

 

For outgoing data flow, you can have some identifier or special parameter in the payload which distinguishes incoming and outgoing requests. 

 

class Index 
{
    public function __construct(        
        \Magento\Framework\App\Action\Context $context
    ){
 
    }

    public function execute()
    {        
        $moduleName = $this->request->getModuleName();        
        $controller = $this->request->getControllerName();        
        $action     = $this->request->getActionName();        
        $route      = $this->request->getRouteName();        
        echo $moduleName."<br/>";        
        echo $controller."<br/>";        
        echo $action."<br/>";  
        echo $route."<br/>";
    }
} 
Suman Kar(suman.jis@gmail.com) Magento Certified Developer Plus Skype: sumanphptech Problem solved? Please give 'Kudos' and accept 'Answer as Solution'.

View solution in original post

1 REPLY 1

Re: Avoiding the cyclic event loop in Magento 2

If the same event is used for both directions of data flow, in that case in your plugin or observer you can have small logic to check controller, action, router etc. 

 

For outgoing data flow, you can have some identifier or special parameter in the payload which distinguishes incoming and outgoing requests. 

 

class Index 
{
    public function __construct(        
        \Magento\Framework\App\Action\Context $context
    ){
 
    }

    public function execute()
    {        
        $moduleName = $this->request->getModuleName();        
        $controller = $this->request->getControllerName();        
        $action     = $this->request->getActionName();        
        $route      = $this->request->getRouteName();        
        echo $moduleName."<br/>";        
        echo $controller."<br/>";        
        echo $action."<br/>";  
        echo $route."<br/>";
    }
} 
Suman Kar(suman.jis@gmail.com) Magento Certified Developer Plus Skype: sumanphptech Problem solved? Please give 'Kudos' and accept 'Answer as Solution'.