Skip to content

Commit

Permalink
bug #14482 Adding batching to the expired carts remover v2 (mamazu, l…
Browse files Browse the repository at this point in the history
…chrusciel)

This PR was merged into the 1.11 branch.

Discussion
----------

| Q               | A                                                            |
|-----------------|--------------------------------------------------------------|
| Branch?         | 1.11 |
| Bug fix?        | yes                                                       |
| New feature?    | no                                                       |
| BC breaks?      | no                                                       |
| Deprecations?   | no |
| Related tickets | Backporting and fixing #14481 |
| License         | MIT                                                          |

<!--
 - Bug fixes must be submitted against the 1.11 branch
 - Features and deprecations must be submitted against the 1.12 branch
 - Make sure that the correct base branch is set

 To be sure you are not breaking any Backward Compatibilities, check the documentation:
 https://docs.sylius.com/en/latest/book/organization/backward-compatibility-promise.html
-->


Commits
-------

eeeafba Adding batching to the expired carts remover
5b4d7b3 Update src/Sylius/Bundle/OrderBundle/Remover/ExpiredCartsRemover.php
fa86a8c [Order] Skip empty flush during expired carts removal
  • Loading branch information
Zales0123 committed Oct 26, 2022
2 parents 4a57e99 + fa86a8c commit 1f80a82
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/Sylius/Bundle/OrderBundle/Remover/ExpiredCartsRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function __construct(
private ObjectManager $orderManager,
private EventDispatcherInterface $eventDispatcher,
private string $expirationPeriod,
private int $batchSize = 100,
) {
}

Expand All @@ -36,11 +37,19 @@ public function remove(): void

$this->eventDispatcher->dispatch(new GenericEvent($expiredCarts), SyliusExpiredCartsEvents::PRE_REMOVE);

$interval = 0;
foreach ($expiredCarts as $expiredCart) {
$this->orderManager->remove($expiredCart);
$interval++;

if ($interval % $this->batchSize === 0) {
$this->orderManager->flush();
}
}

$this->orderManager->flush();
if ($interval % $this->batchSize !== 0) {
$this->orderManager->flush();
}

$this->eventDispatcher->dispatch(new GenericEvent($expiredCarts), SyliusExpiredCartsEvents::POST_REMOVE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,33 @@ function it_removes_a_cart_which_has_been_updated_before_configured_date(
->shouldBeCalled()
;

$orderManager->remove($firstCart);
$orderManager->remove($secondCart);
$orderManager->flush();
$orderManager->remove($firstCart)->shouldBeCalledOnce();
$orderManager->remove($secondCart)->shouldBeCalledOnce();
$orderManager->flush()->shouldBeCalledOnce();

$eventDispatcher
->dispatch(Argument::any(), SyliusExpiredCartsEvents::POST_REMOVE)
->shouldBeCalled()
;

$this->remove();
}

function it_removes_carts_in_batches(
OrderRepositoryInterface $orderRepository,
ObjectManager $orderManager,
EventDispatcher $eventDispatcher,
OrderInterface $cart,
): void {
$orderRepository->findCartsNotModifiedSince(Argument::type('\DateTimeInterface'))->willReturn(array_fill(0, 200, $cart));

$eventDispatcher
->dispatch(Argument::any(), SyliusExpiredCartsEvents::PRE_REMOVE)
->shouldBeCalled()
;

$orderManager->remove(Argument::type(OrderInterface::class))->shouldBeCalledTimes(200);
$orderManager->flush()->shouldBeCalledTimes(2);

$eventDispatcher
->dispatch(Argument::any(), SyliusExpiredCartsEvents::POST_REMOVE)
Expand Down

0 comments on commit 1f80a82

Please sign in to comment.