diff --git a/application/clicommands/HousekeepingCommand.php b/application/clicommands/HousekeepingCommand.php index 20ea1635..974e28db 100644 --- a/application/clicommands/HousekeepingCommand.php +++ b/application/clicommands/HousekeepingCommand.php @@ -63,41 +63,6 @@ class HousekeepingCommand extends Command } } - /** - * Check and repair membership cache - * - * Options: - * --type host Set the object type (Only host supported currently) - * --apply Actually update the database - */ - public function membershipsAction() - { - $type = $this->params->get('type', 'host'); - $apply = $this->params->shift('apply'); - - /** @var MembershipHousekeeping $class */ - $class = 'Icinga\\Module\\Director\\Db\\' . ucfirst($type) . 'MembershipHousekeeping'; - /** @var MembershipHousekeeping $helper */ - $helper = new $class($this->db()); - - printf("Checking %s memberships\n", $type); - - list($new, $outdated) = $helper->check(); - $newCount = count($new); - $outdatedCount = count($outdated); - $objects = $helper->getObjects(); - $groups = $helper->getGroups(); - - printf("%d objects - %d groups\n", count($objects), count($groups)); - - printf("Found %d new and %d outdated mappings\n", $newCount, $outdatedCount); - - if ($apply && ($newCount > 0 || $outdatedCount > 0)) { - $helper->update(); - printf("Update complete.\n"); - } - } - protected function housekeeping() { if ($this->housekeeping === null) { diff --git a/library/Director/Db/Housekeeping.php b/library/Director/Db/Housekeeping.php index 366fe976..8816cb2a 100644 --- a/library/Director/Db/Housekeeping.php +++ b/library/Director/Db/Housekeeping.php @@ -51,6 +51,7 @@ class Housekeeping 'unlinkedImportedRowSets' => N_('Unlinked imported row sets'), 'unlinkedImportedRows' => N_('Unlinked imported rows'), 'unlinkedImportedProperties' => N_('Unlinked imported properties'), + 'resolveCache' => N_('(Host) group resolve cache'), ); } @@ -189,4 +190,16 @@ class Housekeeping return $this->db->exec($sql); } + + public function countResolveCache() + { + $helper = MembershipHousekeeping::instance('host', $this->connection); + return array_sum($helper->check()); + } + + public function wipeResolveCache() + { + $helper = MembershipHousekeeping::instance('host', $this->connection); + return $helper->update(); + } } diff --git a/library/Director/Db/MembershipHousekeeping.php b/library/Director/Db/MembershipHousekeeping.php index 7f1ec010..4d1ae883 100644 --- a/library/Director/Db/MembershipHousekeeping.php +++ b/library/Director/Db/MembershipHousekeeping.php @@ -3,6 +3,7 @@ namespace Icinga\Module\Director\Db; use Icinga\Module\Director\Application\MemoryLimit; +use Icinga\Module\Director\Data\Db\DbConnection; use Icinga\Module\Director\Db; use Icinga\Module\Director\Db\Cache\PrefetchCache; use Icinga\Module\Director\Objects\GroupMembershipResolver; @@ -28,6 +29,8 @@ abstract class MembershipHousekeeping protected $prepared = false; + protected static $instances = []; + public function __construct(Db $connection) { $this->connection = $connection; @@ -37,6 +40,25 @@ abstract class MembershipHousekeeping } } + /** + * @param string $type + * @param DbConnection $connection + * + * @return static + */ + public static function instance($type, $connection) + { + if (! array_key_exists($type, self::$instances)) { + /** @var MembershipHousekeeping $class */ + $class = 'Icinga\\Module\\Director\\Db\\' . ucfirst($type) . 'MembershipHousekeeping'; + + /** @var MembershipHousekeeping $helper */ + self::$instances[$type] = new $class($connection); + } + + return self::$instances[$type]; + } + protected function prepare() { if ($this->prepared) { @@ -73,6 +95,8 @@ abstract class MembershipHousekeeping $this->prepare(); $this->resolver()->refreshDb(true); + + return true; } protected function prepareCache()