From 85ead80fe7f3d501434fff4f0724033e3af6f668 Mon Sep 17 00:00:00 2001 From: bnayal Date: Mon, 5 Feb 2018 12:04:33 +0200 Subject: [PATCH] Better handling batch updates from the admin --- Helper/ConfigHelper.php | 9 +++++++++ Model/Api/Data.php | 2 +- Observer/EventMethods.php | 24 +++++++++++++++++++++--- composer.json | 2 +- etc/module.xml | 2 +- 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/Helper/ConfigHelper.php b/Helper/ConfigHelper.php index 3a13aec..60f2470 100644 --- a/Helper/ConfigHelper.php +++ b/Helper/ConfigHelper.php @@ -18,6 +18,7 @@ class ConfigHelper const RM_STORE_ID = 'remarkety/mgconnector/public_storeId'; const WEBHOOKS_ENABLED = 'remarkety/mgconnector/webhooks'; const PRODUCT_WEBHOOKS_DISABLED = 'remarkety/mgconnector/product_webhooks_disable'; + const FORCE_ASYNC_WEBHOOKS = 'remarkety/mgconnector/forceasyncwebhooks'; protected $_activeStore; protected $_scopeConfig; @@ -65,6 +66,14 @@ public function shouldSendProductUpdates(){ return false; } + public function forceAsyncWebhooks(){ + $async = $this->_scopeConfig->getValue(self::FORCE_ASYNC_WEBHOOKS); + if(!empty($async)){ + return true; + } + return false; + } + /** * @param bool $enabled */ diff --git a/Model/Api/Data.php b/Model/Api/Data.php index 84d794d..780509b 100644 --- a/Model/Api/Data.php +++ b/Model/Api/Data.php @@ -1206,7 +1206,7 @@ public function setConfig($mage_store_id, $configName, $scope, $newValue) */ public function getVersion() { - return '2.2.12'; + return '2.2.13'; } /** diff --git a/Observer/EventMethods.php b/Observer/EventMethods.php index 267e93f..e9c9489 100644 --- a/Observer/EventMethods.php +++ b/Observer/EventMethods.php @@ -69,6 +69,10 @@ class EventMethods { */ protected $logger; + private $_countEvents = 0; + + private $_forceAsyncWebhooks = false; + public function __construct( LoggerInterface $logger, Registry $coreRegistry, @@ -115,6 +119,7 @@ public function __construct( } else { $this->_intervals = explode(',', $intervals); } + $this->_forceAsyncWebhooks = $configHelper->forceAsyncWebhooks(); } protected function isWebhooksEnabled($store){ @@ -203,6 +208,14 @@ public function makeRequest($eventType, $payload, $storeId = null, $attempt = 0, } $client = new \Zend_Http_Client($url, $this->_getRequestConfig($eventType)); $payload = array_merge($payload, $this->_getPayloadBase($eventType)); + + if(empty($queueId) && ($this->_forceAsyncWebhooks || $this->_countEvents >= 3)){ + //batch update, push to queue + $this->_queueRequest($eventType, $payload, 0, null, $storeId); + return true; + } + $this->_countEvents++; + $json = json_encode($payload); $response = $client @@ -233,9 +246,13 @@ protected function _queueRequest($eventType, $payload, $attempt, $queueId, $stor { $queueModel = null; - if(!empty($this->_intervals[$attempt-1])) { + if($attempt == 0 || !empty($this->_intervals[$attempt-1])) { $now = time(); - $nextAttempt = $now + (int)$this->_intervals[$attempt-1] * 60; + if($attempt == 0){ + $nextAttempt = $now; + } else { + $nextAttempt = $now + (int)$this->_intervals[$attempt-1] * 60; + } if($queueId) { $queueModel = $this->_remarketyQueueRepo->getById($queueId); $queueModel->setAttempts($attempt); @@ -282,7 +299,7 @@ protected function _getPayloadBase($eventType) } - protected function _prepareCustomerSubscribtionUpdateData(Subscriber $subscriber, $clientIp = null, $customerId = null) + protected function _prepareCustomerSubscribtionUpdateData(Subscriber $subscriber, $clientIp = null) { $arr = array( 'email' => $subscriber->getSubscriberEmail(), @@ -294,6 +311,7 @@ protected function _prepareCustomerSubscribtionUpdateData(Subscriber $subscriber $arr['client_ip'] = $clientIp; } + $customerId = $subscriber->getCustomerId(); if(!empty($customerId)){ $arr['customerId'] = $customerId; } diff --git a/composer.json b/composer.json index 337bacf..9f5431a 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "lib-libxml": "*" }, "type": "magento2-module", - "version": "2.2.12", + "version": "2.2.13", "license": [ "OSL-3.0", "AFL-3.0" diff --git a/etc/module.xml b/etc/module.xml index 295fa4d..948d613 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,5 +1,5 @@ - +