ImportExport: add fail-safe import, WIP
This commit is contained in:
parent
98099ad48a
commit
67763dc5a9
|
@ -2,10 +2,12 @@
|
|||
|
||||
namespace Icinga\Module\Director;
|
||||
|
||||
use Exception;
|
||||
use Icinga\Module\Director\Data\Db\DbConnection;
|
||||
use Icinga\Module\Director\Objects\IcingaEndpoint;
|
||||
use Icinga\Module\Director\Objects\IcingaObject;
|
||||
use Icinga\Exception\ConfigurationError;
|
||||
use RuntimeException;
|
||||
use Zend_Db_Expr;
|
||||
use Zend_Db_Select;
|
||||
|
||||
|
@ -26,6 +28,25 @@ class Db extends DbConnection
|
|||
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)
|
||||
{
|
||||
$db = $this->db();
|
||||
|
|
|
@ -100,4 +100,22 @@ class ImportExport
|
|||
|
||||
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();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue