module Legion class Service def modules [Legion::Crypt, Legion::Transport, Legion::Cache, Legion::Data, Legion::Supervision].freeze end 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 def setup_data require 'legion/data' Legion::Settings.merge_settings(:data, Legion::Data::Settings.default) Legion::Data.setup end # noinspection RubyArgCount def default_paths [ '/etc/legionio', "#{Etc.getpwuid.dir}/legionio", "#{ENV['home']}/legionio", '~/legionio', './settings' ] end def setup_settings(default_dir = __dir__) require 'legion/settings' config_directory = default_dir default_paths.each do |path| next unless Dir.exist? path 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 def setup_logging(log_level: 'info', **_opts) require 'legion/logging' Legion::Logging.setup(log_level: log_level, level: log_level, trace: true) end def setup_transport require 'legion/transport' Legion::Settings.merge_settings('transport', Legion::Transport::Settings.default) Legion::Transport::Connection.setup end def setup_supervision require 'legion/supervision' @supervision = Legion::Supervision.setup end 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 Legion::Cache.shutdown Legion::Transport::Connection.shutdown Legion::Crypt.shutdown end def load_extensions require 'legion/runner' Legion::Extensions.hook_extensions end end end