Integrate MembershipHousekeeping into Housekeeping

This commit is contained in:
Markus Frosch 2018-09-18 12:21:19 +02:00
parent a7ad2e7ad3
commit 4675a241a8
3 changed files with 37 additions and 35 deletions

View File

@ -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) {

View File

@ -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();
}
}

View File

@ -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()