Class: ModSpox::PluginManager

Inherits:
Object
  • Object
show all
Defined in:
lib/mod_spox/PluginManager.rb

Defined Under Namespace

Classes: PluginFileNotFound, PluginMissing

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#pluginsObject (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_pluginsObject

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(message)
    @pipeline << Messages::Internal::QueueSocket.new
    begin
        path = !message.name ? "#{BotConfig[:userpluginpath]}/#{message.path.gsub(/^.+\//, '')}" : "#{BotConfig[:userpluginpath]}/#{message.name}"
        begin
            File.symlink(message.path, path)
        rescue NotImplementedError => boom
            FileUtils.copy(message.path, path)
        end
        do_load(path)
        @pipeline << Messages::Internal::PluginLoadResponse.new(message.requester, true)
        Logger.info("Loaded new plugin: #{message.path}")
    rescue Object => boom
        Logger.warn("Failed to load plugin: #{message.path} Reason: #{boom}")
        @pipeline << Messages::Internal::PluginLoadResponse.new(message.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(message)
    if(@plugins.has_key?(message.plugin))
        response = Messages::Internal::PluginResponse.new(message.requester, @plugins[message.plugin])
    else
        response = Messages::Internal::PluginResponse.new(message.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(message=nil)
    @pipeline << Messages::Internal::QueueSocket.new
    begin
        @plugin_lock.synchronize do
            if(!message.nil? && (message.fresh && message.stale))
                do_unload(message.stale)
                FileUtils.remove_file(message.stale)
                FileUtils.copy(message.fresh, BotConfig[:userpluginpath])
                do_load(message.stale)
                Logger.info("Completed reload of plugin: #{message.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(message)
    @pipeline << Messages::Internal::PluginModuleResponse.new(message.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(message)
    @pipeline << Messages::Internal::QueueSocket.new
    begin
        do_unload(message.path)
        unless(File.symlink?(message.path))
            unless(message.name.nil?)
                FileUtils.copy(message.path, "#{BotConfig[:userpluginpath]}/#{message.name}")
            end
        end
        File.delete(message.path)
        @pipeline << Messages::Internal::PluginUnloadResponse.new(message.requester, true)
        Logger.info("Unloaded plugin: #{message.path}")
    rescue Object => boom
        Logger.warn("Failed to unload plugin: #{message.path} Reason: #{boom}")
        @pipeline << Messages::Internal::PluginUnloadResponse.new(message.requester, false)
    ensure
        @pipeline << Messages::Internal::UnqueueSocket.new
        @pipeline << Messages::Internal::SignaturesUpdate.new
    end
end

#upgrade_pluginsObject



122
123
124
# File 'lib/mod_spox/PluginManager.rb', line 122

def upgrade_plugins
    @plugins[:PluginLoader].plugin.extras_upgrade
end