Skip to content

Commit

Permalink
Category connection with Queue
Browse files Browse the repository at this point in the history
  • Loading branch information
ugljesaspx committed Nov 1, 2024
1 parent e7bafbf commit d06c586
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 23 deletions.
15 changes: 7 additions & 8 deletions Model/Service/Sync/AbstractBulkPublisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,37 +100,36 @@ public function __construct(// @codingStandardsIgnoreLine
* @inheritDoc
* @throws LocalizedException
*/
public function execute(int $storeId, array $productIds = [])
public function execute(int $storeId, array $entityIds = [])
{
if (!empty($productIds)) {
$this->publishCollectionToMessageQueue($storeId, $productIds);
if (!empty($entityIds)) {
$this->publishCollectionToMessageQueue($storeId, $entityIds);
}
}

/**
* @param $storeId
* @param $productIds
* @param $entityIds
* @throws LocalizedException
* @throws Exception
*/
private function publishCollectionToMessageQueue(
$storeId,
$productIds
$entityIds
) {

if (!$this->canUseAsyncOperations()) {
$this->logger->critical(
"Module Magento_AsynchronousOperations not available. Aborting bulk publish operation"
);
return;
}
$productIdsChunks = array_chunk($productIds, $this->getBulkSize());
$productIdsChunks = array_chunk($entityIds, $this->getBulkSize());
$bulkUuid = $this->identityService->generateId();
/**
* Argument is of type string but array is expected
*/
/** @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal */
$bulkDescription = __('Sync ' . count($productIds) . ' Nosto products');
$bulkDescription = __('Sync ' . count($entityIds) . ' Nosto products');
$operationsData = [];
foreach ($productIdsChunks as $productIdsChunk) {
$operationsData[] = $this->buildOperationData(
Expand Down
4 changes: 2 additions & 2 deletions Model/Service/Sync/BulkPublisherInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ interface BulkPublisherInterface
{
/**
* @param int $storeId
* @param array $productIds
* @param array $entityIds
* @return void
*/
public function execute(int $storeId, array $productIds = []);
public function execute(int $storeId, array $entityIds = []);
}
10 changes: 5 additions & 5 deletions Model/Service/Sync/Upsert/Category/AsyncBulkConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
use Nosto\NostoException;
use Nosto\Tagging\Helper\Scope as NostoScopeHelper;
use Nosto\Tagging\Logger\Logger;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Nosto\Tagging\Model\ResourceModel\Magento\Product\CollectionFactory;
use Nosto\Tagging\Model\Service\Sync\AbstractBulkConsumer;
use Nosto\Tagging\Model\Service\Sync\Upsert\SyncService;

Expand Down Expand Up @@ -98,12 +98,12 @@ public function __construct(
* @throws MemoryOutOfBoundsException
* @throws NostoException
*/
public function doOperation(array $categoryIds, string $storeId)
public function doOperation(array $productIds, string $storeId)
{
$store = $this->nostoScopeHelper->getStore($storeId);
$categoryCollection = $this->collectionFactory->create()
->addIdsToFilter($categoryIds)
$productCollection = $this->collectionFactory->create()
->addIdsToFilter($productIds)
->addStoreFilter($storeId);
$this->syncService->syncProducts($categoryCollection, $store);
$this->syncService->syncProducts($productCollection, $store);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
use Nosto\Tagging\Model\Service\Sync\AbstractBulkPublisher;

// @codingStandardsIgnoreFile
class BulkPublisher extends AbstractBulkPublisher
class AsyncBulkPublisher extends AbstractBulkPublisher
{
public const NOSTO_SYNC_MESSAGE_QUEUE = 'nosto_category_sync.update';
public const BULK_SIZE = 100;
Expand Down
121 changes: 121 additions & 0 deletions Plugin/CategoryUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?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\Plugin;

use Closure;
use Magento\Catalog\Model\ResourceModel\Category as MagentoResourceCategory;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Framework\Model\AbstractModel;
use Nosto\Tagging\Helper\Scope as NostoHelperScope;
use Nosto\Tagging\Model\Indexer\CategoryIndexer;
use Nosto\Tagging\Model\Category\Repository as NostoCategoryRepository;
use Nosto\Tagging\Logger\Logger as NostoLogger;
use Nosto\Tagging\Model\ResourceModel\Magento\Category\CollectionBuilder;
use Nosto\Tagging\Model\Service\Update\CategoryUpdateService;

/**
* Plugin for product updates
*/
class CategoryUpdate
{
/** @var IndexerRegistry */
private IndexerRegistry $indexerRegistry;

/** @var CategoryIndexer */
private CategoryIndexer $categoryIndexer;

/** @var NostoCategoryRepository */
private NostoCategoryRepository $nostoCategoryRepository;

/** @var NostoLogger */
private NostoLogger $logger;

/** @var CategoryUpdateService */
private CategoryUpdateService $categoryUpdateService;

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

/** @var CollectionBuilder */
private CollectionBuilder $categoryCollectionBuilder;

/**
* ProductUpdate constructor.
* @param IndexerRegistry $indexerRegistry
* @param CategoryIndexer $categoryIndexer
* @param NostoCategoryRepository $nostoCategoryRepository
* @param NostoLogger $logger
* @param CategoryUpdateService $categoryUpdateService
* @param NostoHelperScope $nostoHelperScope
*/
public function __construct(
IndexerRegistry $indexerRegistry,
CategoryIndexer $categoryIndexer,
NostoCategoryRepository $nostoCategoryRepository,
NostoLogger $logger,
CategoryUpdateService $categoryUpdateService,
NostoHelperScope $nostoHelperScope,
CollectionBuilder $categoryCollectionBuilder
) {
$this->indexerRegistry = $indexerRegistry;
$this->categoryIndexer = $categoryIndexer;
$this->nostoCategoryRepository = $nostoCategoryRepository;
$this->logger = $logger;
$this->categoryUpdateService = $categoryUpdateService;
$this->nostoHelperScope = $nostoHelperScope;
$this->categoryCollectionBuilder = $categoryCollectionBuilder;
}

public function aroundSave(
MagentoResourceCategory $resourceCategory,
Closure $proceed,
AbstractModel $category
) {
$storeIds = $category->getStoreIds();
$categoryCollection = $this->categoryCollectionBuilder->withIds([$category->getId()])->build();

foreach ($storeIds as $storeId) {
$store = $this->nostoHelperScope->getStore($storeId);
$this->categoryUpdateService->addCollectionToUpdateMessageQueue($categoryCollection, $store);
}


return $proceed($category);

}

}
16 changes: 10 additions & 6 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,13 @@
<preference for="Nosto\Tagging\Model\Mview\MviewInterface" type="Nosto\Tagging\Model\Mview\Mview"/>
<preference for="Nosto\Tagging\Model\Service\Indexer\IndexerStatusServiceInterface" type="Nosto\Tagging\Model\Service\Indexer\IndexerStatusService"/>
<preference for="Nosto\Tagging\Model\Service\Sync\BulkPublisherInterface" type="Nosto\Tagging\Model\Service\Sync\Upsert\AsyncBulkPublisher"/>
<preference for="Nosto\Tagging\Model\Service\Sync\BulkPublisherInterface" type="Nosto\Tagging\Model\Service\Sync\Upsert\Category\AsyncBulkPublisher"/>
<preference for="Nosto\Tagging\Model\Cache\Type\ProductDataInterface" type="Nosto\Tagging\Model\Cache\Type\ProductData" />
<type name="Magento\Catalog\Model\ResourceModel\Product">
<plugin name="nostoProductObserverUpdate" type="Nosto\Tagging\Plugin\ProductUpdate"/>
<plugin name="nostoCategoryObserverUpdate" type="Nosto\Tagging\Plugin\ProductUpdate"/>
</type>
<type name="Magento\Catalog\Model\ResourceModel\Category">
<plugin name="nostoProductObserverUpdate" type="Nosto\Tagging\Plugin\CategoryUpdate"/>
</type>
<type name="Nosto\Tagging\Console\Command\NostoAccountConnectCommand">
<arguments>
Expand Down Expand Up @@ -201,6 +205,9 @@
<argument name="indexers" xsi:type="array">
<item name="nosto_index_product" xsi:type="string">nosto_index_product</item>
</argument>
<argument name="indexers" xsi:type="array">
<item name="nosto_index_category" xsi:type="string">nosto_index_category</item>
</argument>
</arguments>
</type>
<type name="Nosto\Tagging\Model\Indexer\ProductIndexer">
Expand All @@ -224,7 +231,7 @@
</argument>
</arguments>
</type>
<type name="Nosto\Tagging\Model\Service\Sync\Upsert\Category\BulkPublisher">
<type name="Nosto\Tagging\Model\Service\Sync\Upsert\Category\AsyncBulkPublisher">
<arguments>
<argument name="AsyncBulkConsumer" xsi:type="object" shared="false">
Nosto\Tagging\Model\Service\Sync\Upsert\Category\AsyncBulkConsumer
Expand Down Expand Up @@ -259,10 +266,7 @@
<type name="Nosto\Tagging\Model\Service\Update\CategoryUpdateService">
<arguments>
<argument name="upsertCategoryBulkPublisher" xsi:type="object" shared="false">
Nosto\Tagging\Model\Service\Sync\Upsert\Category\BulkPublisher
</argument>
<argument name="deleteBulkPublisher" xsi:type="object" shared="false">
Nosto\Tagging\Model\Service\Sync\Delete\AsyncBulkPublisher
Nosto\Tagging\Model\Service\Sync\Upsert\Category\AsyncBulkPublisher
</argument>
<argument name="batchSize" xsi:type="number">500</argument>
</arguments>
Expand Down
2 changes: 1 addition & 1 deletion etc/queue_consumer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<consumer name="nosto_category_sync.update"
queue="nosto_category_sync.update"
connection="amqp"
maxMessages="200"
maxMessages="20"
consumerInstance="Magento\Framework\MessageQueue\Consumer"
handler="Nosto\Tagging\Model\Service\Sync\Upsert\Category\AsyncBulkConsumer::processOperation"/>
</config>
1 change: 1 addition & 0 deletions etc/queue_topology.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@
<exchange name="magento-amqp" type="topic" connection="amqp">
<binding id="updateBindingUpsert" topic="nosto_product_sync.update" destinationType="queue" destination="nosto_product_sync.update"/>
<binding id="updateBindingDelete" topic="nosto_product_sync.delete" destinationType="queue" destination="nosto_product_sync.delete"/>
<binding id="updateBindingCategory" topic="nosto_category_sync.update" destinationType="queue" destination="nosto_category_sync.update"/>
</exchange>
</config>

0 comments on commit d06c586

Please sign in to comment.