Skip to content

Commit

Permalink
Merge pull request #8488 from magento-lynx/LYNX-199
Browse files Browse the repository at this point in the history
[LYNX] Place order lock process optimization
  • Loading branch information
sidolov authored Sep 3, 2023
2 parents 0cdd86b + 1bb7a03 commit 3cc15fb
Show file tree
Hide file tree
Showing 7 changed files with 6 additions and 233 deletions.
6 changes: 2 additions & 4 deletions app/code/Magento/Quote/Model/QuoteManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class QuoteManagement implements CartManagementInterface, ResetAfterRequestInter
{
private const LOCK_PREFIX = 'PLACE_ORDER_';

private const LOCK_TIMEOUT = 10;
private const LOCK_TIMEOUT = 0;

/**
* @var EventManager
Expand Down Expand Up @@ -615,13 +615,12 @@ protected function submitQuote(QuoteEntity $quote, $orderData = [])
);

$lockedName = self::LOCK_PREFIX . $quote->getId();
if ($this->lockManager->isLocked($lockedName)) {
if (!$this->lockManager->lock($lockedName, self::LOCK_TIMEOUT)) {
throw new LocalizedException(__(
'A server error stopped your order from being placed. Please try to place your order again.'
));
}
try {
$this->lockManager->lock($lockedName, self::LOCK_TIMEOUT);
$order = $this->orderManagement->place($order);
$quote->setIsActive(false);
$this->eventManager->dispatch(
Expand All @@ -632,7 +631,6 @@ protected function submitQuote(QuoteEntity $quote, $orderData = [])
]
);
$this->quoteRepository->save($quote);
$this->lockManager->unlock($lockedName);
} catch (\Exception $e) {
$this->lockManager->unlock($lockedName);
$this->rollbackAddresses($quote, $order, $e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ public function testSubmit(): void
['order' => $order, 'quote' => $quote]
]
);
$this->lockManagerMock->method('isLocked')->willReturn(false);
$this->lockManagerMock->method('lock')->willReturn(true);
$this->quoteRepositoryMock->expects($this->once())->method('save')->with($quote);
$this->assertEquals($order, $this->model->submit($quote, $orderData));
}
Expand Down Expand Up @@ -1378,6 +1378,7 @@ public function testSubmitForCustomer(): void
['order' => $order, 'quote' => $quote]
]
);
$this->lockManagerMock->method('lock')->willReturn(true);
$this->quoteRepositoryMock->expects($this->once())->method('save')->with($quote);
$this->assertEquals($order, $this->model->submit($quote, $orderData));
}
Expand Down Expand Up @@ -1501,7 +1502,7 @@ public function testSubmitWithLockException(): void
['order' => $order, 'quote' => $quote]
]
);
$this->lockManagerMock->method('isLocked')->willReturn(true);
$this->lockManagerMock->method('lock')->willReturn(false);

$this->expectExceptionMessage(
'A server error stopped your order from being placed. Please try to place your order again.'
Expand Down
72 changes: 0 additions & 72 deletions app/code/Magento/QuoteGraphQl/Model/Cart/PlaceOrderMutex.php

This file was deleted.

This file was deleted.

31 changes: 1 addition & 30 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/PlaceOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Magento\QuoteGraphQl\Model\Resolver;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
Expand All @@ -17,7 +16,6 @@
use Magento\QuoteGraphQl\Model\Cart\GetCartForCheckout;
use Magento\GraphQl\Model\Query\ContextInterface;
use Magento\QuoteGraphQl\Model\Cart\PlaceOrder as PlaceOrderModel;
use Magento\QuoteGraphQl\Model\Cart\PlaceOrderMutexInterface;
use Magento\Sales\Api\OrderRepositoryInterface;

/**
Expand Down Expand Up @@ -45,30 +43,22 @@ class PlaceOrder implements ResolverInterface
*/
private $errorMessageFormatter;

/**
* @var PlaceOrderMutexInterface
*/
private $placeOrderMutex;

/**
* @param GetCartForCheckout $getCartForCheckout
* @param PlaceOrderModel $placeOrder
* @param OrderRepositoryInterface $orderRepository
* @param AggregateExceptionMessageFormatter $errorMessageFormatter
* @param PlaceOrderMutexInterface|null $placeOrderMutex
*/
public function __construct(
GetCartForCheckout $getCartForCheckout,
PlaceOrderModel $placeOrder,
OrderRepositoryInterface $orderRepository,
AggregateExceptionMessageFormatter $errorMessageFormatter,
?PlaceOrderMutexInterface $placeOrderMutex = null
AggregateExceptionMessageFormatter $errorMessageFormatter
) {
$this->getCartForCheckout = $getCartForCheckout;
$this->placeOrder = $placeOrder;
$this->orderRepository = $orderRepository;
$this->errorMessageFormatter = $errorMessageFormatter;
$this->placeOrderMutex = $placeOrderMutex ?: ObjectManager::getInstance()->get(PlaceOrderMutexInterface::class);
}

/**
Expand All @@ -80,25 +70,6 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
}

return $this->placeOrderMutex->execute(
$args['input']['cart_id'],
\Closure::fromCallable([$this, 'run']),
[$field, $context, $info, $args]
);
}

/**
* Run the resolver.
*
* @param Field $field
* @param ContextInterface $context
* @param ResolveInfo $info
* @param array|null $args
* @return array[]
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
*/
private function run(Field $field, ContextInterface $context, ResolveInfo $info, ?array $args): array
{
$maskedCartId = $args['input']['cart_id'];
$userId = (int)$context->getUserId();
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
Expand Down
1 change: 0 additions & 1 deletion app/code/Magento/QuoteGraphQl/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<preference for="Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValueInterface" type="Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue\Composite" />
<preference for="Magento\QuoteGraphQl\Model\CartItem\DataProvider\Processor\ItemDataProcessorInterface" type="Magento\QuoteGraphQl\Model\CartItem\DataProvider\Processor\ItemDataCompositeProcessor" />
<preference for="Magento\QuoteGraphQl\Model\CartItem\PrecursorInterface" type="Magento\QuoteGraphQl\Model\CartItem\PrecursorComposite" />
<preference for="Magento\QuoteGraphQl\Model\Cart\PlaceOrderMutexInterface" type="Magento\QuoteGraphQl\Model\Cart\PlaceOrderMutex" />
<type name="Magento\QuoteGraphQl\Model\Resolver\CartItemTypeResolver">
<arguments>
<argument name="supportedTypes" xsi:type="array">
Expand Down

This file was deleted.

0 comments on commit 3cc15fb

Please sign in to comment.