Class: ModSpox::PluginManager
- Inherits:
-
Object
- Object
- ModSpox::PluginManager
- Defined in:
- lib/mod_spox/PluginManager.rb
Defined Under Namespace
Classes: PluginFileNotFound, PluginMissing
Instance Attribute Summary collapse
-
#plugins ⇒ Object
readonly
Hash of plugins.
Instance Method Summary collapse
-
#destroy_plugins ⇒ Object
Destroys plugins.
-
#initialize(pipeline) ⇒ PluginManager
constructor
- pipeline
-
Pipeline for messages Create new PluginManager.
-
#load_plugin(message) ⇒ Object
- message
-
Messages::Internal::PluginLoadRequest Loads a plugin.
-
#plugin_request(message) ⇒ Object
- message
-
Messages::Internal::PluginRequest Returns a plugin to requesting object.
-
#reload_plugins(message = nil) ⇒ Object
- message
-
Messages::Internal::PluginReload Destroys and reinitializes plugins.
-
#send_modules(message) ⇒ Object
- message
-
Messages::Internal::PluginModuleRequest Sends the plugins module to the requester.
-
#unload_plugin(message) ⇒ Object
- message
-
Messages::Internal::PluginUnloadRequest Unloads a plugin.
- #upgrade_plugins ⇒ Object
Constructor Details
#initialize(pipeline) ⇒ PluginManager
- pipeline
-
Pipeline for messages
Create new PluginManager
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/mod_spox/PluginManager.rb', line 17 def initialize(pipeline) @plugins = Hash.new @pipeline = pipeline @pipeline.hook(self, :load_plugin, :Internal_PluginLoadRequest) @pipeline.hook(self, :unload_plugin, :Internal_PluginUnloadRequest) @pipeline.hook(self, :reload_plugins, :Internal_PluginReload) @pipeline.hook(self, :send_modules, :Internal_PluginModuleRequest) @pipeline.hook(self, :plugin_request, :Internal_PluginRequest) @plugins_module = Module.new @plugin_lock = Mutex.new load_plugins end |
Instance Attribute Details
#plugins ⇒ Object (readonly)
Hash of plugins. Defined by class name symbol (i.e. Trivia class: plugins)
13 14 15 |
# File 'lib/mod_spox/PluginManager.rb', line 13 def plugins @plugins end |
Instance Method Details
#destroy_plugins ⇒ Object
Destroys plugins
55 56 57 |
# File 'lib/mod_spox/PluginManager.rb', line 55 def destroy_plugins unload_plugins end |
#load_plugin(message) ⇒ Object
- message
-
Messages::Internal::PluginLoadRequest
Loads a plugin
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/mod_spox/PluginManager.rb', line 61 def load_plugin() @pipeline << Messages::Internal::QueueSocket.new begin path = !.name ? "#{BotConfig[:userpluginpath]}/#{.path.gsub(/^.+\//, '')}" : "#{BotConfig[:userpluginpath]}/#{.name}" begin File.symlink(.path, path) rescue NotImplementedError => boom FileUtils.copy(.path, path) end do_load(path) @pipeline << Messages::Internal::PluginLoadResponse.new(.requester, true) Logger.info("Loaded new plugin: #{.path}") rescue Object => boom Logger.warn("Failed to load plugin: #{.path} Reason: #{boom}") @pipeline << Messages::Internal::PluginLoadResponse.new(.requester, false) ensure @pipeline << Messages::Internal::SignaturesUpdate.new @pipeline << Messages::Internal::UnqueueSocket.new end end |
#plugin_request(message) ⇒ Object
- message
-
Messages::Internal::PluginRequest
Returns a plugin to requesting object
113 114 115 116 117 118 119 120 |
# File 'lib/mod_spox/PluginManager.rb', line 113 def plugin_request() if(@plugins.has_key?(.plugin)) response = Messages::Internal::PluginResponse.new(.requester, @plugins[.plugin]) else response = Messages::Internal::PluginResponse.new(.requester, nil) end @pipeline << response end |
#reload_plugins(message = nil) ⇒ Object
- message
-
Messages::Internal::PluginReload
Destroys and reinitializes plugins
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/mod_spox/PluginManager.rb', line 32 def reload_plugins(=nil) @pipeline << Messages::Internal::QueueSocket.new begin @plugin_lock.synchronize do if(!.nil? && (.fresh && .stale)) do_unload(.stale) FileUtils.remove_file(.stale) FileUtils.copy(.fresh, BotConfig[:userpluginpath]) do_load(.stale) Logger.info("Completed reload of plugin: #{.stale}") else unload_plugins load_plugins end end rescue Object => boom Logger.error("PluginManager caught error on plugin reload: #{boom}") ensure @pipeline << Messages::Internal::UnqueueSocket.new end end |
#send_modules(message) ⇒ Object
- message
-
Messages::Internal::PluginModuleRequest
Sends the plugins module to the requester
107 108 109 |
# File 'lib/mod_spox/PluginManager.rb', line 107 def send_modules() @pipeline << Messages::Internal::PluginModuleResponse.new(.requester, @plugins_module) end |
#unload_plugin(message) ⇒ Object
- message
-
Messages::Internal::PluginUnloadRequest
Unloads a plugin
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/mod_spox/PluginManager.rb', line 84 def unload_plugin() @pipeline << Messages::Internal::QueueSocket.new begin do_unload(.path) unless(File.symlink?(.path)) unless(.name.nil?) FileUtils.copy(.path, "#{BotConfig[:userpluginpath]}/#{.name}") end end File.delete(.path) @pipeline << Messages::Internal::PluginUnloadResponse.new(.requester, true) Logger.info("Unloaded plugin: #{.path}") rescue Object => boom Logger.warn("Failed to unload plugin: #{.path} Reason: #{boom}") @pipeline << Messages::Internal::PluginUnloadResponse.new(.requester, false) ensure @pipeline << Messages::Internal::UnqueueSocket.new @pipeline << Messages::Internal::SignaturesUpdate.new end end |
#upgrade_plugins ⇒ Object
122 123 124 |
# File 'lib/mod_spox/PluginManager.rb', line 122 def upgrade_plugins @plugins[:PluginLoader].plugin.extras_upgrade end |