Class: Legion::Extension::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extension/loader.rb

Overview

New magical extension loader

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(supervision, _extensions = Legion::Settings[:legion][:extensions]) ⇒ Loader

Returns a new instance of Loader.



8
9
10
11
# File 'lib/legion/extension/loader.rb', line 8

def initialize(supervision, _extensions = Legion::Settings[:legion][:extensions])
  @loaded_extensions = []
  @supervision = supervision
end

Instance Attribute Details

#loaded_extensionsObject (readonly)

Returns the value of attribute loaded_extensions.



7
8
9
# File 'lib/legion/extension/loader.rb', line 7

def loaded_extensions
  @loaded_extensions
end

Instance Method Details

#gem_load(name) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/legion/extension/loader.rb', line 81

def gem_load(name)
  search_name = 'lex-' + name.to_s
  Legion::Logging.unknown search_name
  gem_dir = Gem::Specification.find_by_name(search_name).gem_dir
  Legion::Logging.unknown gem_dir
  require "#{gem_dir}/lib/legion/extensions/#{name}"
  true
rescue LoadError => ex
  Legion::Logging.unknown ex.message
  Legion::Logging.unknown ex.backtrace
  Legion::Logging.warn "gem path:  #{gem_dir}/lib/legion/extensions/#{name}"
  false
end

#load_actor_pool(klass, name, size = 1) ⇒ Object



25
26
27
# File 'lib/legion/extension/loader.rb', line 25

def load_actor_pool(klass, name, size = 1)
  @supervision.supervision_group.pool(klass, as: name, size: size)
end

#load_extension(extension, values) ⇒ Object

rubocop:disable Metrics/AbcSize



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/legion/extension/loader.rb', line 40

def load_extension(extension, values) # rubocop:disable Metrics/AbcSize
  Legion::Logging.debug "Skipping #{extension} because it's disabled" unless values[:enabled]
  return false unless values[:enabled]

  unless gem_load(extension)
    Legion::Logging.warn "#{extension} failed to load gem path"
    return false
  end

  klass = Kernel.const_get(values[:class])
  klass.autobuild

  register_lex(extension, klass.lex_methods)

  klass.actors.each do |actor|
    load_actor_pool(actor[:class], actor[:group_name], 1)
  end
  true
rescue Sequel::DatabaseConnectionError => exception
  Legion::Logging.fatal("Legion::Extension #{extension} requires a database connection but failed")
  Legion::Logging.debug("Extension failed with #{exception.message}")
  Legion::Logging.debug("Backtrace: #{exception.backtrace}")
  false
rescue NameError => exception
  Legion::Logging.fatal("Legion::Extension #{extension} failed to load, moving on without it")
  Legion::Logging.warn("Extension failed with #{exception.message}")
  Legion::Logging.warn("Backtrace: #{exception.backtrace}")
  false
rescue LoadError => exception
  Legion::Logging.fatal("Legion::Extension #{extension} failed to load, moving on without it")
  Legion::Logging.warn("Extension failed with #{exception.message}")
  Legion::Logging.warn("Backtrace: #{exception.backtrace}")
  false
rescue StandardError => exception
  Legion::Logging.fatal("Legion::Extension #{extension} failed to load, moving on without it")
  Legion::Logging.fatal("#{extension} was caught by default")
  Legion::Logging.warn("Extension failed with #{exception.message}")
  Legion::Logging.warn("Backtrace: #{exception.backtrace}")
  false
end

#load_extensions(extensions = Legion::Settings[:legion]) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/legion/extension/loader.rb', line 13

def load_extensions(extensions = Legion::Settings[:legion][:extensions])
  extensions.each do |extension, values|
    Legion::Logging.debug "Skipping #{extension} because it's disabled" unless values[:enabled]
    next unless values[:enabled]

    result = load_extension(extension, values)
    Legion::Logging.info("#{extension} was loaded") if result
    Legion::Logging.warn("#{extension} failed to load") unless result
    @loaded_extensions.push(extension) if result
  end
end

#register_lex(_extension, lex_methods) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/legion/extension/loader.rb', line 29

def register_lex(_extension, lex_methods)
  lex_methods.each do |namespace|
    namespace[:class_methods].each do |class_method, _attrs|
      options = { namespace: {}, method: {} }
      options[:namespace][:queue] = namespace[:queue] unless namespace[:queue].nil?
      options[:namespace][:uri] = namespace[:uri] unless namespace[:uri].nil?
      Legion::Transport::Messages::LexRegister.new(namespace[:namespace], class_method, options).publish
    end
  end
end