From 579f735b493f310fe5c66e59221fca92a3bfbdfc Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Mon, 12 Dec 2011 23:57:05 +0000 Subject: [PATCH] Add Sync plugins Add support for synchronising remote content before running the source dispatchers. First plugin adds Rsync support. --- .../DownloadDispatcher/Processor.class.php | 22 +++++++-- .../DownloadDispatcher/Sync/IPlugin.class.php | 16 +++++++ .../Sync/Plugin/Rsync.class.php | 48 +++++++++++++++++++ .../Sync/PluginFactory.class.php | 30 ++++++++++++ 4 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 source/lib/DownloadDispatcher/Sync/IPlugin.class.php create mode 100644 source/lib/DownloadDispatcher/Sync/Plugin/Rsync.class.php create mode 100644 source/lib/DownloadDispatcher/Sync/PluginFactory.class.php diff --git a/source/lib/DownloadDispatcher/Processor.class.php b/source/lib/DownloadDispatcher/Processor.class.php index c52313e..e7a766d 100644 --- a/source/lib/DownloadDispatcher/Processor.class.php +++ b/source/lib/DownloadDispatcher/Processor.class.php @@ -13,12 +13,28 @@ class DownloadDispatcher_Processor { $config = $main->config(); $log = $main->log(); - // Find the list of available plugins + // Find the list of available Sync plugins + $sync_plugins = $config->get('sync'); + foreach ($sync_plugins as $plugin_name) { + // Get a list of all the instances of this plugin to be used + $instances = $config->get("sync.{$plugin_name}"); + foreach ($instances as $instance) { + try { + $plugin = DownloadDispatcher_Sync_PluginFactory::create($plugin_name, $config, $log, $instance); + $plugin->run(); + + } catch(SihnonFramework_Exception_LogException $e) { + SihnonFramework_LogEntry::warning($log, $e->getMessage()); + } + } + } + + // Find the list of available source plugins DownloadDispatcher_Source_PluginFactory::scan(); - $plugins = DownloadDispatcher_Source_PluginFactory::getValidPlugins(); + $source_plugins = DownloadDispatcher_Source_PluginFactory::getValidPlugins(); $enabled_plugins = $config->get('sources'); - foreach ($plugins as $plugin_name) { + foreach ($source_plugins as $plugin_name) { if (in_array($plugin_name, $enabled_plugins)) { $plugin = DownloadDispatcher_Source_PluginFactory::create($plugin_name, $config, $log); $plugin->run(); diff --git a/source/lib/DownloadDispatcher/Sync/IPlugin.class.php b/source/lib/DownloadDispatcher/Sync/IPlugin.class.php new file mode 100644 index 0000000..b33918f --- /dev/null +++ b/source/lib/DownloadDispatcher/Sync/IPlugin.class.php @@ -0,0 +1,16 @@ + \ No newline at end of file diff --git a/source/lib/DownloadDispatcher/Sync/Plugin/Rsync.class.php b/source/lib/DownloadDispatcher/Sync/Plugin/Rsync.class.php new file mode 100644 index 0000000..e5e61b4 --- /dev/null +++ b/source/lib/DownloadDispatcher/Sync/Plugin/Rsync.class.php @@ -0,0 +1,48 @@ +config = $config; + $this->log = $log; + $this->instance = $instance; + + $this->options = $this->config->get("sync.Rsync.{$this->instance}.options"); + $this->source = $this->config->get("sync.Rsync.{$this->instance}.source"); + $this->destination = $this->config->get("sync.Rsync.{$this->instance}.destination"); + } + + public function run() { + DownloadDispatcher_LogEntry::debug($this->log, "Running Rsync synchroniser: '{$this->instance}'"); + + $command = "/usr/bin/rsync {$this->options} '{$this->source}' '{$this->destination}'"; + DownloadDispatcher_LogEntry::debug($this->log, "Running foreground task: {$command}"); + + DownloadDispatcher_ForegroundTask::execute($command, null, null, null, array($this, 'output'), null, $this->instance); + } + + public function output($identifier, $data) { + DownloadDispatcher_LogEntry::debug($this->log, "{$identifier}: {$data}"); + } +} + +?> \ No newline at end of file diff --git a/source/lib/DownloadDispatcher/Sync/PluginFactory.class.php b/source/lib/DownloadDispatcher/Sync/PluginFactory.class.php new file mode 100644 index 0000000..47c1ca2 --- /dev/null +++ b/source/lib/DownloadDispatcher/Sync/PluginFactory.class.php @@ -0,0 +1,30 @@ + 'DownloadDispatcher/Sync/Plugin/', + ); + + + public static function init() { + + } + + public static function create($plugin, SihnonFramework_Config $config, SihnonFramework_Log $log, $instance) { + self::ensureScanned(); + + if (! self::isValidPlugin($plugin)) { + throw new Sihnon_Exception_InvalidPluginName($plugin); + } + + $classname = self::classname($plugin); + + return call_user_func(array($classname, 'create'), $config, $log, $instance); + } + +} + +?> \ No newline at end of file