Skip to content

Commit

Permalink
FRW-8523 Introduced caching for OMS processes. (#10973)
Browse files Browse the repository at this point in the history
FRW-8523 Introduced caching for OMS processes.
  • Loading branch information
spryker-release-bot authored Jul 4, 2024
1 parent ef82733 commit 1298f8c
Show file tree
Hide file tree
Showing 18 changed files with 798 additions and 51 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Oms module",
"license": "proprietary",
"require": {
"ext-simplexml": "*",
"php": ">=8.1",
"spryker/acl-merchant-portal-extension": "^1.0.0",
"spryker/decimal-object": "^1.0.0",
Expand Down
10 changes: 10 additions & 0 deletions src/Spryker/Shared/Oms/OmsConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,14 @@ interface OmsConstants
* @var string
*/
public const ENABLE_OMS_TRANSITION_LOG = 'OMS:ENABLE_OMS_TRANSITION_LOG';

/**
* Specification:
* - Defines where to store cached processes.
*
* @api
*
* @var string
*/
public const PROCESS_CACHE_PATH = 'OMS:PROCESS_CACHE_PATH';
}
25 changes: 25 additions & 0 deletions src/Spryker/Zed/Oms/Business/OmsBusinessFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
use Spryker\Zed\Oms\Business\Process\Process;
use Spryker\Zed\Oms\Business\Process\State;
use Spryker\Zed\Oms\Business\Process\Transition;
use Spryker\Zed\Oms\Business\Reader\ProcessCacheReader;
use Spryker\Zed\Oms\Business\Reader\ProcessCacheReaderInterface;
use Spryker\Zed\Oms\Business\Reader\ReservationReader;
use Spryker\Zed\Oms\Business\Reader\ReservationReaderInterface;
use Spryker\Zed\Oms\Business\Reader\StateMachineReader;
Expand All @@ -54,6 +56,8 @@
use Spryker\Zed\Oms\Business\Util\TimeoutProcessorCollection;
use Spryker\Zed\Oms\Business\Util\TimeoutProcessorCollectionInterface;
use Spryker\Zed\Oms\Business\Util\TransitionLog;
use Spryker\Zed\Oms\Business\Writer\ProcessCacheWriter;
use Spryker\Zed\Oms\Business\Writer\ProcessCacheWriterInterface;
use Spryker\Zed\Oms\OmsDependencyProvider;

/**
Expand Down Expand Up @@ -127,6 +131,8 @@ public function createOrderStateMachineBuilder()
$this->createProcessTransition(),
$this->createProcessProcess(),
$this->getConfig()->getProcessDefinitionLocation(),
$this->createProcessCacheReader(),
$this->createProcessCacheWriter(),
$this->getConfig()->getSubProcessPrefixDelimiter(),
);
}
Expand Down Expand Up @@ -598,4 +604,23 @@ public function getOmsEventTriggeredListenerPlugins(): array
{
return $this->getProvidedDependency(OmsDependencyProvider::PLUGINS_OMS_EVENT_TRIGGERED_LISTENER);
}

/**
* @return \Spryker\Zed\Oms\Business\Reader\ProcessCacheReaderInterface
*/
public function createProcessCacheReader(): ProcessCacheReaderInterface
{
return new ProcessCacheReader($this->getConfig());
}

/**
* @return \Spryker\Zed\Oms\Business\Writer\ProcessCacheWriterInterface
*/
public function createProcessCacheWriter(): ProcessCacheWriterInterface
{
return new ProcessCacheWriter(
$this->getConfig(),
$this->createProcessCacheReader(),
);
}
}
66 changes: 53 additions & 13 deletions src/Spryker/Zed/Oms/Business/OrderStateMachine/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Spryker\Zed\Oms\Business\Process\ProcessInterface;
use Spryker\Zed\Oms\Business\Process\StateInterface;
use Spryker\Zed\Oms\Business\Process\TransitionInterface;
use Spryker\Zed\Oms\Business\Reader\ProcessCacheReaderInterface;
use Spryker\Zed\Oms\Business\Writer\ProcessCacheWriterInterface;
use Symfony\Component\Finder\Finder as SymfonyFinder;

class Builder implements BuilderInterface
Expand Down Expand Up @@ -58,12 +60,24 @@ class Builder implements BuilderInterface
*/
protected $subProcessPrefixDelimiter;

/**
* @var \Spryker\Zed\Oms\Business\Reader\ProcessCacheReaderInterface
*/
protected ProcessCacheReaderInterface $processCacheReader;

/**
* @var \Spryker\Zed\Oms\Business\Writer\ProcessCacheWriterInterface
*/
protected ProcessCacheWriterInterface $processCacheWriter;

/**
* @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 array|string $processDefinitionLocation
* @param \Spryker\Zed\Oms\Business\Reader\ProcessCacheReaderInterface $processCacheReader
* @param \Spryker\Zed\Oms\Business\Writer\ProcessCacheWriterInterface $processCacheWriter
* @param string $subProcessPrefixDelimiter
*/
public function __construct(
Expand All @@ -72,12 +86,16 @@ public function __construct(
TransitionInterface $transition,
ProcessInterface $process,
$processDefinitionLocation,
ProcessCacheReaderInterface $processCacheReader,
ProcessCacheWriterInterface $processCacheWriter,
$subProcessPrefixDelimiter = ' - '
) {
$this->event = $event;
$this->state = $state;
$this->transition = $transition;
$this->process = $process;
$this->processCacheReader = $processCacheReader;
$this->processCacheWriter = $processCacheWriter;
$this->subProcessPrefixDelimiter = $subProcessPrefixDelimiter;

$this->setProcessDefinitionLocation($processDefinitionLocation);
Expand All @@ -90,28 +108,50 @@ public function __construct(
*/
public function createProcess($processName)
{
if (!isset(static::$processBuffer[$processName])) {
$this->rootElement = $this->loadXmlFromProcessName($processName);
if (isset(static::$processBuffer[$processName])) {
return static::$processBuffer[$processName];
}

if ($this->processCacheReader->hasProcess($processName)) {
static::$processBuffer[$processName] = $this->processCacheReader->getProcess($processName);

return static::$processBuffer[$processName];
}

$mainProcess = $this->createMainProcess($processName);

static::$processBuffer[$processName] = $mainProcess;

$this->mergeSubProcessFiles();
$this->processCacheWriter->cacheProcess($mainProcess, $processName);

/** @var array<\Spryker\Zed\Oms\Business\Process\ProcessInterface> $processMap */
$processMap = [];
return static::$processBuffer[$processName];
}

/**
* @param string $processName
*
* @return \Spryker\Zed\Oms\Business\Process\ProcessInterface
*/
protected function createMainProcess(string $processName): ProcessInterface
{
$this->rootElement = $this->loadXmlFromProcessName($processName);

[$processMap, $mainProcess] = $this->createSubProcess($processMap);
$this->mergeSubProcessFiles();

$stateToProcessMap = $this->createStates($processMap);
/** @var array<\Spryker\Zed\Oms\Business\Process\ProcessInterface> $processMap */
$processMap = [];

$this->createSubProcesses($processMap);
[$processMap, $mainProcess] = $this->createSubProcess($processMap);

$eventMap = $this->createEvents();
$stateToProcessMap = $this->createStates($processMap);

$this->createTransitions($stateToProcessMap, $processMap, $eventMap);
$this->createSubProcesses($processMap);

static::$processBuffer[$processName] = $mainProcess;
}
$eventMap = $this->createEvents();

return static::$processBuffer[$processName];
$this->createTransitions($stateToProcessMap, $processMap, $eventMap);

return $mainProcess->warmupCache();
}

/**
Expand Down
97 changes: 90 additions & 7 deletions src/Spryker/Zed/Oms/Business/Process/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,41 @@ class Process implements ProcessInterface
*/
protected $subProcesses = [];

/**
* @var array<string, \Spryker\Zed\Oms\Business\Process\StateInterface>|null
*/
protected ?array $processStates = null;

/**
* @var array<\Spryker\Zed\Oms\Business\Process\StateInterface>|null
*/
protected ?array $allStates = null;

/**
* @var array<\Spryker\Zed\Oms\Business\Process\StateInterface>|null
*/
protected ?array $allReservedStates = null;

/**
* @var array<\Spryker\Zed\Oms\Business\Process\TransitionInterface>|null
*/
protected ?array $allTransitions = null;

/**
* @var array<\Spryker\Zed\Oms\Business\Process\TransitionInterface>|null
*/
protected ?array $allTransitionsWithoutEvent = null;

/**
* @var array<\Spryker\Zed\Oms\Business\Process\EventInterface>|null
*/
protected ?array $manualEvents = null;

/**
* @var array<string, array<string>>|null
*/
protected ?array $manualEventsBySource = null;

/**
* @param \Spryker\Zed\Oms\Business\Util\DrawerInterface $drawer
*/
Expand Down Expand Up @@ -188,10 +223,16 @@ public function hasState($stateId)
*/
public function getStateFromAllProcesses($stateId)
{
if ($this->processStates !== null && isset($this->processStates[$stateId])) {
return $this->processStates[$stateId];
}

$processes = $this->getAllProcesses();
foreach ($processes as $process) {
if ($process->hasState($stateId)) {
return $process->getState($stateId);
$this->processStates[$stateId] = $process->getState($stateId);

return $this->processStates[$stateId];
}
}

Expand Down Expand Up @@ -255,6 +296,10 @@ public function hasTransitions()
*/
public function getAllStates()
{
if ($this->allStates !== null) {
return $this->allStates;
}

$states = [];
if ($this->hasStates()) {
$states = $this->getStates();
Expand All @@ -267,14 +312,18 @@ public function getAllStates()
}
}

return $states;
return $this->allStates = $states;
}

/**
* @return array<\Spryker\Zed\Oms\Business\Process\StateInterface>
*/
public function getAllReservedStates()
{
if ($this->allReservedStates !== null) {
return $this->allReservedStates;
}

$reservedStates = [];
$states = $this->getAllStates();
foreach ($states as $state) {
Expand All @@ -283,14 +332,18 @@ public function getAllReservedStates()
}
}

return $reservedStates;
return $this->allReservedStates = $reservedStates;
}

/**
* @return array<\Spryker\Zed\Oms\Business\Process\TransitionInterface>
*/
public function getAllTransitions()
{
if ($this->allTransitions !== null) {
return $this->allTransitions;
}

$transitions = [];
if ($this->hasTransitions()) {
$transitions = $this->getTransitions();
Expand All @@ -301,14 +354,18 @@ public function getAllTransitions()
}
}

return $transitions;
return $this->allTransitions = $transitions;
}

/**
* @return array<\Spryker\Zed\Oms\Business\Process\TransitionInterface>
*/
public function getAllTransitionsWithoutEvent()
{
if ($this->allTransitionsWithoutEvent !== null) {
return $this->allTransitionsWithoutEvent;
}

$transitions = [];
$allTransitions = $this->getAllTransitions();
foreach ($allTransitions as $transition) {
Expand All @@ -317,7 +374,7 @@ public function getAllTransitionsWithoutEvent()
}
}

return $transitions;
return $this->allTransitionsWithoutEvent = $transitions;
}

/**
Expand All @@ -327,6 +384,10 @@ public function getAllTransitionsWithoutEvent()
*/
public function getManualEvents()
{
if ($this->manualEvents !== null) {
return $this->manualEvents;
}

$manuallyExecutableEventList = [];
$transitions = $this->getAllTransitions();
foreach ($transitions as $transition) {
Expand All @@ -338,14 +399,18 @@ public function getManualEvents()
}
}

return $manuallyExecutableEventList;
return $this->manualEvents = $manuallyExecutableEventList;
}

/**
* @return array<array<string>>
*/
public function getManualEventsBySource()
{
if ($this->manualEventsBySource !== null) {
return $this->manualEventsBySource;
}

$events = $this->getManualEvents();

$eventsBySource = [];
Expand All @@ -362,7 +427,7 @@ public function getManualEventsBySource()
}
}

return $eventsBySource;
return $this->manualEventsBySource = $eventsBySource;
}

/**
Expand Down Expand Up @@ -402,4 +467,22 @@ public function getFile()
{
return $this->file;
}

/**
* @return $this
*/
public function warmupCache()
{
$allStates = $this->getAllStates();
foreach ($allStates as $state) {
$this->getStateFromAllProcesses($state->getName());
}
$this->getAllReservedStates();
$this->getAllTransitions();
$this->getAllTransitionsWithoutEvent();
$this->getManualEvents();
$this->getManualEventsBySource();

return $this;
}
}
Loading

0 comments on commit 1298f8c

Please sign in to comment.