Skip to content

Commit 3cc15fb

Browse files
authored
Merge pull request #8488 from magento-lynx/LYNX-199
[LYNX] Place order lock process optimization
2 parents 0cdd86b + 1bb7a03 commit 3cc15fb

File tree

7 files changed

+6
-233
lines changed

7 files changed

+6
-233
lines changed

app/code/Magento/Quote/Model/QuoteManagement.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class QuoteManagement implements CartManagementInterface, ResetAfterRequestInter
5555
{
5656
private const LOCK_PREFIX = 'PLACE_ORDER_';
5757

58-
private const LOCK_TIMEOUT = 10;
58+
private const LOCK_TIMEOUT = 0;
5959

6060
/**
6161
* @var EventManager
@@ -615,13 +615,12 @@ protected function submitQuote(QuoteEntity $quote, $orderData = [])
615615
);
616616

617617
$lockedName = self::LOCK_PREFIX . $quote->getId();
618-
if ($this->lockManager->isLocked($lockedName)) {
618+
if (!$this->lockManager->lock($lockedName, self::LOCK_TIMEOUT)) {
619619
throw new LocalizedException(__(
620620
'A server error stopped your order from being placed. Please try to place your order again.'
621621
));
622622
}
623623
try {
624-
$this->lockManager->lock($lockedName, self::LOCK_TIMEOUT);
625624
$order = $this->orderManagement->place($order);
626625
$quote->setIsActive(false);
627626
$this->eventManager->dispatch(
@@ -632,7 +631,6 @@ protected function submitQuote(QuoteEntity $quote, $orderData = [])
632631
]
633632
);
634633
$this->quoteRepository->save($quote);
635-
$this->lockManager->unlock($lockedName);
636634
} catch (\Exception $e) {
637635
$this->lockManager->unlock($lockedName);
638636
$this->rollbackAddresses($quote, $order, $e);

app/code/Magento/Quote/Test/Unit/Model/QuoteManagementTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ public function testSubmit(): void
837837
['order' => $order, 'quote' => $quote]
838838
]
839839
);
840-
$this->lockManagerMock->method('isLocked')->willReturn(false);
840+
$this->lockManagerMock->method('lock')->willReturn(true);
841841
$this->quoteRepositoryMock->expects($this->once())->method('save')->with($quote);
842842
$this->assertEquals($order, $this->model->submit($quote, $orderData));
843843
}
@@ -1378,6 +1378,7 @@ public function testSubmitForCustomer(): void
13781378
['order' => $order, 'quote' => $quote]
13791379
]
13801380
);
1381+
$this->lockManagerMock->method('lock')->willReturn(true);
13811382
$this->quoteRepositoryMock->expects($this->once())->method('save')->with($quote);
13821383
$this->assertEquals($order, $this->model->submit($quote, $orderData));
13831384
}
@@ -1501,7 +1502,7 @@ public function testSubmitWithLockException(): void
15011502
['order' => $order, 'quote' => $quote]
15021503
]
15031504
);
1504-
$this->lockManagerMock->method('isLocked')->willReturn(true);
1505+
$this->lockManagerMock->method('lock')->willReturn(false);
15051506

15061507
$this->expectExceptionMessage(
15071508
'A server error stopped your order from being placed. Please try to place your order again.'

app/code/Magento/QuoteGraphQl/Model/Cart/PlaceOrderMutex.php

Lines changed: 0 additions & 72 deletions
This file was deleted.

app/code/Magento/QuoteGraphQl/Model/Cart/PlaceOrderMutexInterface.php

Lines changed: 0 additions & 27 deletions
This file was deleted.

app/code/Magento/QuoteGraphQl/Model/Resolver/PlaceOrder.php

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace Magento\QuoteGraphQl\Model\Resolver;
99

10-
use Magento\Framework\App\ObjectManager;
1110
use Magento\Framework\Exception\LocalizedException;
1211
use Magento\Framework\GraphQl\Config\Element\Field;
1312
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
@@ -17,7 +16,6 @@
1716
use Magento\QuoteGraphQl\Model\Cart\GetCartForCheckout;
1817
use Magento\GraphQl\Model\Query\ContextInterface;
1918
use Magento\QuoteGraphQl\Model\Cart\PlaceOrder as PlaceOrderModel;
20-
use Magento\QuoteGraphQl\Model\Cart\PlaceOrderMutexInterface;
2119
use Magento\Sales\Api\OrderRepositoryInterface;
2220

2321
/**
@@ -45,30 +43,22 @@ class PlaceOrder implements ResolverInterface
4543
*/
4644
private $errorMessageFormatter;
4745

48-
/**
49-
* @var PlaceOrderMutexInterface
50-
*/
51-
private $placeOrderMutex;
52-
5346
/**
5447
* @param GetCartForCheckout $getCartForCheckout
5548
* @param PlaceOrderModel $placeOrder
5649
* @param OrderRepositoryInterface $orderRepository
5750
* @param AggregateExceptionMessageFormatter $errorMessageFormatter
58-
* @param PlaceOrderMutexInterface|null $placeOrderMutex
5951
*/
6052
public function __construct(
6153
GetCartForCheckout $getCartForCheckout,
6254
PlaceOrderModel $placeOrder,
6355
OrderRepositoryInterface $orderRepository,
64-
AggregateExceptionMessageFormatter $errorMessageFormatter,
65-
?PlaceOrderMutexInterface $placeOrderMutex = null
56+
AggregateExceptionMessageFormatter $errorMessageFormatter
6657
) {
6758
$this->getCartForCheckout = $getCartForCheckout;
6859
$this->placeOrder = $placeOrder;
6960
$this->orderRepository = $orderRepository;
7061
$this->errorMessageFormatter = $errorMessageFormatter;
71-
$this->placeOrderMutex = $placeOrderMutex ?: ObjectManager::getInstance()->get(PlaceOrderMutexInterface::class);
7262
}
7363

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

83-
return $this->placeOrderMutex->execute(
84-
$args['input']['cart_id'],
85-
\Closure::fromCallable([$this, 'run']),
86-
[$field, $context, $info, $args]
87-
);
88-
}
89-
90-
/**
91-
* Run the resolver.
92-
*
93-
* @param Field $field
94-
* @param ContextInterface $context
95-
* @param ResolveInfo $info
96-
* @param array|null $args
97-
* @return array[]
98-
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
99-
*/
100-
private function run(Field $field, ContextInterface $context, ResolveInfo $info, ?array $args): array
101-
{
10273
$maskedCartId = $args['input']['cart_id'];
10374
$userId = (int)$context->getUserId();
10475
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();

app/code/Magento/QuoteGraphQl/etc/di.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
<preference for="Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValueInterface" type="Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue\Composite" />
1010
<preference for="Magento\QuoteGraphQl\Model\CartItem\DataProvider\Processor\ItemDataProcessorInterface" type="Magento\QuoteGraphQl\Model\CartItem\DataProvider\Processor\ItemDataCompositeProcessor" />
1111
<preference for="Magento\QuoteGraphQl\Model\CartItem\PrecursorInterface" type="Magento\QuoteGraphQl\Model\CartItem\PrecursorComposite" />
12-
<preference for="Magento\QuoteGraphQl\Model\Cart\PlaceOrderMutexInterface" type="Magento\QuoteGraphQl\Model\Cart\PlaceOrderMutex" />
1312
<type name="Magento\QuoteGraphQl\Model\Resolver\CartItemTypeResolver">
1413
<arguments>
1514
<argument name="supportedTypes" xsi:type="array">

dev/tests/api-functional/testsuite/Magento/QuoteGraphQl/Model/Cart/PlaceOrderMutexTest.php

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)