2015-07-21 15:16:18 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Objects;
|
|
|
|
|
2016-04-22 15:31:02 +02:00
|
|
|
use Icinga\Application\Benchmark;
|
2016-02-17 21:23:24 +01:00
|
|
|
use Icinga\Module\Director\Data\Db\DbObjectWithSettings;
|
2016-04-22 15:31:02 +02:00
|
|
|
use Icinga\Module\Director\Import\Import;
|
|
|
|
use Exception;
|
2015-07-21 15:16:18 +02:00
|
|
|
|
2016-02-17 21:23:24 +01:00
|
|
|
class ImportSource extends DbObjectWithSettings
|
2015-07-21 15:16:18 +02:00
|
|
|
{
|
|
|
|
protected $table = 'import_source';
|
|
|
|
|
|
|
|
protected $keyName = 'id';
|
|
|
|
|
|
|
|
protected $autoincKeyName = 'id';
|
|
|
|
|
|
|
|
protected $defaultProperties = array(
|
2016-04-22 15:31:02 +02:00
|
|
|
'id' => null,
|
|
|
|
'source_name' => null,
|
|
|
|
'provider_class' => null,
|
|
|
|
'key_column' => null,
|
2016-05-02 10:44:12 +02:00
|
|
|
'import_state' => 'unknown',
|
2016-04-22 15:31:02 +02:00
|
|
|
'last_error_message' => null,
|
|
|
|
'last_attempt' => null,
|
2015-07-21 15:16:18 +02:00
|
|
|
);
|
|
|
|
|
2016-02-17 21:23:24 +01:00
|
|
|
protected $settingsTable = 'import_source_setting';
|
2015-07-21 15:16:18 +02:00
|
|
|
|
2016-02-17 21:23:24 +01:00
|
|
|
protected $settingsRemoteId = 'source_id';
|
2016-02-19 02:47:12 +01:00
|
|
|
|
2016-06-24 17:01:47 +02:00
|
|
|
public function fetchLastRun()
|
2016-06-24 11:48:54 +02:00
|
|
|
{
|
|
|
|
return $this->fetchLastRunBefore(time());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fetchLastRunBefore($timestamp)
|
|
|
|
{
|
|
|
|
if (! $this->hasBeenLoadedFromDb()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($timestamp === null) {
|
|
|
|
$timestamp = time();
|
|
|
|
}
|
|
|
|
|
|
|
|
$db = $this->getDb();
|
|
|
|
$query = $db->select()->from(
|
|
|
|
array('ir' => 'import_run'),
|
|
|
|
'ir.id'
|
|
|
|
)->where('ir.source_id = ?', $this->id)
|
2016-06-24 17:01:47 +02:00
|
|
|
->where('ir.start_time < ?', date('Y-m-d H:i:s', $timestamp))
|
|
|
|
->order('ir.start_time DESC')
|
2016-06-24 11:48:54 +02:00
|
|
|
->limit(1);
|
|
|
|
|
|
|
|
$runId = $db->fetchOne($query);
|
|
|
|
|
|
|
|
if ($runId) {
|
2016-06-24 17:01:47 +02:00
|
|
|
return ImportRun::load($runId, $this->getConnection());
|
2016-06-24 11:48:54 +02:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-19 02:47:12 +01:00
|
|
|
public function fetchRowModifiers()
|
|
|
|
{
|
|
|
|
$db = $this->getDb();
|
|
|
|
return ImportRowModifier::loadAll(
|
|
|
|
$this->getConnection(),
|
|
|
|
$db->select()
|
|
|
|
->from('import_row_modifier')
|
|
|
|
->where('source_id = ?', $this->id)
|
|
|
|
->order('priority DESC')
|
|
|
|
);
|
|
|
|
}
|
2016-04-22 15:31:02 +02:00
|
|
|
|
|
|
|
public function checkForChanges($runImport = false)
|
|
|
|
{
|
|
|
|
$hadChanges = false;
|
|
|
|
|
|
|
|
Benchmark::measure('Starting with import ' . $this->source_name);
|
|
|
|
try {
|
|
|
|
$import = new Import($this);
|
2016-06-26 16:00:43 +02:00
|
|
|
$this->last_attempt = date('Y-m-d H:i:s');
|
2016-04-22 15:31:02 +02:00
|
|
|
if ($import->providesChanges()) {
|
|
|
|
Benchmark::measure('Found changes for ' . $this->source_name);
|
2016-06-28 10:10:15 +02:00
|
|
|
$hadChanges = true;
|
2016-04-22 15:31:02 +02:00
|
|
|
$this->import_state = 'pending-changes';
|
|
|
|
|
|
|
|
if ($runImport && $import->run()) {
|
|
|
|
Benchmark::measure('Import succeeded for ' . $this->source_name);
|
|
|
|
$this->import_state = 'in-sync';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->import_state = 'in-sync';
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->last_error_message = null;
|
|
|
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->import_state = 'failing';
|
|
|
|
Benchmark::measure('Import failed for ' . $this->source_name);
|
2016-06-26 16:00:43 +02:00
|
|
|
$this->last_error_message = $e->getMessage();
|
2016-04-22 15:31:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->hasBeenModified()) {
|
|
|
|
$this->store();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $hadChanges;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function runImport()
|
|
|
|
{
|
|
|
|
return $this->checkForChanges(true);
|
|
|
|
}
|
2015-07-21 15:16:18 +02:00
|
|
|
}
|