Skip to content

Commit

Permalink
Merge pull request #2088 from spryker/bugfix/919-lock-oms-state-machine
Browse files Browse the repository at this point in the history
919 added lock for state machine triggers
  • Loading branch information
hhebbo committed Jun 8, 2016
2 parents 1e42567 + 6b8235b commit 42d5d95
Show file tree
Hide file tree
Showing 20 changed files with 989 additions and 20 deletions.
5 changes: 0 additions & 5 deletions src/Spryker/Shared/Oms/OmsConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ interface OmsConstants

const INITIAL_STATUS = 'new';

/**
* @deprecated Please use OmsConfig to get default process location
*/
const DEFAULT_PROCESS_LOCATION = '/config/Zed/oms';

const PROCESS_LOCATION = 'PROCESS_LOCATION';

const NAME_CREDIT_MEMO_REFERENCE = 'CreditMemoReference';
Expand Down
11 changes: 11 additions & 0 deletions src/Spryker/Zed/Oms/Business/Exception/LockException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace Spryker\Zed\Oms\Business\Exception;

class LockException extends \Exception
{
}
32 changes: 32 additions & 0 deletions src/Spryker/Zed/Oms/Business/Lock/LockerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace Spryker\Zed\Oms\Business\Lock;

interface LockerInterface
{

/**
* @param int $identifier
*
* @return bool
*/
public function acquire($identifier);

/**
* @param int $identifier
*
* @return void
*/
public function release($identifier);

/**
* @return void
*/
public function clearLocks();

}
109 changes: 109 additions & 0 deletions src/Spryker/Zed/Oms/Business/Lock/TriggerLocker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace Spryker\Zed\Oms\Business\Lock;

use Orm\Zed\Oms\Persistence\SpyOmsStateMachineLock;
use Propel\Runtime\Exception\PropelException;
use Spryker\Zed\Oms\Business\Exception\LockException;
use Spryker\Zed\Oms\OmsConfig;
use Spryker\Zed\Oms\Persistence\OmsQueryContainerInterface;

class TriggerLocker implements LockerInterface
{

/**
* @var \Spryker\Zed\Oms\Persistence\OmsQueryContainerInterface
*/
protected $queryContainer;

/**
* @var \Spryker\Zed\Oms\OmsConfig
*/
protected $omsConfig;

/**
* @param \Spryker\Zed\Oms\Persistence\OmsQueryContainerInterface $queryContainer
* @param \Spryker\Zed\Oms\OmsConfig $omsConfig
*/
public function __construct(
OmsQueryContainerInterface $queryContainer,
OmsConfig $omsConfig
) {
$this->queryContainer = $queryContainer;
$this->omsConfig = $omsConfig;
}

/**
* Attempts to save a lock entity, and if it fails due to unique identifier constraint (entity already locked) -
* throws a LockException
*
* @param int $identifier
*
* @throws \Spryker\Zed\Oms\Business\Exception\LockException
*
* @return bool
*/
public function acquire($identifier)
{
$stateMachineLockEntity = $this->createStateMachineLockEntity();

$stateMachineLockEntity->setIdentifier($identifier);
$stateMachineLockEntity->setExpires($this->createExpirationDate());
try {
$affectedRows = $stateMachineLockEntity->save();
} catch (PropelException $exception) {
throw new LockException('State machine trigger is locked.');
}

return $affectedRows > 0;
}

/**
* @param int $identifier
*
* @return void
*/
public function release($identifier)
{
$this->queryContainer
->queryLockItemsByIdentifier($identifier)
->delete();
}

/**
* @return void
*/
public function clearLocks()
{
$this->queryContainer
->queryLockedItemsByExpirationDate(new \DateTime('now'))
->delete();
}

/**
* @return \DateTime
*/
protected function createExpirationDate()
{
$dateInterval = \DateInterval::createFromDateString(
$this->omsConfig->getStateMachineLockerTimeoutInterval()
);
$expirationDate = new \DateTime();
$expirationDate->add($dateInterval);

return $expirationDate;
}

/**
* @return \Orm\Zed\Oms\Persistence\SpyOmsStateMachineLock
*/
protected function createStateMachineLockEntity()
{
return new SpyOmsStateMachineLock();
}

}
30 changes: 29 additions & 1 deletion src/Spryker/Zed/Oms/Business/OmsBusinessFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
namespace Spryker\Zed\Oms\Business;

use Spryker\Zed\Kernel\Business\AbstractBusinessFactory;
use Spryker\Zed\Oms\Business\Lock\TriggerLocker;
use Spryker\Zed\Oms\Business\OrderStateMachine\Builder;
use Spryker\Zed\Oms\Business\OrderStateMachine\Dummy;
use Spryker\Zed\Oms\Business\OrderStateMachine\Finder;
use Spryker\Zed\Oms\Business\OrderStateMachine\LockedOrderStateMachine;
use Spryker\Zed\Oms\Business\OrderStateMachine\OrderStateMachine;
use Spryker\Zed\Oms\Business\OrderStateMachine\PersistenceManager;
use Spryker\Zed\Oms\Business\OrderStateMachine\Timeout;
Expand Down Expand Up @@ -42,6 +44,8 @@ public function createUtilReadOnlyArrayObject(array $array = [])
}

/**
* @deprecated Please use createLockedOrderStateMachine() instead
*
* @param array $logContext
*
* @return \Spryker\Zed\Oms\Business\OrderStateMachine\OrderStateMachineInterface
Expand All @@ -60,7 +64,20 @@ public function createOrderStateMachine(array $logContext = [])
}

/**
* @deprecated Please use createOrderStateMachine() instead
* @param array $logContext
*
* @return \Spryker\Zed\Oms\Business\OrderStateMachine\LockedOrderStateMachine
*/
public function createLockedOrderStateMachine(array $logContext = [])
{
return new LockedOrderStateMachine(
$this->createOrderStateMachine($logContext),
$this->createTriggerLocker()
);
}

/**
* @deprecated Please use createLockedOrderStateMachine() instead
*
* @param array $logContext
*
Expand Down Expand Up @@ -204,4 +221,15 @@ public function createUtilOrderItemMatrix()
return new OrderItemMatrix($this->getQueryContainer(), $this->getConfig());
}

/**
* @return \Spryker\Zed\Oms\Business\Lock\TriggerLocker
*/
public function createTriggerLocker()
{
return new TriggerLocker(
$this->getQueryContainer(),
$this->getConfig()
);
}

}
26 changes: 18 additions & 8 deletions src/Spryker/Zed/Oms/Business/OmsFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function isOrderFlaggedAll($idOrder, $flag)
public function triggerEventForOrderItems($eventId, array $orderItemIds, array $data = [])
{
return $this->getFactory()
->createOrderStateMachine()
->createLockedOrderStateMachine()
->triggerEventForOrderItems($eventId, $orderItemIds, $data);
}

Expand All @@ -89,7 +89,7 @@ public function triggerEventForOrderItems($eventId, array $orderItemIds, array $
public function triggerEventForNewOrderItems(array $orderItemIds, array $data = [])
{
return $this->getFactory()
->createOrderStateMachine()
->createLockedOrderStateMachine()
->triggerEventForNewOrderItems($orderItemIds, $data);
}

Expand All @@ -105,7 +105,7 @@ public function triggerEventForNewOrderItems(array $orderItemIds, array $data =
public function triggerEventForOneOrderItem($eventId, $orderItemId, array $data = [])
{
return $this->getFactory()
->createOrderStateMachine()
->createLockedOrderStateMachine()
->triggerEventForOneOrderItem($eventId, $orderItemId, $data);
}

Expand Down Expand Up @@ -143,7 +143,7 @@ public function getProcessList()
public function checkConditions(array $logContext = [])
{
return $this->getFactory()
->createOrderStateMachine($logContext)
->createLockedOrderStateMachine($logContext)
->checkConditions();
}

Expand All @@ -157,7 +157,7 @@ public function checkConditions(array $logContext = [])
public function checkTimeouts(array $logContext = [])
{
$orderStateMachine = $this->getFactory()
->createOrderStateMachine($logContext);
->createLockedOrderStateMachine($logContext);

return $this->getFactory()
->createOrderStateMachineTimeout()
Expand Down Expand Up @@ -340,7 +340,7 @@ public function triggerEvent($eventId, ObjectCollection $orderItems, array $logC
$orderItemsArray = $orderItems->getData();

return $this->getFactory()
->createOrderStateMachine($logContext)
->createLockedOrderStateMachine($logContext)
->triggerEvent($eventId, $orderItemsArray, $data);
}

Expand All @@ -358,7 +358,7 @@ public function triggerEventForNewItem(ObjectCollection $orderItems, array $logC
$orderItemsArray = $orderItems->getData();

return $this->getFactory()
->createOrderStateMachine($logContext)
->createLockedOrderStateMachine($logContext)
->triggerEventForNewItem($orderItemsArray, $data);
}

Expand All @@ -377,7 +377,7 @@ public function triggerEventForOneItem($eventId, $orderItem, array $logContext,
$orderItemsArray = [$orderItem];

return $this->getFactory()
->createOrderStateMachine($logContext)
->createLockedOrderStateMachine($logContext)
->triggerEvent($eventId, $orderItemsArray, $data);
}

Expand Down Expand Up @@ -419,4 +419,14 @@ public function getDistinctManualEventsByIdSalesOrder($idSalesOrder)
->getDistinctManualEventsByIdSalesOrder($idSalesOrder);
}

/**
* @api
*
* @return void
*/
public function clearLocks()
{
$this->getFactory()->createTriggerLocker()->clearLocks();
}

}
7 changes: 7 additions & 0 deletions src/Spryker/Zed/Oms/Business/OmsFacadeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,11 @@ public function getOrderItemMatrix();
*/
public function getManualEventsByIdSalesOrder($idSalesOrder);

/**
* @api
*
* @return void
*/
public function clearLocks();

}
7 changes: 2 additions & 5 deletions src/Spryker/Zed/Oms/Business/OrderStateMachine/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,13 @@ class Builder implements BuilderInterface
protected $processDefinitionLocation;

/**
* @deprecated The optional argument `$processDefinitionLocation` will be mandatory in next major version.
* Define paths to your process definition in `OmsConfig::getProcessDefinitionLocation()`
*
* @param \Spryker\Zed\Oms\Business\Process\EventInterface $event
* @param \Spryker\Zed\Oms\Business\Process\StateInterface $state
* @param \Spryker\Zed\Oms\Business\Process\TransitionInterface $transition
* @param \Spryker\Zed\Oms\Business\Process\ProcessInterface $process
* @param string|array|null $processDefinitionLocation
* @param string|array $processDefinitionLocation
*/
public function __construct(EventInterface $event, StateInterface $state, TransitionInterface $transition, $process, $processDefinitionLocation = null)
public function __construct(EventInterface $event, StateInterface $state, TransitionInterface $transition, $process, $processDefinitionLocation)
{
$this->event = $event;
$this->state = $state;
Expand Down
Loading

0 comments on commit 42d5d95

Please sign in to comment.