Skip to content

Commit

Permalink
Beta implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwanyoike committed Oct 25, 2016
1 parent 0b8d263 commit b0b2704
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
vendor
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
42 changes: 42 additions & 0 deletions Plugin/ProductRepositoryWithLockingPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace SnowIO\ProductSaveMutex\Plugin;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use SnowIO\Lock\Api\LockService;

class ProductRepositoryWithLockingPlugin
{
/** @var LockService */
private $lockService;

public function __construct(LockService $lockService)
{
$this->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";
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# magento2-product-save-mutex
magento2-product-save-mutex - A Magento 2 module that provides a mutex lock during product saves
23 changes: 23 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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\\": ""
}
}
}
6 changes: 6 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Api\ProductRepositoryInterface">
<plugin name="ProductRepositoryWithLockingPlugin" type="SnowIO\ProductSaveMutex\Plugin\ProductRepositoryWithLockingPlugin" disabled="false" />
</type>
</config>
3 changes: 3 additions & 0 deletions etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="SnowIO_ProductSaveMutex" setup_version="1.0.0"/>
</config>
6 changes: 6 additions & 0 deletions registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'SnowIO_ProductSaveMutex',
__DIR__
);

0 comments on commit b0b2704

Please sign in to comment.