-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2088 from spryker/bugfix/919-lock-oms-state-machine
919 added lock for state machine triggers
- Loading branch information
Showing
20 changed files
with
989 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.