Remove exact comparison with the last order id

[MAILPOET-4065]
This commit is contained in:
Jan Lysý
2022-01-14 11:05:37 +01:00
committed by Veljko V
parent d0ea442300
commit 7e965b4e74
2 changed files with 10 additions and 16 deletions

View File

@@ -41,21 +41,21 @@ class WooCommerceSync extends SimpleWorker {
$meta = $task->getMeta(); $meta = $task->getMeta();
$highestOrderId = $this->getHighestOrderId(); $highestOrderId = $this->getHighestOrderId();
if (!isset($meta['last_processed_order_id'])) { if (!isset($meta['last_checked_order_id'])) {
$meta['last_processed_order_id'] = 0; $meta['last_checked_order_id'] = 0;
} }
do { do {
$this->cronHelper->enforceExecutionLimit($timer); $this->cronHelper->enforceExecutionLimit($timer);
$meta['last_processed_order_id'] = $this->woocommerceSegment->synchronizeCustomers( $meta['last_checked_order_id'] = $this->woocommerceSegment->synchronizeCustomers(
$meta['last_processed_order_id'], $meta['last_checked_order_id'],
$highestOrderId, $highestOrderId,
self::BATCH_SIZE self::BATCH_SIZE
); );
$task->setMeta($meta); $task->setMeta($meta);
$this->scheduledTasksRepository->persist($task); $this->scheduledTasksRepository->persist($task);
$this->scheduledTasksRepository->flush(); $this->scheduledTasksRepository->flush();
} while ($meta['last_processed_order_id'] !== $highestOrderId); } while ($meta['last_checked_order_id'] < $highestOrderId);
return true; return true;
} }

View File

@@ -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->wpSegment->synchronizeUsers(); // synchronize registered users
$this->markRegisteredCustomers(); $this->markRegisteredCustomers();
$processedOrders = $this->insertSubscribersFromOrders($lastProcessedOrderId, $batchSize); $processedOrders = $this->insertSubscribersFromOrders($lastCheckedOrderId, $batchSize);
$this->updateNames($processedOrders); $this->updateNames($processedOrders);
// When a batch of posts doesn't contain any shop order with customer email, $lastCheckedOrderId = $lastCheckedOrderId + $batchSize;
// it returns the highest order id used in subscribers sync to prevent infinite loop if (!$highestOrderId || $lastCheckedOrderId >= $highestOrderId) {
if (!$processedOrders) {
$lastProcessedOrderId = $lastProcessedOrderId + $batchSize;
} else {
$lastProcessedOrderId = end($processedOrders);
}
if (!$highestOrderId || $lastProcessedOrderId === $highestOrderId) {
$this->insertUsersToSegment(); $this->insertUsersToSegment();
$this->unsubscribeUsersFromSegment(); $this->unsubscribeUsersFromSegment();
$this->removeOrphanedSubscribers(); $this->removeOrphanedSubscribers();
@@ -191,7 +185,7 @@ class WooCommerce {
$this->updateGlobalStatus(); $this->updateGlobalStatus();
} }
return (int)$lastProcessedOrderId; return $lastCheckedOrderId;
} }
private function ensureColumnCollation(): void { private function ensureColumnCollation(): void {