From d8a5e9a636a9a5daff2a7dec57976e4ff1bb3f5e Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Wed, 2 Dec 2015 04:02:45 +0100 Subject: [PATCH] cli: add generic jobs command, remove import --- application/clicommands/ImportCommand.php | 42 ----------- application/clicommands/JobsCommand.php | 88 +++++++++++++++++++++++ 2 files changed, 88 insertions(+), 42 deletions(-) delete mode 100644 application/clicommands/ImportCommand.php create mode 100644 application/clicommands/JobsCommand.php diff --git a/application/clicommands/ImportCommand.php b/application/clicommands/ImportCommand.php deleted file mode 100644 index 9da9a297..00000000 --- a/application/clicommands/ImportCommand.php +++ /dev/null @@ -1,42 +0,0 @@ -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; - } - -} - diff --git a/application/clicommands/JobsCommand.php b/application/clicommands/JobsCommand.php new file mode 100644 index 00000000..76040d15 --- /dev/null +++ b/application/clicommands/JobsCommand.php @@ -0,0 +1,88 @@ +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; + } +}