From 0e57319c58838338b44cc24af1fd4cc68f85e19f Mon Sep 17 00:00:00 2001 From: Anna Kotiuk <79214317+annakotiuk@users.noreply.github.com> Date: Thu, 3 Oct 2024 10:41:32 +0200 Subject: [PATCH] FRW-9017 Added regeneration of oms process cache. (#11117) FRW-9017 Added regeneration of oms process cache. --- src/Spryker/Zed/Oms/Business/OmsFacade.php | 6 ++++-- .../Zed/Oms/Business/OmsFacadeInterface.php | 4 +++- .../Oms/Business/OrderStateMachine/Builder.php | 5 +++-- .../OrderStateMachine/BuilderInterface.php | 5 ++++- .../Oms/Business/OrderStateMachine/Finder.php | 6 ++++-- .../OrderStateMachine/FinderInterface.php | 4 +++- .../Console/ProcessCacheWarmUpConsole.php | 2 +- .../Business/OrderStateMachine/BuilderTest.php | 18 ++++++++++++++---- 8 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/Spryker/Zed/Oms/Business/OmsFacade.php b/src/Spryker/Zed/Oms/Business/OmsFacade.php index a98c264c..027eea43 100644 --- a/src/Spryker/Zed/Oms/Business/OmsFacade.php +++ b/src/Spryker/Zed/Oms/Business/OmsFacade.php @@ -155,13 +155,15 @@ public function triggerEventForOneOrderItem($eventId, $orderItemId, array $data * * @api * + * @param bool $regenerateCache + * * @return array<\Spryker\Zed\Oms\Business\Process\ProcessInterface> */ - public function getProcesses() + public function getProcesses(bool $regenerateCache = false) { return $this->getFactory() ->createOrderStateMachineFinder() - ->getProcesses(); + ->getProcesses($regenerateCache); } /** diff --git a/src/Spryker/Zed/Oms/Business/OmsFacadeInterface.php b/src/Spryker/Zed/Oms/Business/OmsFacadeInterface.php index 456351f6..6688d8d2 100644 --- a/src/Spryker/Zed/Oms/Business/OmsFacadeInterface.php +++ b/src/Spryker/Zed/Oms/Business/OmsFacadeInterface.php @@ -166,9 +166,11 @@ public function triggerEventForOneOrderItem($eventId, $orderItemId, array $data * * @api * + * @param bool $regenerateCache + * * @return array<\Spryker\Zed\Oms\Business\Process\ProcessInterface> */ - public function getProcesses(); + public function getProcesses(bool $regenerateCache = false); /** * Specification: diff --git a/src/Spryker/Zed/Oms/Business/OrderStateMachine/Builder.php b/src/Spryker/Zed/Oms/Business/OrderStateMachine/Builder.php index 1828a61e..9155c446 100644 --- a/src/Spryker/Zed/Oms/Business/OrderStateMachine/Builder.php +++ b/src/Spryker/Zed/Oms/Business/OrderStateMachine/Builder.php @@ -112,10 +112,11 @@ public function __construct( /** * @param string $processName + * @param bool $regenerateCache * * @return \Spryker\Zed\Oms\Business\Process\ProcessInterface */ - public function createProcess($processName) + public function createProcess($processName, bool $regenerateCache = false): ProcessInterface { if (isset(static::$processBuffer[$processName])) { return static::$processBuffer[$processName]; @@ -135,7 +136,7 @@ public function createProcess($processName) $process = $this->processCacheReader->getProcess($processName); } - if (!$isProcessCached) { + if (!$isProcessCached || $regenerateCache) { $process = $this->createMainProcess($processName); $this->processCacheWriter->cacheProcess($process, $processName); } diff --git a/src/Spryker/Zed/Oms/Business/OrderStateMachine/BuilderInterface.php b/src/Spryker/Zed/Oms/Business/OrderStateMachine/BuilderInterface.php index 4e3495f1..5a11c28c 100644 --- a/src/Spryker/Zed/Oms/Business/OrderStateMachine/BuilderInterface.php +++ b/src/Spryker/Zed/Oms/Business/OrderStateMachine/BuilderInterface.php @@ -7,12 +7,15 @@ namespace Spryker\Zed\Oms\Business\OrderStateMachine; +use Spryker\Zed\Oms\Business\Process\ProcessInterface; + interface BuilderInterface { /** * @param string $processName + * @param bool $regenerateCache * * @return \Spryker\Zed\Oms\Business\Process\ProcessInterface */ - public function createProcess($processName); + public function createProcess($processName, bool $regenerateCache = false): ProcessInterface; } diff --git a/src/Spryker/Zed/Oms/Business/OrderStateMachine/Finder.php b/src/Spryker/Zed/Oms/Business/OrderStateMachine/Finder.php index cead2ab5..3572b252 100644 --- a/src/Spryker/Zed/Oms/Business/OrderStateMachine/Finder.php +++ b/src/Spryker/Zed/Oms/Business/OrderStateMachine/Finder.php @@ -257,14 +257,16 @@ public function getItemsWithoutFlag(SpySalesOrder $order, $flag) } /** + * @param bool $regenerateCache + * * @return array<\Spryker\Zed\Oms\Business\Process\ProcessInterface> */ - public function getProcesses() + public function getProcesses(bool $regenerateCache = false) { $processes = []; foreach ($this->activeProcesses as $processName) { $builder = clone $this->builder; - $processes[$processName] = $builder->createProcess($processName); + $processes[$processName] = $builder->createProcess($processName, $regenerateCache); } return $processes; diff --git a/src/Spryker/Zed/Oms/Business/OrderStateMachine/FinderInterface.php b/src/Spryker/Zed/Oms/Business/OrderStateMachine/FinderInterface.php index 89eb9a4c..b0273770 100644 --- a/src/Spryker/Zed/Oms/Business/OrderStateMachine/FinderInterface.php +++ b/src/Spryker/Zed/Oms/Business/OrderStateMachine/FinderInterface.php @@ -61,9 +61,11 @@ public function getItemsWithFlag(SpySalesOrder $order, $flag); public function getItemsWithoutFlag(SpySalesOrder $order, $flag); /** + * @param bool $regenerateCache + * * @return array<\Spryker\Zed\Oms\Business\Process\ProcessInterface> */ - public function getProcesses(); + public function getProcesses(bool $regenerateCache = false); /** * @param \Orm\Zed\Sales\Persistence\SpySalesOrderItem $orderItem diff --git a/src/Spryker/Zed/Oms/Communication/Console/ProcessCacheWarmUpConsole.php b/src/Spryker/Zed/Oms/Communication/Console/ProcessCacheWarmUpConsole.php index 38f8ba4d..c5101d59 100644 --- a/src/Spryker/Zed/Oms/Communication/Console/ProcessCacheWarmUpConsole.php +++ b/src/Spryker/Zed/Oms/Communication/Console/ProcessCacheWarmUpConsole.php @@ -49,7 +49,7 @@ protected function configure(): void */ protected function execute(InputInterface $input, OutputInterface $output): int { - $this->getFacade()->getProcesses(); + $this->getFacade()->getProcesses(true); return static::CODE_SUCCESS; } diff --git a/tests/SprykerTest/Zed/Oms/Business/OrderStateMachine/BuilderTest.php b/tests/SprykerTest/Zed/Oms/Business/OrderStateMachine/BuilderTest.php index c2ca8f40..9ad4f69b 100644 --- a/tests/SprykerTest/Zed/Oms/Business/OrderStateMachine/BuilderTest.php +++ b/tests/SprykerTest/Zed/Oms/Business/OrderStateMachine/BuilderTest.php @@ -182,13 +182,15 @@ public function testCreateProcessShouldReadFromCacheIfCacheIsEnabled( * @dataProvider omsProcessCachingWriterDataProvider * * @param bool $cacheIsEnabled - * @param int $expectedReaderCalls + * @param bool $regenerateCache + * @param int|bool $expectedReaderCalls * @param int $expectedWriterCalls * * @return void */ public function testCreateProcessShouldWriteToCacheIfCacheIsEnabled( bool $cacheIsEnabled, + bool $regenerateCache, int $expectedReaderCalls, int $expectedWriterCalls ): void { @@ -230,10 +232,10 @@ public function testCreateProcessShouldWriteToCacheIfCacheIsEnabled( // Act $this->tester->resetProcessBuffer(); - $builder->createProcess('process-a'); + $builder->createProcess('process-a', $regenerateCache); $this->tester->resetProcessBuffer(); - $builder->createProcess('process-a'); + $builder->createProcess('process-a', $regenerateCache); } /** @@ -242,13 +244,21 @@ public function testCreateProcessShouldWriteToCacheIfCacheIsEnabled( protected function omsProcessCachingWriterDataProvider(): array { return [ - 'test when cache is enabled' => [ + 'test when cache is enabled and should be regenerated' => [ + 'cacheIsEnabled' => true, + 'regenerateCache' => true, + 'expectedReaderCalls' => 2, + 'expectedWriterCalls' => 2, + ], + 'test when cache is enabled and should not be regenerated' => [ 'cacheIsEnabled' => true, + 'regenerateCache' => false, 'expectedReaderCalls' => 2, 'expectedWriterCalls' => 1, ], 'test when cache is disabled' => [ 'cacheIsEnabled' => false, + 'regenerateCache' => false, 'expectedReaderCalls' => 0, 'expectedWriterCalls' => 0, ],