From 9098c0157d71c92dbae5e9480f0335f8856713ba Mon Sep 17 00:00:00 2001 From: Mark Middleton Date: Fri, 20 Oct 2023 16:20:40 -0700 Subject: [PATCH] Breaking products into smaller chunks for more efficient loading --- src/jobs/AlgoliaChunkLoadTask.php | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/jobs/AlgoliaChunkLoadTask.php b/src/jobs/AlgoliaChunkLoadTask.php index c8a8b4c..b33685f 100644 --- a/src/jobs/AlgoliaChunkLoadTask.php +++ b/src/jobs/AlgoliaChunkLoadTask.php @@ -96,27 +96,28 @@ public function execute($queue) CASE 'variant': - AlgoliaSync::$plugin->algoliaSyncService->logger("loading a chunck of variants into the Algolia sync queue", basename(__FILE__) , __LINE__); + AlgoliaSync::$plugin->algoliaSyncService->logger("loading a chunk of product variants into the Algolia sync queue", basename(__FILE__) , __LINE__); $commercePlugin = Craft::$app->plugins->getPlugin('commerce'); if ($commercePlugin) { - $variants = \craft\commerce\elements\Variant::find()->typeId($sectionId)->status('enabled')->all(); + $recordCount = \craft\commerce\elements\Variant::find()->typeId($sectionId)->offset($offsetCount)->limit($limitCount)->status('enabled')->count(); + $variants = \craft\commerce\elements\Variant::find()->typeId($sectionId)->offset($offsetCount)->limit($limitCount)->status('enabled')->all(); - $recordCount = count($variants); + if ($recordCount > 0) { + AlgoliaSync::$plugin->algoliaSyncService->logger("Now loading ".$recordCount." products to be synced", basename(__FILE__) , __LINE__); - AlgoliaSync::$plugin->algoliaSyncService->logger("Now loading ".$recordCount." products to be synced", basename(__FILE__) , __LINE__); + $currentLoopCount = 0; + foreach ($variants as $variant) { + $progress = $currentLoopCount / $recordCount; + $this->setProgress($queue, $progress); - $currentLoopCount = 0; - foreach ($variants as $variant) { - $progress = $currentLoopCount / $recordCount; - $this->setProgress($queue, $progress); + AlgoliaSync::$plugin->algoliaSyncService->logger(print_r($variant, true), basename(__FILE__) , __LINE__); - AlgoliaSync::$plugin->algoliaSyncService->logger(print_r($variant, true), basename(__FILE__) , __LINE__); - - AlgoliaSync::$plugin->algoliaSyncService->prepareAlgoliaSyncElement($variant); - $currentLoopCount++; + AlgoliaSync::$plugin->algoliaSyncService->prepareAlgoliaSyncElement($variant); + $currentLoopCount++; + } } } break;