ImportExport: add fail-safe import, WIP

This commit is contained in:
Thomas Gelf 2018-06-11 17:16:01 +02:00
parent 98099ad48a
commit 67763dc5a9
2 changed files with 39 additions and 0 deletions

View File

@ -2,10 +2,12 @@
namespace Icinga\Module\Director; namespace Icinga\Module\Director;
use Exception;
use Icinga\Module\Director\Data\Db\DbConnection; use Icinga\Module\Director\Data\Db\DbConnection;
use Icinga\Module\Director\Objects\IcingaEndpoint; use Icinga\Module\Director\Objects\IcingaEndpoint;
use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Exception\ConfigurationError; use Icinga\Exception\ConfigurationError;
use RuntimeException;
use Zend_Db_Expr; use Zend_Db_Expr;
use Zend_Db_Select; use Zend_Db_Select;
@ -26,6 +28,25 @@ class Db extends DbConnection
return $this->getDbAdapter(); return $this->getDbAdapter();
} }
public function runFailSafeTransaction($callable)
{
if (! is_callable($callable)) {
throw new RuntimeException(__METHOD__ . ' needs a Callable');
}
$db = $this->db();
$db->beginTransaction();
try {
$callable();
$db->commit();
} catch (Exception $e) {
$db->rollback();
throw $e;
}
return $this;
}
public function countActivitiesSinceLastDeployedConfig(IcingaObject $object = null) public function countActivitiesSinceLastDeployedConfig(IcingaObject $object = null)
{ {
$db = $this->db(); $db = $this->db();

View File

@ -100,4 +100,22 @@ class ImportExport
return $res; return $res;
} }
public function unserializeImportSources($objects)
{
$this->connection->runFailSafeTransaction(function () use ($objects) {
foreach ($objects as $object) {
ImportSource::import($object, $this->connection)->store();
}
});
}
public function unserializeSyncRules($objects)
{
$this->connection->runFailSafeTransaction(function () use ($objects) {
foreach ($objects as $object) {
SyncRule::import($object, $this->connection)->store();
}
});
}
} }