Class: Legion::Service

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

Instance Method Summary collapse

Constructor Details

#initialize(transport: true, cache: true, data: true, supervision: true, extensions: true, crypt: true, log_level: 'info') ⇒ Service

rubocop:disable Metrics/ParameterLists



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/legion/service.rb', line 7

def initialize(transport: true, cache: true, data: true, supervision: true, extensions: true, crypt: true, log_level: 'info') # rubocop:disable Metrics/ParameterLists
  setup_logging(log_level: log_level)
  Legion::Logging.debug('Starting Legion::Service')
  setup_settings
  Legion::Logging.info("node name: #{Legion::Settings[:client][:name]}")

  if crypt
    require 'legion/crypt'
    Legion::Crypt.start
  end

  setup_transport if transport

  require 'legion/cache' if cache

  setup_data if data
  setup_supervision if supervision
  require 'legion/runner'
  load_extensions if extensions

  Legion::Crypt.cs if crypt
  Legion::Settings[:client][:ready] = true
end

Instance Method Details

#default_pathsObject

noinspection RubyArgCount



48
49
50
51
52
53
54
55
# File 'lib/legion/service.rb', line 48

def default_paths
  [
    '/etc/legionio',
    "#{ENV['home']}/legionio",
    '~/legionio',
    './settings'
  ]
end

#load_extensionsObject



125
126
127
128
# File 'lib/legion/service.rb', line 125

def load_extensions
  require 'legion/runner'
  Legion::Extensions.hook_extensions
end

#modulesObject



3
4
5
# File 'lib/legion/service.rb', line 3

def modules
  [Legion::Crypt, Legion::Transport, Legion::Cache, Legion::Data, Legion::Supervision].freeze
end

#reloadObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/legion/service.rb', line 102

def reload
  Legion::Logging.info 'Legion::Service.reload was called'
  Legion::Extensions.shutdown
  sleep(1)
  Legion::Data.shutdown
  Legion::Cache.shutdown
  Legion::Transport::Connection.shutdown
  Legion::Crypt.shutdown
  Legion::Settings[:client][:ready] = false

  sleep(5)
  setup_settings
  Legion::Crypt.start
  setup_transport
  setup_data
  setup_supervision
  load_extensions

  Legion::Crypt.cs
  Legion::Settings[:client][:ready] = true
  Legion::Logging.info 'Legion has been reloaded'
end

#setup_dataObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/legion/service.rb', line 31

def setup_data
  # if RUBY_ENGINE == 'truffleruby'
  #   Legion::Logging.error 'Legion::Data does not support truffleruby, please use MRI for any LEX that require it '
  #   Legion::Settings[:data][:connected] = false
  #   return false
  # end

  require 'legion/data'
  Legion::Settings.merge_settings(:data, Legion::Data::Settings.default)
  Legion::Data.setup
rescue LoadError
  Legion::Logging.info 'Legion::Data gem is not installed, please install it manually with gem install legion-data'
rescue StandardError => e
  Legion::Logging.warn "Legion::Data failed to load, starting without it. e: #{e.message}"
end

#setup_logging(log_level: 'info', **_opts) ⇒ Object



73
74
75
76
# File 'lib/legion/service.rb', line 73

def setup_logging(log_level: 'info', **_opts)
  require 'legion/logging'
  Legion::Logging.setup(log_level: log_level, level: log_level, trace: true)
end

#setup_settings(default_dir = __dir__) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/legion/service.rb', line 57

def setup_settings(default_dir = __dir__)
  require 'legion/settings'
  config_directory = default_dir
  default_paths.each do |path|
    next unless Dir.exist? path

    Legion::Logging.info "Using #{path} for settings"
    config_directory = path
    break
  end

  Legion::Logging.info "Using directory #{config_directory} for settings"
  Legion::Settings.load(config_dir: config_directory)
  Legion::Logging.info('Legion::Settings Loaded')
end

#setup_supervisionObject



84
85
86
87
# File 'lib/legion/service.rb', line 84

def setup_supervision
  require 'legion/supervision'
  @supervision = Legion::Supervision.setup
end

#setup_transportObject



78
79
80
81
82
# File 'lib/legion/service.rb', line 78

def setup_transport
  require 'legion/transport'
  Legion::Settings.merge_settings('transport', Legion::Transport::Settings.default)
  Legion::Transport::Connection.setup
end

#shutdownObject



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/legion/service.rb', line 89

def shutdown
  Legion::Logging.info('Legion::Service.shutdown was called')
  @shutdown = true
  Legion::Settings[:client][:shutting_down] = true
  sleep(0.5)
  Legion::Extensions.shutdown
  sleep(1)
  Legion::Data.shutdown if Legion::Settings[:data][:connected]
  Legion::Cache.shutdown
  Legion::Transport::Connection.shutdown
  Legion::Crypt.shutdown
end