cli: add generic jobs command, remove import

This commit is contained in:
Thomas Gelf 2015-12-02 04:02:45 +01:00
parent 3c07cdcf66
commit d8a5e9a636
2 changed files with 88 additions and 42 deletions

View File

@ -1,42 +0,0 @@
<?php
namespace Icinga\Module\Director\Clicommands;
use Icinga\Cli\Command;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\ImportSource;
use Icinga\Module\Director\Import\Import;
class ImportCommand extends Command
{
protected $db;
public function runAction()
{
$id = $this->params->shift();
$import = new Import(ImportSource::load($id, $this->db()));
if ($runId = $import->run()) {
echo "Triggered new import\n";
} else {
echo "Nothing changed\n";
}
}
protected function db()
{
if ($this->db === null) {
$this->app->setupZendAutoloader();
$resourceName = $this->Config()->get('db', 'resource');
if ($resourceName) {
$this->db = Db::fromResourceName($resourceName);
} else {
$this->fail('Director is not configured correctly');
}
}
return $this->db;
}
}

View File

@ -0,0 +1,88 @@
<?php
namespace Icinga\Module\Director\Clicommands;
use Icinga\Module\Director\Cli\Command;
use Icinga\Module\Director\Objects\ImportSource;
use Icinga\Module\Director\Objects\SyncRule;
use Icinga\Module\Director\Import\Import;
use Icinga\Module\Director\Import\Sync;
use Icinga\Application\Benchmark;
use Exception;
class JobsCommand extends Command
{
public function runAction()
{
if ($this->hasBeenDisabled()) {
return;
}
$this->runScheduledImports()
->runScheduledSyncs()
->syncCoreStages()
->runScheduledDeployments()
;
}
protected function hasBeenDisabled()
{
return false;
}
protected function runScheduledImports()
{
foreach (ImportSource::loadAll($this->db()) as $source) {
Benchmark::measure('Starting with import ' . $source->source_name);
try {
$import = new Import($source);
if ($import->providesChanges()) {
printf('Import "%s" provides changes, triggering run... ', $source->source_name);
Benchmark::measure('Found changes for ' . $source->source_name);
if ($import->run()) {
Benchmark::measure('Import succeeded for ' . $source->source_name);
print "SUCCEEDED\n";
}
}
} catch (Exception $e) {
echo $this->screen->colorize('ERROR: ' . $e->getMessage(), 'red') . "\n";
Benchmark::measure('FAILED');
}
}
return $this;
}
protected function runScheduledSyncs()
{
// TODO: import-triggered:
// foreach $rule->involvedImports() -> if changedsince -> ... syncChangedRows
foreach (SyncRule::loadAll($this->db) as $rule) {
Benchmark::measure('Checking sync rule ' . $rule->rule_name);
$mod = Sync::getExpectedModifications($rule);
if (count($mod) > 0) {
printf('Sync rule "%s" provides changes, triggering sync... ', $rule->rule_name);
Benchmark::measure('Got modifications for sync rule ' . $rule->rule_name);
if (Sync::run($rule)) {
Benchmark::measure('Successfully synced rule ' . $rule->rule_name);
print "SUCCEEDED\n";
}
}
}
return $this;
}
protected function syncCoreStages()
{
return $this;
}
protected function runScheduledDeployments()
{
return $this;
}
}