Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduces caching for OMS processes to improve loading time #8

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"license": "proprietary",
"require": {
"php": ">=8.1",
"ext-simplexml": "*",
"spryker/acl-merchant-portal-extension": "^1.0.0",
"spryker/decimal-object": "^1.0.0",
"spryker/error-handler": "^2.1.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';
}
22 changes: 22 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 @@ -128,6 +132,8 @@ public function createOrderStateMachineBuilder()
$this->createProcessProcess(),
$this->getConfig()->getProcessDefinitionLocation(),
$this->getConfig()->getSubProcessPrefixDelimiter(),
$this->createProcessCacheReader(),
$this->createProcessCacheWriter(),
);
}

Expand Down Expand Up @@ -598,4 +604,20 @@ 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());
}
}
84 changes: 70 additions & 14 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,27 +60,37 @@ class Builder implements BuilderInterface
*/
protected $subProcessPrefixDelimiter;

protected ?ProcessCacheReaderInterface $processCacheReader;

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 string $subProcessPrefixDelimiter
* @param \Spryker\Zed\Oms\Business\Reader\ProcessCacheReaderInterface|null $processCacheReader
* @param \Spryker\Zed\Oms\Business\Writer\ProcessCacheWriterInterface|null $processCacheWriter
*/
public function __construct(
EventInterface $event,
StateInterface $state,
TransitionInterface $transition,
ProcessInterface $process,
$processDefinitionLocation,
$subProcessPrefixDelimiter = ' - '
$subProcessPrefixDelimiter = ' - ',
?ProcessCacheReaderInterface $processCacheReader = null,
?ProcessCacheWriterInterface $processCacheWriter = null
) {
$this->event = $event;
$this->state = $state;
$this->transition = $transition;
$this->process = $process;
$this->subProcessPrefixDelimiter = $subProcessPrefixDelimiter;
$this->processCacheReader = $processCacheReader;
$this->processCacheWriter = $processCacheWriter;

$this->setProcessDefinitionLocation($processDefinitionLocation);
}
Expand All @@ -90,28 +102,54 @@ 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 && $this->processCacheReader->hasProcess($processName)) {
static::$processBuffer[$processName] = $this->processCacheReader->getProcess($processName);

$this->mergeSubProcessFiles();
return static::$processBuffer[$processName];
}

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

[$processMap, $mainProcess] = $this->createSubProcess($processMap);
static::$processBuffer[$processName] = $mainProcess;

$stateToProcessMap = $this->createStates($processMap);
if ($this->processCacheWriter) {
$this->processCacheWriter->cacheProcess($mainProcess, $processName);
}

$this->createSubProcesses($processMap);
return static::$processBuffer[$processName];
}

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

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

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

return static::$processBuffer[$processName];
[$processMap, $mainProcess] = $this->createSubProcess($processMap);

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

$this->createSubProcesses($processMap);

$eventMap = $this->createEvents();

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

$this->precalculateProcess($mainProcess);

return $mainProcess;
}

/**
Expand Down Expand Up @@ -544,4 +582,22 @@ protected function createSubProcessPathPattern($fileName)
{
return '/\b' . preg_quote(dirname($fileName), '/') . '\b/';
}

/**
* @param \Spryker\Zed\Oms\Business\Process\ProcessInterface $process
*
* @return void
*/
protected function precalculateProcess(ProcessInterface $process): void
{
$allStates = $process->getAllStates();
foreach ($allStates as $state) {
$process->getStateFromAllProcesses($state->getName());
}
$process->getAllReservedStates();
$process->getAllTransitions();
$process->getAllTransitionsWithoutEvent();
$process->getManualEvents();
$process->getManualEventsBySource();
}
}
79 changes: 72 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>
*/
protected mixed $processStates = [];

/**
* @var array<\Spryker\Zed\Oms\Business\Process\StateInterface>
*/
protected mixed $allStates = [];

/**
* @var array<\Spryker\Zed\Oms\Business\Process\StateInterface>
*/
protected array $allReservedStates = [];

/**
* @var array<\Spryker\Zed\Oms\Business\Process\TransitionInterface>
*/
protected array $allTransitions = [];

/**
* @var array<\Spryker\Zed\Oms\Business\Process\TransitionInterface>
*/
protected array $allTransitionsWithoutEvent = [];

/**
* @var array<\Spryker\Zed\Oms\Business\Process\EventInterface>
*/
protected array $manualEvents = [];

/**
* @var array<string, array<string>>
*/
protected array $manualEventsBySource = [];

/**
* @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 (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) {
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) {
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) {
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) {
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) {
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) {
return $this->manualEventsBySource;
}

$events = $this->getManualEvents();

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

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

/**
Expand Down
Loading
Loading