Skip to content

Commit

Permalink
Revert "Delete delete queue"
Browse files Browse the repository at this point in the history
This reverts commit 99293cf.
  • Loading branch information
phonglynosto committed May 29, 2023
1 parent 70eb790 commit f1fee82
Show file tree
Hide file tree
Showing 11 changed files with 394 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Model/Indexer/ProductIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,38 @@ public function doIndex(Store $store, array $ids = [])
$collection,
$store
);
$this->handleDeletedProducts($collection, $store, $ids);
}

/**
* @param ProductCollection $existingCollection
* @param Store $store
* @param array $givenIds
* @throws NostoException
*/
private function handleDeletedProducts(ProductCollection $existingCollection, Store $store, array $givenIds)
{
if (!empty($givenIds)) {
$existingCollection->setPageSize(1000);
$iterator = new PagingIterator($existingCollection);
$present = [];
foreach ($iterator as $page) {
foreach ($page->getItems() as $item) {
/** @noinspection PhpPossiblePolymorphicInvocationInspection */
$id = $item->getId();
$present[$id] = $id;
}
}
$removed = [];
foreach ($givenIds as $productId) {
if (!isset($present[$productId])) {
$removed[] = $productId;
}
}
if (count($removed) > 0) {
$this->productUpdateService->addIdsToDeleteMessageQueue($removed, $store);
}
}
}

/**
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);
}
}
Loading

0 comments on commit f1fee82

Please sign in to comment.