Skip to content

Commit f900207

Browse files
authored
Merge pull request #1779 from algolia/feat/MAGE-1138-use-tmp-index
MAGE-1138: Configurable temporary index usage
2 parents d0dcd9e + aa36598 commit f900207

File tree

9 files changed

+226
-60
lines changed

9 files changed

+226
-60
lines changed

Helper/ConfigHelper.php

Lines changed: 75 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Algolia\AlgoliaSearch\Api\Product\ReplicaManagerInterface;
66
use Algolia\AlgoliaSearch\Helper\Configuration\AutocompleteHelper;
77
use Algolia\AlgoliaSearch\Helper\Configuration\InstantSearchHelper;
8+
use Algolia\AlgoliaSearch\Helper\Configuration\QueueHelper;
89
use Algolia\AlgoliaSearch\Service\AlgoliaConnector;
910
use Algolia\AlgoliaSearch\Service\Serializer;
1011
use Magento\Cookie\Helper\Cookie as CookieHelper;
@@ -42,11 +43,6 @@ class ConfigHelper
4243
public const INDEX_EMPTY_CATEGORIES = 'algoliasearch_categories/categories/index_empty_categories';
4344
public const CATEGORY_SEPARATOR = 'algoliasearch_categories/categories/category_separator';
4445

45-
public const IS_ACTIVE = 'algoliasearch_queue/queue/active';
46-
public const USE_BUILT_IN_CRON = 'algoliasearch_queue/queue/use_built_in_cron';
47-
public const NUMBER_OF_JOB_TO_RUN = 'algoliasearch_queue/queue/number_of_job_to_run';
48-
public const RETRY_LIMIT = 'algoliasearch_queue/queue/number_of_retries';
49-
5046
public const XML_PATH_IMAGE_WIDTH = 'algoliasearch_images/image/width';
5147
public const XML_PATH_IMAGE_HEIGHT = 'algoliasearch_images/image/height';
5248
public const XML_PATH_IMAGE_TYPE = 'algoliasearch_images/image/type';
@@ -156,7 +152,8 @@ public function __construct(
156152
protected GroupExcludedWebsiteRepositoryInterface $groupExcludedWebsiteRepository,
157153
protected CookieHelper $cookieHelper,
158154
protected AutocompleteHelper $autocompleteConfig,
159-
protected InstantSearchHelper $instantSearchConfig
155+
protected InstantSearchHelper $instantSearchConfig,
156+
protected QueueHelper $queueHelper
160157
)
161158
{}
162159

@@ -384,44 +381,6 @@ public function getNumberOfElementByPage($storeId = null)
384381
return (int)$this->configInterface->getValue(self::NUMBER_OF_ELEMENT_BY_PAGE, ScopeInterface::SCOPE_STORE, $storeId);
385382
}
386383

387-
/**
388-
* @param $storeId
389-
* @return mixed
390-
*/
391-
public function getNumberOfJobToRun($storeId = null)
392-
{
393-
$nbJobs = (int)$this->configInterface->getValue(self::NUMBER_OF_JOB_TO_RUN, ScopeInterface::SCOPE_STORE, $storeId);
394-
395-
return max($nbJobs, 1);
396-
}
397-
398-
/**
399-
* @param $storeId
400-
* @return int
401-
*/
402-
public function getRetryLimit($storeId = null)
403-
{
404-
return (int)$this->configInterface->getValue(self::RETRY_LIMIT, ScopeInterface::SCOPE_STORE, $storeId);
405-
}
406-
407-
/**
408-
* @param $storeId
409-
* @return bool
410-
*/
411-
public function isQueueActive($storeId = null)
412-
{
413-
return $this->configInterface->isSetFlag(self::IS_ACTIVE, ScopeInterface::SCOPE_STORE, $storeId);
414-
}
415-
416-
/**
417-
* @param $storeId
418-
* @return bool
419-
*/
420-
public function useBuiltInCron($storeId = null)
421-
{
422-
return $this->configInterface->isSetFlag(self::USE_BUILT_IN_CRON, ScopeInterface::SCOPE_STORE, $storeId);
423-
}
424-
425384
/**
426385
* @param $storeId
427386
* @return bool
@@ -1715,6 +1674,32 @@ public function isQueueIndexerEnabled(): bool
17151674
*/
17161675
public const LEGACY_USE_VIRTUAL_REPLICA_ENABLED = 'algoliasearch_instant/instant/use_virtual_replica';
17171676

1677+
// --- Indexing Queue --- //
1678+
1679+
/**
1680+
* @deprecated This constant has been moved to a domain specific config helper and will be removed in a future release
1681+
* @see \Algolia\AlgoliaSearch\Helper\Configuration\QueueHelper::IS_ACTIVE
1682+
*/
1683+
public const IS_ACTIVE = QueueHelper::IS_ACTIVE;
1684+
1685+
/**
1686+
* @deprecated This constant has been moved to a domain specific config helper and will be removed in a future release
1687+
* @see \Algolia\AlgoliaSearch\Helper\Configuration\QueueHelper::USE_BUILT_IN_CRON
1688+
*/
1689+
public const USE_BUILT_IN_CRON = QueueHelper::USE_BUILT_IN_CRON;
1690+
1691+
/**
1692+
* @deprecated This constant has been moved to a domain specific config helper and will be removed in a future release
1693+
* @see \Algolia\AlgoliaSearch\Helper\Configuration\QueueHelper::NUMBER_OF_JOB_TO_RUN
1694+
*/
1695+
public const NUMBER_OF_JOB_TO_RUN = QueueHelper::NUMBER_OF_JOB_TO_RUN;
1696+
1697+
/**
1698+
* @deprecated This constant has been moved to a domain specific config helper and will be removed in a future release
1699+
* @see \Algolia\AlgoliaSearch\Helper\Configuration\QueueHelper::RETRY_LIMIT
1700+
*/
1701+
public const RETRY_LIMIT = QueueHelper::RETRY_LIMIT;
1702+
17181703
// --- Indexing Manager --- //
17191704

17201705
/**
@@ -2045,6 +2030,52 @@ public function hidePaginationInInstantSearchPage($storeId = null)
20452030
return $this->instantSearchConfig->shouldHidePagination($storeId);
20462031
}
20472032

2033+
// --- Indexing Queue --- //
2034+
2035+
/**
2036+
* @param $storeId
2037+
* @return bool
2038+
* @deprecated This method has been moved to the Queue config helper and will be removed in a future version
2039+
* @see \Algolia\AlgoliaSearch\Helper\Configuration\QueueHelper::isQueueActive()
2040+
*/
2041+
public function isQueueActive($storeId = null)
2042+
{
2043+
return $this->queueHelper->isQueueActive($storeId);
2044+
}
2045+
2046+
/**
2047+
* @param $storeId
2048+
* @return bool
2049+
* @deprecated This method has been moved to the Queue config helper and will be removed in a future version
2050+
* @see \Algolia\AlgoliaSearch\Helper\Configuration\QueueHelper::useBuiltInCron()
2051+
*/
2052+
public function useBuiltInCron($storeId = null)
2053+
{
2054+
return $this->queueHelper->useBuiltInCron($storeId);
2055+
}
2056+
2057+
/**
2058+
* @param $storeId
2059+
* @return bool
2060+
* @deprecated This method has been moved to the Queue config helper and will be removed in a future version
2061+
* @see \Algolia\AlgoliaSearch\Helper\Configuration\QueueHelper::getNumberOfJobToRun()
2062+
*/
2063+
public function getNumberOfJobToRun($storeId = null)
2064+
{
2065+
return $this->queueHelper->getNumberOfJobToRun($storeId);
2066+
}
2067+
2068+
/**
2069+
* @param $storeId
2070+
* @return bool
2071+
* @deprecated This method has been moved to the Queue config helper and will be removed in a future version
2072+
* @see \Algolia\AlgoliaSearch\Helper\Configuration\QueueHelper::getRetryLimit()
2073+
*/
2074+
public function getRetryLimit($storeId = null)
2075+
{
2076+
return $this->queueHelper->getRetryLimit($storeId);
2077+
}
2078+
20482079
// --- Indexing Manager --- //
20492080

20502081
/**

Helper/Configuration/QueueHelper.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Helper\Configuration;
4+
5+
use Magento\Framework\App\Config\ScopeConfigInterface;
6+
use Magento\Store\Model\ScopeInterface;
7+
8+
class QueueHelper
9+
{
10+
public const IS_ACTIVE = 'algoliasearch_queue/queue/active';
11+
public const USE_BUILT_IN_CRON = 'algoliasearch_queue/queue/use_built_in_cron';
12+
public const NUMBER_OF_JOB_TO_RUN = 'algoliasearch_queue/queue/number_of_job_to_run';
13+
public const RETRY_LIMIT = 'algoliasearch_queue/queue/number_of_retries';
14+
public const USE_TMP_INDEX = 'algoliasearch_queue/queue/use_tmp_index';
15+
16+
public function __construct(
17+
protected ScopeConfigInterface $configInterface,
18+
) {}
19+
20+
/**
21+
* @param $storeId
22+
* @return bool
23+
*/
24+
public function isQueueActive($storeId = null)
25+
{
26+
return $this->configInterface->isSetFlag(self::IS_ACTIVE, ScopeInterface::SCOPE_STORE, $storeId);
27+
}
28+
29+
/**
30+
* @param $storeId
31+
* @return bool
32+
*/
33+
public function useBuiltInCron($storeId = null)
34+
{
35+
return $this->configInterface->isSetFlag(self::USE_BUILT_IN_CRON, ScopeInterface::SCOPE_STORE, $storeId);
36+
}
37+
38+
/**
39+
* @param $storeId
40+
* @return mixed
41+
*/
42+
public function getNumberOfJobToRun($storeId = null)
43+
{
44+
$nbJobs = (int)$this->configInterface->getValue(self::NUMBER_OF_JOB_TO_RUN, ScopeInterface::SCOPE_STORE, $storeId);
45+
46+
return max($nbJobs, 1);
47+
}
48+
49+
/**
50+
* @param $storeId
51+
* @return int
52+
*/
53+
public function getRetryLimit($storeId = null)
54+
{
55+
return (int)$this->configInterface->getValue(self::RETRY_LIMIT, ScopeInterface::SCOPE_STORE, $storeId);
56+
}
57+
58+
/**
59+
* @param $storeId
60+
* @return bool
61+
*/
62+
public function useTmpIndex($storeId = null)
63+
{
64+
return $this->isQueueActive($storeId) &&
65+
$this->configInterface->isSetFlag(self::USE_TMP_INDEX, ScopeInterface::SCOPE_STORE, $storeId);
66+
}
67+
}

Service/Product/BatchQueueProcessor.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Algolia\AlgoliaSearch\Exception\DiagnosticsException;
77
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
88
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
9+
use Algolia\AlgoliaSearch\Helper\Configuration\QueueHelper;
910
use Algolia\AlgoliaSearch\Helper\Data;
1011
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
1112
use Algolia\AlgoliaSearch\Logger\DiagnosticsLogger;
@@ -24,6 +25,7 @@ public function __construct(
2425
protected Data $dataHelper,
2526
protected ConfigHelper $configHelper,
2627
protected ProductHelper $productHelper,
28+
protected QueueHelper $queueHelper,
2729
protected Queue $queue,
2830
protected DiagnosticsLogger $diag,
2931
protected AlgoliaCredentialsManager $algoliaCredentialsManager,
@@ -56,7 +58,7 @@ public function processBatch(int $storeId, ?array $entityIds = null): void
5658
return;
5759
}
5860

59-
$useTmpIndex = $this->configHelper->isQueueActive($storeId);
61+
$useTmpIndex = $this->queueHelper->useTmpIndex($storeId);
6062
$this->syncAlgoliaSettings($storeId, $useTmpIndex);
6163

6264
$this->handleFullIndex($storeId, $productsPerPage, $useTmpIndex);

Service/Product/IndexBuilder.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ public function buildIndex(int $storeId, ?array $entityIds, ?array $options): vo
101101
$collection,
102102
$options['page'] ?? 1,
103103
$options['pageSize'] ?? $this->configHelper->getNumberOfElementByPage($storeId),
104-
$entityIds
104+
$entityIds,
105+
$options['useTmpIndex'] ?? false
105106
);
106107

107108
$this->stopEmulation();
@@ -144,6 +145,7 @@ public function deleteInactiveProducts($storeId): void
144145
* @param int $page
145146
* @param int $pageSize
146147
* @param array|null $productIds - pre-batched product ids - if specified no paging will be applied
148+
* @param bool|null $useTmpIndex
147149
* @return void
148150
* @throws AlgoliaException
149151
* @throws DiagnosticsException
@@ -154,7 +156,8 @@ protected function buildIndexPage(
154156
Collection $collection,
155157
int $page,
156158
int $pageSize,
157-
?array $productIds = null
159+
?array $productIds = null,
160+
?bool $useTmpIndex = false
158161
): void
159162
{
160163
if ($this->isIndexingEnabled($storeId) === false) {

Setup/Patch/Schema/ConfigPatch.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class ConfigPatch implements SchemaPatchInterface
6868
'algoliasearch_queue/queue/active' => '0',
6969
'algoliasearch_queue/queue/number_of_job_to_run' => '5',
7070
'algoliasearch_queue/queue/number_of_retries' => '3',
71+
'algoliasearch_queue/queue/use_tmp_index' => '1',
7172

7273
'algoliasearch_cc_analytics/cc_analytics_group/enable' => '0',
7374
'algoliasearch_cc_analytics/cc_analytics_group/is_selector' => '.ais-Hits-item a.result, .ais-InfiniteHits-item a.result',

0 commit comments

Comments
 (0)