mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-09-26 03:09:11 +02:00
SyncRule: introduce purge strategies
This commit is contained in:
parent
39cf0a5fb8
commit
9387b92da1
@ -0,0 +1,73 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Icinga\Module\Director\Import\PurgeStrategy;
|
||||||
|
|
||||||
|
use Icinga\Module\Director\Objects\ImportRun;
|
||||||
|
use Icinga\Module\Director\Objects\ImportSource;
|
||||||
|
|
||||||
|
class ImportRunBasedPurgeStrategy extends PurgeStrategy
|
||||||
|
{
|
||||||
|
public function listObjectsToPurge()
|
||||||
|
{
|
||||||
|
$remove = array();
|
||||||
|
|
||||||
|
foreach ($this->getSyncRule()->fetchInvolvedImportSources() as $source) {
|
||||||
|
$remove += $this->checkImportSource($source);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $remove;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getLastSync()
|
||||||
|
{
|
||||||
|
return strtotime($this->getSyncRule()->getLastSyncTimestamp());
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: NAMING!
|
||||||
|
protected function checkImportSource(ImportSource $source)
|
||||||
|
{
|
||||||
|
if (null === ($lastSync = $this->getLastSync())) {
|
||||||
|
// No last sync, nothing to purge
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
|
||||||
|
$runA = $source->fetchLastRunBefore($lastSync);
|
||||||
|
if ($runA === null) {
|
||||||
|
// Nothing to purge for this source
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
|
||||||
|
$runB = $source->fetchLastRun();
|
||||||
|
if ($runA->rowset_checksum === $runB->rowset_checksum) {
|
||||||
|
// Same source data, nothing to purge
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->listKeysRemovedBetween($runA, $runB);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function listKeysRemovedBetween(ImportRun $runA, ImportRun $runB)
|
||||||
|
{
|
||||||
|
$db = $this->getSyncRule()->getDb();
|
||||||
|
|
||||||
|
$selectA = $runA->prepareImportedObjectQuery();
|
||||||
|
$selectB = $runB->prepareImportedObjectQuery();
|
||||||
|
|
||||||
|
$query = $db->select()->from(
|
||||||
|
array('a' => $selectA),
|
||||||
|
'a.object_name'
|
||||||
|
)->joinLeft(
|
||||||
|
array('b' => $selectB),
|
||||||
|
'a.object_name = b.object_name',
|
||||||
|
array()
|
||||||
|
)->where('b.object_name IS NULL');
|
||||||
|
|
||||||
|
$result = $db->fetchCol($query);
|
||||||
|
|
||||||
|
if ($result) {
|
||||||
|
return array_combine($result, $result);
|
||||||
|
} else {
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Icinga\Module\Director\Import\PurgeStrategy;
|
||||||
|
|
||||||
|
class PurgeNothingPurgeStrategy extends PurgeStrategy
|
||||||
|
{
|
||||||
|
public function listObjectsToPurge()
|
||||||
|
{
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
}
|
31
library/Director/Import/PurgeStrategy/PurgeStrategy.php
Normal file
31
library/Director/Import/PurgeStrategy/PurgeStrategy.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Icinga\Module\Director\Import\PurgeStrategy;
|
||||||
|
|
||||||
|
use Icinga\Module\Director\Objects\SyncRule;
|
||||||
|
|
||||||
|
abstract class PurgeStrategy
|
||||||
|
{
|
||||||
|
private $rule;
|
||||||
|
|
||||||
|
public function __construct(SyncRule $rule)
|
||||||
|
{
|
||||||
|
$this->rule = $rule;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getSyncRule()
|
||||||
|
{
|
||||||
|
return $this->rule;
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract function listObjectsToPurge();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return PurgeStrategy
|
||||||
|
*/
|
||||||
|
public static function load($name, SyncRule $rule)
|
||||||
|
{
|
||||||
|
$class = __NAMESPACE__ . '\\' . $name . 'PurgeStrategy';
|
||||||
|
return new $class($rule);
|
||||||
|
}
|
||||||
|
}
|
@ -5,6 +5,7 @@ namespace Icinga\Module\Director\Objects;
|
|||||||
use Icinga\Application\Benchmark;
|
use Icinga\Application\Benchmark;
|
||||||
use Icinga\Data\Filter\Filter;
|
use Icinga\Data\Filter\Filter;
|
||||||
use Icinga\Module\Director\Data\Db\DbObject;
|
use Icinga\Module\Director\Data\Db\DbObject;
|
||||||
|
use Icinga\Module\Director\Import\PurgeStrategy\PurgeStrategy;
|
||||||
use Icinga\Module\Director\Import\Sync;
|
use Icinga\Module\Director\Import\Sync;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
||||||
@ -30,6 +31,8 @@ class SyncRule extends DbObject
|
|||||||
|
|
||||||
private $sync;
|
private $sync;
|
||||||
|
|
||||||
|
private $purgeStrategy;
|
||||||
|
|
||||||
private $currentSyncRunId;
|
private $currentSyncRunId;
|
||||||
|
|
||||||
private $filter;
|
private $filter;
|
||||||
@ -52,6 +55,17 @@ class SyncRule extends DbObject
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function fetchInvolvedImportSources()
|
||||||
|
{
|
||||||
|
$sources = array();
|
||||||
|
|
||||||
|
foreach ($this->listInvolvedSourceIds() as $sourceId) {
|
||||||
|
$sources[$sourceId] = ImportSource::load($sourceId, $this->getConnection());
|
||||||
|
}
|
||||||
|
|
||||||
|
return $sources;
|
||||||
|
}
|
||||||
|
|
||||||
public function getLastSyncTimestamp()
|
public function getLastSyncTimestamp()
|
||||||
{
|
{
|
||||||
if (! $this->hasBeenLoadedFromDb()) {
|
if (! $this->hasBeenLoadedFromDb()) {
|
||||||
@ -117,6 +131,8 @@ class SyncRule extends DbObject
|
|||||||
|
|
||||||
Benchmark::measure('Checking sync rule ' . $this->rule_name);
|
Benchmark::measure('Checking sync rule ' . $this->rule_name);
|
||||||
try {
|
try {
|
||||||
|
$this->last_attempt = date('Y-m-d H:i:s');
|
||||||
|
$this->sync_state = 'unknown';
|
||||||
$sync = $this->sync();
|
$sync = $this->sync();
|
||||||
if ($sync->hasModifications()) {
|
if ($sync->hasModifications()) {
|
||||||
Benchmark::measure('Got modifications for sync rule ' . $this->rule_name);
|
Benchmark::measure('Got modifications for sync rule ' . $this->rule_name);
|
||||||
@ -136,6 +152,7 @@ class SyncRule extends DbObject
|
|||||||
|
|
||||||
$this->last_error_message = null;
|
$this->last_error_message = null;
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
|
throw $e;
|
||||||
$this->sync_state = 'failing';
|
$this->sync_state = 'failing';
|
||||||
$this->last_error_message = $e->getMessage();
|
$this->last_error_message = $e->getMessage();
|
||||||
}
|
}
|
||||||
@ -175,6 +192,25 @@ class SyncRule extends DbObject
|
|||||||
return $this->filter;
|
return $this->filter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function purgeStrategy()
|
||||||
|
{
|
||||||
|
if ($this->purgeStrategy === null) {
|
||||||
|
$this->purgeStrategy = $this->loadConfiguredPurgeStrategy();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->purgeStrategy;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Allow for more
|
||||||
|
protected function loadConfiguredPurgeStrategy()
|
||||||
|
{
|
||||||
|
if ($this->purge_existing) {
|
||||||
|
return PurgeStrategy::load('ImportRunBased', $this);
|
||||||
|
} else {
|
||||||
|
return PurgeStrategy::load('PurgeNothing', $this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function fetchSyncProperties()
|
public function fetchSyncProperties()
|
||||||
{
|
{
|
||||||
$db = $this->getDb();
|
$db = $this->getDb();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user