11
11
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
12
12
use Magento \Framework \GraphQl \Query \ResolverInterface ;
13
13
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
14
+ use Magento \Framework \Lock \LockManagerInterface ;
14
15
use Magento \GraphQl \Model \Query \ContextInterface ;
15
16
use Magento \Quote \Model \Cart \AddProductsToCart as AddProductsToCartService ;
16
17
use Magento \Quote \Model \Cart \Data \AddProductsToCartOutput ;
@@ -41,19 +42,27 @@ class AddProductsToCart implements ResolverInterface
41
42
*/
42
43
private $ itemDataProcessor ;
43
44
45
+ /**
46
+ * @var LockManagerInterface
47
+ */
48
+ private $ lockManager ;
49
+
44
50
/**
45
51
* @param GetCartForUser $getCartForUser
46
52
* @param AddProductsToCartService $addProductsToCart
47
- * @param ItemDataProcessorInterface $itemDataProcessor
53
+ * @param ItemDataProcessorInterface $itemDataProcessor
54
+ * @param LockManagerInterface $lockManager
48
55
*/
49
56
public function __construct (
50
57
GetCartForUser $ getCartForUser ,
51
58
AddProductsToCartService $ addProductsToCart ,
52
- ItemDataProcessorInterface $ itemDataProcessor
59
+ ItemDataProcessorInterface $ itemDataProcessor ,
60
+ LockManagerInterface $ lockManager
53
61
) {
54
62
$ this ->getCartForUser = $ getCartForUser ;
55
63
$ this ->addProductsToCartService = $ addProductsToCart ;
56
64
$ this ->itemDataProcessor = $ itemDataProcessor ;
65
+ $ this ->lockManager = $ lockManager ;
57
66
}
58
67
59
68
/**
@@ -72,6 +81,12 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
72
81
$ maskedCartId = $ args ['cartId ' ];
73
82
$ cartItemsData = $ args ['cartItems ' ];
74
83
$ storeId = (int )$ context ->getExtensionAttributes ()->getStore ()->getId ();
84
+ $ lockName = 'cart_processing_lock_ ' . $ maskedCartId ;
85
+ while ($ this ->lockManager ->isLocked ($ lockName )) {
86
+ // wait till other process working with the same cart complete
87
+ usleep (rand (100 , 600 ));
88
+ }
89
+ $ this ->lockManager ->lock ($ lockName , 1 );
75
90
76
91
// Shopping Cart validation
77
92
$ this ->getCartForUser ->execute ($ maskedCartId , $ context ->getUserId (), $ storeId );
@@ -86,6 +101,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
86
101
87
102
/** @var AddProductsToCartOutput $addProductsToCartOutput */
88
103
$ addProductsToCartOutput = $ this ->addProductsToCartService ->execute ($ maskedCartId , $ cartItems );
104
+ $ this ->lockManager ->unlock ($ lockName );
89
105
90
106
return [
91
107
'cart ' => [
0 commit comments