From 7e965b4e7422b10e965f4c84835e8aa3df8625af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Lys=C3=BD?= Date: Fri, 14 Jan 2022 11:05:37 +0100 Subject: [PATCH] Remove exact comparison with the last order id [MAILPOET-4065] --- lib/Cron/Workers/WooCommerceSync.php | 10 +++++----- lib/Segments/WooCommerce.php | 16 +++++----------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/lib/Cron/Workers/WooCommerceSync.php b/lib/Cron/Workers/WooCommerceSync.php index 067551ab69..70fa5213d1 100644 --- a/lib/Cron/Workers/WooCommerceSync.php +++ b/lib/Cron/Workers/WooCommerceSync.php @@ -41,21 +41,21 @@ class WooCommerceSync extends SimpleWorker { $meta = $task->getMeta(); $highestOrderId = $this->getHighestOrderId(); - if (!isset($meta['last_processed_order_id'])) { - $meta['last_processed_order_id'] = 0; + if (!isset($meta['last_checked_order_id'])) { + $meta['last_checked_order_id'] = 0; } do { $this->cronHelper->enforceExecutionLimit($timer); - $meta['last_processed_order_id'] = $this->woocommerceSegment->synchronizeCustomers( - $meta['last_processed_order_id'], + $meta['last_checked_order_id'] = $this->woocommerceSegment->synchronizeCustomers( + $meta['last_checked_order_id'], $highestOrderId, self::BATCH_SIZE ); $task->setMeta($meta); $this->scheduledTasksRepository->persist($task); $this->scheduledTasksRepository->flush(); - } while ($meta['last_processed_order_id'] !== $highestOrderId); + } while ($meta['last_checked_order_id'] < $highestOrderId); return true; } diff --git a/lib/Segments/WooCommerce.php b/lib/Segments/WooCommerce.php index f734d27188..721cb22e79 100644 --- a/lib/Segments/WooCommerce.php +++ b/lib/Segments/WooCommerce.php @@ -167,23 +167,17 @@ class WooCommerce { } } - public function synchronizeCustomers(int $lastProcessedOrderId = 0, ?int $highestOrderId = null, int $batchSize = 1000): int { + public function synchronizeCustomers(int $lastCheckedOrderId = 0, ?int $highestOrderId = null, int $batchSize = 1000): int { $this->wpSegment->synchronizeUsers(); // synchronize registered users $this->markRegisteredCustomers(); - $processedOrders = $this->insertSubscribersFromOrders($lastProcessedOrderId, $batchSize); + $processedOrders = $this->insertSubscribersFromOrders($lastCheckedOrderId, $batchSize); $this->updateNames($processedOrders); - // When a batch of posts doesn't contain any shop order with customer email, - // it returns the highest order id used in subscribers sync to prevent infinite loop - if (!$processedOrders) { - $lastProcessedOrderId = $lastProcessedOrderId + $batchSize; - } else { - $lastProcessedOrderId = end($processedOrders); - } - if (!$highestOrderId || $lastProcessedOrderId === $highestOrderId) { + $lastCheckedOrderId = $lastCheckedOrderId + $batchSize; + if (!$highestOrderId || $lastCheckedOrderId >= $highestOrderId) { $this->insertUsersToSegment(); $this->unsubscribeUsersFromSegment(); $this->removeOrphanedSubscribers(); @@ -191,7 +185,7 @@ class WooCommerce { $this->updateGlobalStatus(); } - return (int)$lastProcessedOrderId; + return $lastCheckedOrderId; } private function ensureColumnCollation(): void {