diff --git a/library/Director/Core/CoreApi.php b/library/Director/Core/CoreApi.php index ee32266e..d1d0668d 100644 --- a/library/Director/Core/CoreApi.php +++ b/library/Director/Core/CoreApi.php @@ -567,7 +567,7 @@ constants public function collectLogFiles(Db $db) { $existing = $this->listModuleStages('director'); - foreach ($db->getUncollectedDeployments() as $deployment) { + foreach (DirectorDeploymentLog::getUncollected($db) as $deployment) { $stage = $deployment->get('stage_name'); if (! in_array($stage, $existing)) { continue; @@ -603,7 +603,7 @@ constants public function wipeInactiveStages(Db $db) { - $uncollected = $db->getUncollectedDeployments(); + $uncollected = DirectorDeploymentLog::getUncollected($db); $moduleName = 'director'; foreach ($this->listModuleStages($moduleName, false) as $stage) { if (array_key_exists($stage, $uncollected)) { diff --git a/library/Director/Core/LegacyDeploymentApi.php b/library/Director/Core/LegacyDeploymentApi.php index 9dda5944..8134b62a 100644 --- a/library/Director/Core/LegacyDeploymentApi.php +++ b/library/Director/Core/LegacyDeploymentApi.php @@ -35,7 +35,7 @@ class LegacyDeploymentApi implements DeploymentApiInterface { $existing = $this->listModuleStages('director'); - foreach ($db->getUncollectedDeployments() as $deployment) { + foreach (DirectorDeploymentLog::getUncollected($db) as $deployment) { $stage = $deployment->get('stage_name'); if (! in_array($stage, $existing)) { continue; @@ -77,7 +77,7 @@ class LegacyDeploymentApi implements DeploymentApiInterface */ public function wipeInactiveStages(Db $db) { - $uncollected = $db->getUncollectedDeployments(); + $uncollected = DirectorDeploymentLog::getUncollected($db); $moduleName = 'director'; $currentStage = $this->getActiveStageName(); diff --git a/library/Director/Db.php b/library/Director/Db.php index 42f006db..914f2c40 100644 --- a/library/Director/Db.php +++ b/library/Director/Db.php @@ -672,33 +672,4 @@ class Db extends DbConnection return $db->fetchPairs($query); } - - /** - * @return DirectorDeploymentLog[] - */ - public function getUncollectedDeployments() - { - $db = $this->db(); - - $query = $db->select() - ->from('director_deployment_log') - ->where('stage_name IS NOT NULL') - ->where('stage_collected IS NULL') - ->where('startup_succeeded IS NULL') - ->order('stage_name'); - - return DirectorDeploymentLog::loadAll($this, $query, 'stage_name'); - } - - public function hasUncollectedDeployments() - { - $db = $this->db(); - $query = $db->select() - ->from('director_deployment_log', array('cnt' => 'COUNT(*)')) - ->where('stage_name IS NOT NULL') - ->where('stage_collected IS NULL') - ->where('startup_succeeded IS NULL'); - - return $db->fetchOne($query) > 0; - } } diff --git a/library/Director/Deployment/DeploymentInfo.php b/library/Director/Deployment/DeploymentInfo.php new file mode 100644 index 00000000..77d52de4 --- /dev/null +++ b/library/Director/Deployment/DeploymentInfo.php @@ -0,0 +1,59 @@ +db = $db; + } + + public function setObject(IcingaObject $object) + { + $this->object = $object; + return $this; + } + + public function getTotalChanges() + { + if ($this->totalChanges === null) { + $this->totalChanges = $this->db->countActivitiesSinceLastDeployedConfig(); + } + + return $this->totalChanges; + } + + public function getSingleObjectChanges() + { + if ($this->objectChanges === null) { + if ($this->object === null) { + $this->objectChanges = 0; + } else { + $this->objectChanges = $this->db + ->countActivitiesSinceLastDeployedConfig($this->object); + } + } + + return $this->objectChanges; + } + + public function hasUndeployedChanges() + { + return $this->getSingleObjectChanges() > 0 && $this->getTotalChanges() > 0; + } +} diff --git a/library/Director/Objects/DirectorDeploymentLog.php b/library/Director/Objects/DirectorDeploymentLog.php index 95791310..e5d1d097 100644 --- a/library/Director/Objects/DirectorDeploymentLog.php +++ b/library/Director/Objects/DirectorDeploymentLog.php @@ -44,7 +44,7 @@ class DirectorDeploymentLog extends DbObject public function getConfig() { if ($this->config === null) { - $this->config = IcingaConfig::load($this->config_checksum); + $this->config = IcingaConfig::load($this->config_checksum, $this->connection); } return $this->config; @@ -103,4 +103,32 @@ class DirectorDeploymentLog extends DbObject return static::load($db->fetchOne($query), $connection); } + + /** + * @return static[] + */ + public static function getUncollected(Db $connection) + { + $db = $connection->getDbAdapter(); + $query = $db->select() + ->from('director_deployment_log') + ->where('stage_name IS NOT NULL') + ->where('stage_collected IS NULL') + ->where('startup_succeeded IS NULL') + ->order('stage_name'); + + return static::loadAll($connection, $query, 'stage_name'); + } + + public static function hasUncollected(Db $connection) + { + $db = $connection->getDbAdapter(); + $query = $db->select() + ->from('director_deployment_log', ['cnt' => 'COUNT(*)']) + ->where('stage_name IS NOT NULL') + ->where('stage_collected IS NULL') + ->where('startup_succeeded IS NULL'); + + return $db->fetchOne($query) > 0; + } }