Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into feature/collect-ca…
Browse files Browse the repository at this point in the history
…tegory-listing
  • Loading branch information
phonglynosto committed Jun 27, 2023
2 parents 8de0bfa + 10cd477 commit 5b78327
Show file tree
Hide file tree
Showing 16 changed files with 431 additions and 38 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. This projec
* Update tagging with category listing support
* Add category export

### 7.2.5
* Restore `nosto_product_sync.delete` message queue consumer to handle product deletion
>>>>>>> origin/develop
### 7.2.4
* Add null check in order observer to allow for orders overriding

Expand All @@ -17,7 +21,7 @@ All notable changes to this project will be documented in this file. This projec
* Add store filter to the DefaulCategoryService to generate categories for specific store

### 7.2.0
* Remove `nosto_product_sync` message queue consumer
* Remove `nosto_product_sync.delete` message queue consumer

### 7.1.2
* Remove unreffered system configuration for enabling 'Indexer full reindex' for Nosto indexers
Expand Down
93 changes: 93 additions & 0 deletions Model/Service/Sync/Delete/AsyncBulkConsumer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* Copyright (c) 2020, Nosto Solutions Ltd
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author Nosto Solutions Ltd <[email protected]>
* @copyright 2020 Nosto Solutions Ltd
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause
*
*/

namespace Nosto\Tagging\Model\Service\Sync\Delete;

use Nosto\NostoException;
use Nosto\Tagging\Model\Service\Sync\AbstractBulkConsumer;
use Nosto\Tagging\Helper\Scope as NostoHelperScope;
use Magento\Framework\EntityManager\EntityManager;
use Magento\Framework\Json\Helper\Data as JsonHelper;
use Nosto\Tagging\Logger\Logger;
use Magento\Store\Model\App\Emulation;

class AsyncBulkConsumer extends AbstractBulkConsumer
{
/** @var DeleteService */
private DeleteService $deleteService;

/** @var NostoHelperScope */
private NostoHelperScope $nostoHelperScope;

/**
* AsyncBulkConsumer constructor.
* @param DeleteService $deleteService
* @param NostoHelperScope $nostoHelperScope
* @param JsonHelper $jsonHelper
* @param EntityManager $entityManager
* @param Emulation $storeEmulation
* @param Logger $logger
*/
public function __construct(
DeleteService $deleteService,
NostoHelperScope $nostoHelperScope,
JsonHelper $jsonHelper,
EntityManager $entityManager,
Emulation $storeEmulation,
Logger $logger
) {
$this->deleteService = $deleteService;
$this->nostoHelperScope = $nostoHelperScope;
parent::__construct(
$logger,
$jsonHelper,
$entityManager,
$storeEmulation
);
}

/**
* @inheritDoc
* @param array $productIds
* @param string $storeId
* @throws NostoException
*/
public function doOperation(array $productIds, string $storeId)
{
$store = $this->nostoHelperScope->getStore($storeId);
$this->deleteService->delete($productIds, $store);
}
}
77 changes: 77 additions & 0 deletions Model/Service/Sync/Delete/AsyncBulkPublisher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Copyright (c) 2020, Nosto Solutions Ltd
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author Nosto Solutions Ltd <[email protected]>
* @copyright 2020 Nosto Solutions Ltd
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause
*
*/

namespace Nosto\Tagging\Model\Service\Sync\Delete;

use Nosto\Tagging\Model\Service\Sync\AbstractBulkPublisher;

class AsyncBulkPublisher extends AbstractBulkPublisher
{
public const NOSTO_DELETE_MESSAGE_QUEUE = 'nosto_product_sync.delete';
public const BULK_SIZE = 100;

/**
* @inheritDoc
*/
public function getTopicName(): string
{
return self::NOSTO_DELETE_MESSAGE_QUEUE;
}

/**
* @inheritDoc
*/
public function getBulkSize(): int
{
return self::BULK_SIZE;
}

/**
* @inheritDoc
*/
public function getBulkDescription(): string
{
return sprintf('Delete %d Nosto products', 2);
}

/**
* @inheritDoc
*/
public function getMetaData(): string
{
return 'Delete Nosto products';
}
}
134 changes: 134 additions & 0 deletions Model/Service/Sync/Delete/DeleteService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php
/**
* Copyright (c) 2020, Nosto Solutions Ltd
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author Nosto Solutions Ltd <[email protected]>
* @copyright 2020 Nosto Solutions Ltd
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause
*
*/

namespace Nosto\Tagging\Model\Service\Sync\Delete;

use Exception;
use Magento\Store\Model\Store;
use Nosto\Model\Signup\Account as NostoSignupAccount;
use Nosto\NostoException;
use Nosto\Operation\DeleteProduct;
use Nosto\Tagging\Helper\Account as NostoHelperAccount;
use Nosto\Tagging\Helper\Data as NostoHelperData;
use Nosto\Tagging\Helper\Url as NostoHelperUrl;
use Nosto\Tagging\Logger\Logger as NostoLogger;
use Nosto\Tagging\Model\Service\AbstractService;
use Nosto\Tagging\Model\Service\Cache\CacheService;

class DeleteService extends AbstractService
{

public const BENCHMARK_DELETE_NAME = 'nosto_product_delete';
public const BENCHMARK_DELETE_BREAKPOINT = 1;
public const PRODUCT_DELETION_BATCH_SIZE = 100;

/** @var CacheService */
private CacheService $cacheService;

/** @var NostoHelperAccount */
private NostoHelperAccount $nostoHelperAccount;

/** @var NostoHelperUrl */
private NostoHelperUrl $nostoHelperUrl;

/** @var int */
private int $deleteBatchSize;

/**
* DeleteService constructor.
* @param CacheService $cacheService
* @param NostoHelperAccount $nostoHelperAccount
* @param NostoHelperData $nostoHelperData
* @param NostoHelperUrl $nostoHelperUrl
* @param NostoLogger $logger
* @param $deleteBatchSize
*/
public function __construct(
CacheService $cacheService,
NostoHelperAccount $nostoHelperAccount,
NostoHelperData $nostoHelperData,
NostoHelperUrl $nostoHelperUrl,
NostoLogger $logger,
$deleteBatchSize
) {
$this->cacheService = $cacheService;
$this->nostoHelperAccount = $nostoHelperAccount;
$this->nostoHelperUrl = $nostoHelperUrl;
$this->deleteBatchSize = $deleteBatchSize;
parent::__construct($nostoHelperData, $nostoHelperAccount, $logger);
}

/**
* Discontinues products in Nosto and removes indexed products from Nosto product index
*
* @param array $productIds
* @param Store $store
* @throws NostoException
*/
public function delete(array $productIds, Store $store)
{
if (count($productIds) === 0) {
return;
}
$account = $this->nostoHelperAccount->findAccount($store);
if ($account instanceof NostoSignupAccount === false) {
throw new NostoException(sprintf('Store view %s does not have Nosto installed', $store->getName()));
}
$this->startBenchmark(self::BENCHMARK_DELETE_NAME, self::BENCHMARK_DELETE_BREAKPOINT);
$productIdBatches = array_chunk($productIds, $this->deleteBatchSize);
$this->logDebugWithStore(
sprintf(
'Deleting total of %d products in batches of %d',
count($productIds),
count($productIdBatches)
),
$store
);
foreach ($productIdBatches as $ids) {
try {
$op = new DeleteProduct($account, $this->nostoHelperUrl->getActiveDomain($store));
$op->setResponseTimeout(30);
$op->setProductIds($ids);
$op->delete(); // @codingStandardsIgnoreLine
$this->cacheService->removeByProductIds($store, $ids);
$this->tickBenchmark(self::BENCHMARK_DELETE_NAME);
} catch (Exception $e) {
$this->getLogger()->exception($e);
}
}
$this->logBenchmarkSummary(self::BENCHMARK_DELETE_NAME, $store);
}
}
24 changes: 24 additions & 0 deletions Model/Service/Update/ProductUpdateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,17 @@ class ProductUpdateService extends AbstractService
/** @var BulkPublisherInterface */
private BulkPublisherInterface $upsertBulkPublisher;

/** @var BulkPublisherInterface */
private BulkPublisherInterface $deleteBulkPublisher;

/**
* ProductUpdateService constructor.
* @param NostoLogger $logger
* @param NostoDataHelper $nostoDataHelper
* @param NostoAccountHelper $nostoAccountHelper
* @param NostoProductRepository $nostoProductRepository
* @param BulkPublisherInterface $upsertBulkPublisher
* @param BulkPublisherInterface $deleteBulkPublisher
* @param int $batchSize
*/
public function __construct(
Expand All @@ -76,11 +80,13 @@ public function __construct(
NostoAccountHelper $nostoAccountHelper,
NostoProductRepository $nostoProductRepository,
BulkPublisherInterface $upsertBulkPublisher,
BulkPublisherInterface $deleteBulkPublisher,
int $batchSize
) {
parent::__construct($nostoDataHelper, $nostoAccountHelper, $logger);
$this->nostoProductRepository = $nostoProductRepository;
$this->upsertBulkPublisher = $upsertBulkPublisher;
$this->deleteBulkPublisher = $deleteBulkPublisher;
$this->batchSize = $batchSize;
}

Expand Down Expand Up @@ -117,6 +123,24 @@ public function addCollectionToUpdateMessageQueue(ProductCollection $collection,
}
}

/**
* Sets the product ids into the delete message queue
*
* @param array $productIds
* @param Store $store
*/
public function addIdsToDeleteMessageQueue(array $productIds, Store $store)
{
if ($this->getAccountHelper()->findAccount($store) === null) {
$this->logDebugWithStore('No nosto account found for the store', $store);
return;
}
$batchedIds = array_chunk($productIds, $this->batchSize);
foreach ($batchedIds as $idBatch) {
$this->deleteBulkPublisher->execute($store->getId(), $idBatch);
}
}

/**
* @param ProductCollection $collection
* @return array
Expand Down
Loading

0 comments on commit 5b78327

Please sign in to comment.