Skip to content

Commit

Permalink
Better handling batch updates from the admin
Browse files Browse the repository at this point in the history
  • Loading branch information
bnayalivne committed Feb 5, 2018
1 parent fbcf45f commit 85ead80
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
9 changes: 9 additions & 0 deletions Helper/ConfigHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion Model/Api/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,7 @@ public function setConfig($mage_store_id, $configName, $scope, $newValue)
*/
public function getVersion()
{
return '2.2.12';
return '2.2.13';
}

/**
Expand Down
24 changes: 21 additions & 3 deletions Observer/EventMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ class EventMethods {
*/
protected $logger;

private $_countEvents = 0;

private $_forceAsyncWebhooks = false;

public function __construct(
LoggerInterface $logger,
Registry $coreRegistry,
Expand Down Expand Up @@ -115,6 +119,7 @@ public function __construct(
} else {
$this->_intervals = explode(',', $intervals);
}
$this->_forceAsyncWebhooks = $configHelper->forceAsyncWebhooks();
}

protected function isWebhooksEnabled($store){
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(),
Expand All @@ -294,6 +311,7 @@ protected function _prepareCustomerSubscribtionUpdateData(Subscriber $subscriber
$arr['client_ip'] = $clientIp;
}

$customerId = $subscriber->getCustomerId();
if(!empty($customerId)){
$arr['customerId'] = $customerId;
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lib-libxml": "*"
},
"type": "magento2-module",
"version": "2.2.12",
"version": "2.2.13",
"license": [
"OSL-3.0",
"AFL-3.0"
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Remarkety_Mgconnector" setup_version="2.2.12">
<module name="Remarkety_Mgconnector" setup_version="2.2.13">
</module>
</config>

0 comments on commit 85ead80

Please sign in to comment.