From ec14bdff0db8ba496829d1e6d278b35761ca7d9d Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Mon, 18 Aug 2025 12:33:18 +0200 Subject: [PATCH] Ensure that backend states are applied when applying `ProcessChanges` --- application/controllers/ProcessController.php | 14 +++++----- library/Businessprocess/BpConfig.php | 27 +++++++++++++++++++ .../Businessprocess/State/IcingaDbState.php | 2 ++ .../Businessprocess/State/MonitoringState.php | 1 + library/Businessprocess/Web/Controller.php | 20 +++++++++++++- 5 files changed, 57 insertions(+), 7 deletions(-) diff --git a/application/controllers/ProcessController.php b/application/controllers/ProcessController.php index 406bce2..f5a2695 100644 --- a/application/controllers/ProcessController.php +++ b/application/controllers/ProcessController.php @@ -94,12 +94,14 @@ class ProcessController extends Controller $bp = $this->loadModifiedBpConfig(); $node = $this->getNode($bp); - if (Module::exists('icingadb') && - (! $bp->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend()) - ) { - IcingaDbState::apply($bp); - } else { - MonitoringState::apply($bp); + if (! $bp->statesApplied()) { + if (Module::exists('icingadb') && + (! $bp->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend()) + ) { + IcingaDbState::apply($bp); + } else { + MonitoringState::apply($bp); + } } $this->handleSimulations($bp); diff --git a/library/Businessprocess/BpConfig.php b/library/Businessprocess/BpConfig.php index c9e70fd..5df3fb6 100644 --- a/library/Businessprocess/BpConfig.php +++ b/library/Businessprocess/BpConfig.php @@ -133,6 +133,9 @@ class BpConfig /** @var bool Whether the config is faulty */ protected $isFaulty = false; + /** @var bool Whether the backend states are applied */ + protected bool $statesApplied = false; + public function __construct() { } @@ -151,6 +154,30 @@ class BpConfig return $this->metadata; } + /** + * Get whether the backend states are applied + * + * @return bool + */ + public function statesApplied(): bool + { + return $this->statesApplied; + } + + /** + * Set whether the backend states are applied + * + * @param bool $statesApplied + * + * @return $this + */ + public function setStatesApplied(bool $statesApplied = true): static + { + $this->statesApplied = $statesApplied; + + return $this; + } + /** * Set metadata * diff --git a/library/Businessprocess/State/IcingaDbState.php b/library/Businessprocess/State/IcingaDbState.php index 6de8caa..13d4b5b 100644 --- a/library/Businessprocess/State/IcingaDbState.php +++ b/library/Businessprocess/State/IcingaDbState.php @@ -146,6 +146,8 @@ class IcingaDbState Benchmark::measure('Retrieved states for ' . count($hostIds) . ' hosts in ' . $config->getName()); } + $config->setStatesApplied(); + Benchmark::measure('Got states for business process ' . $config->getName()); return $this; diff --git a/library/Businessprocess/State/MonitoringState.php b/library/Businessprocess/State/MonitoringState.php index b6a2391..d4c8fc0 100644 --- a/library/Businessprocess/State/MonitoringState.php +++ b/library/Businessprocess/State/MonitoringState.php @@ -107,6 +107,7 @@ class MonitoringState } } + $config->setStatesApplied(); // TODO: Union, single query? Benchmark::measure('Got states for business process ' . $config->getName()); diff --git a/library/Businessprocess/Web/Controller.php b/library/Businessprocess/Web/Controller.php index 43200cc..c3ea9ee 100644 --- a/library/Businessprocess/Web/Controller.php +++ b/library/Businessprocess/Web/Controller.php @@ -3,8 +3,12 @@ namespace Icinga\Module\Businessprocess\Web; use Icinga\Application\Icinga; +use Icinga\Application\Modules\Module; use Icinga\Module\Businessprocess\BpConfig; use Icinga\Module\Businessprocess\Modification\ProcessChanges; +use Icinga\Module\Businessprocess\ProvidedHook\Icingadb\IcingadbSupport; +use Icinga\Module\Businessprocess\State\IcingaDbState; +use Icinga\Module\Businessprocess\State\MonitoringState; use Icinga\Module\Businessprocess\Storage\LegacyStorage; use Icinga\Module\Businessprocess\Storage\Storage; use Icinga\Module\Businessprocess\Web\Component\ActionBar; @@ -196,7 +200,21 @@ class Controller extends CompatController $changes->clear(); $this->redirectNow($this->url()->without('dismissChanges')->without('unlocked')); } - $bp->applyChanges($changes); + + if (! $changes->isEmpty()) { + if (! $bp->statesApplied()) { + if (Module::exists('icingadb') && + (! $bp->hasBackendName() && IcingadbSupport::useIcingaDbAsBackend()) + ) { + IcingaDbState::apply($bp); + } else { + MonitoringState::apply($bp); + } + } + + $bp->applyChanges($changes); + } + return $bp; }