diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3ce5adb --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +vendor diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c0c630b --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +Copyright (c) 2016 snow.io Limited (GB) + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/Plugin/ProductRepositoryWithLockingPlugin.php b/Plugin/ProductRepositoryWithLockingPlugin.php new file mode 100644 index 0000000..128fbe5 --- /dev/null +++ b/Plugin/ProductRepositoryWithLockingPlugin.php @@ -0,0 +1,42 @@ +lockService = $lockService; + } + + public function aroundSave( + ProductRepositoryInterface $productRepository, + callable $proceed, + ProductInterface $product, + $saveOptions = false + ) { + $lockName = $this->getLockName($product->getSku()); + + if (!$this->lockService->acquireLock($lockName, 0)) { + throw new \RuntimeException('A conflict occurred while saving the product. No changes were applied.'); + } + + try { + return $proceed($product, $saveOptions); + } finally { + $this->lockService->releaseLock($lockName); + } + } + + private function getLockName($sku) + { + return "product_save.$sku"; + } +} \ No newline at end of file diff --git a/README.md b/README.md index 5753b4a..389dc3c 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ # magento2-product-save-mutex +magento2-product-save-mutex - A Magento 2 module that provides a mutex lock during product saves \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..9abdf73 --- /dev/null +++ b/composer.json @@ -0,0 +1,23 @@ +{ + "name": "snowio/magento2-product-save-mutex", + "license": "MIT", + "description": "A Magento 2 module that provides a mutex lock during product saves", + "type": "magento2-module", + "authors": [ + { + "name": "Alexander Wanyoike", + "email": "amw@amp.co" + } + ], + "require": { + "php": ">=5.6", + "magento/module-catalog": "^100|^101", + "snowio/magento2-lock": "^1.0.0" + }, + "autoload": { + "files": [ "registration.php" ], + "psr-4": { + "SnowIO\\ProductSaveMutex\\": "" + } + } +} \ No newline at end of file diff --git a/etc/di.xml b/etc/di.xml new file mode 100644 index 0000000..ccf8576 --- /dev/null +++ b/etc/di.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/etc/module.xml b/etc/module.xml new file mode 100644 index 0000000..851349c --- /dev/null +++ b/etc/module.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/registration.php b/registration.php new file mode 100644 index 0000000..dc9554f --- /dev/null +++ b/registration.php @@ -0,0 +1,6 @@ +