class Virtuaservices::Utils::MicroService

This class is a singleton to load and save parameters for the whole application. @author Vincent Courtois <courtois.vincent@outlook.com>

Attributes

instance[R]

@!attribute [r] instance

@return [Virtuaservices::Monitoring::Instance] the instance of the service currently deployed.
location[R]

@!attribute [r] location

@return [String] the path to the file loading the whole application, used to deduce the loading paths.
name[R]

@!attribute [r] name

@return [String] the name of the service, used later to instantiate it when the mongoid configuration is fully loaded.
service[R]

@!attribute [r] name

@return [String] the name of the service you want to load.
type[R]

@!attribute [r] type

@return [Symbol] the type of instance the application is declaring

Public Class Methods

new() click to toggle source
# File lib/virtuaservices/utils/micro_service.rb, line 24
def initialize
  @location = false
  @service = false
  @instance = false
  @name = false
  @type = ENV['INSTANCE_TYPE'] || :heroku
  @controller_classes = []
end

Public Instance Methods

deactivate!() click to toggle source

Deactivates the current instance and the associated service if no more instances are available.

# File lib/virtuaservices/utils/micro_service.rb, line 95
def deactivate!
  instance.update_attribute(:running, false)
  service.update_attribute(:test_mode, false) if ENV['TEST_MODE']
end
from_location(filename) click to toggle source

Sets the location of the file calling the micro service and initializing it so that it's used as root. @param filename [String] the full naame of the file with the extension. @return [Virtuaservices::utils::MicroService] the instance of the micro-service to chain other calls.

# File lib/virtuaservices/utils/micro_service.rb, line 66
def from_location(filename)
  @location = File.dirname(filename)
  return self
end
get_const(symbol) click to toggle source
# File lib/virtuaservices/utils/micro_service.rb, line 39
def get_const(symbol)
  return Object.const_get("Controllers::#{symbol.to_s}")
end
get_controllers() click to toggle source
# File lib/virtuaservices/utils/micro_service.rb, line 33
def get_controllers
  return [] if defined?(Controllers).nil?
  classes = Controllers.constants.map { |symbol| get_const(symbol) }
  return classes.select { |symbol| symbol.is_a? Class }
end
in_standard_mode() click to toggle source

Loads the application in standard (production/development) mode, without the test files. @return [Virtuaservices::utils::MicroService] the instance of the micro-service to chain other calls.

# File lib/virtuaservices/utils/micro_service.rb, line 73
def in_standard_mode
  return load_application(test_mode: false)
end
in_test_mode() click to toggle source

Loads the application in test mode, by adding the needed files to run the test suite to the standard loading process. @return [Virtuaservices::utils::MicroService] the instance of the micro-service to chain other calls.

# File lib/virtuaservices/utils/micro_service.rb, line 79
def in_test_mode
  @location = File.join(location, '..')
  return load_application(test_mode: true)
end
in_websocket_mode() click to toggle source

Loads the application as a websockets service. Only the websockets application should use that. @return [Virtuaservices::utils::MicroService] the instance of the micro-service to chain other calls.

# File lib/virtuaservices/utils/micro_service.rb, line 86
def in_websocket_mode
  load_mongoid_configuration
  load_standard_files

  Virtuaservices::Monitoring::Websocket.find_or_create_by(url: ENV['WEBSOCKET_URL']).save
  return self
end
loadable?() click to toggle source

Determines if the application can be loaded (all the parameters have been correctly set) @return [Boolean] TRUE if the application can be safely loaded, FALSE otherwise.

# File lib/virtuaservices/utils/micro_service.rb, line 45
def loadable?
  return !!(service && location)
end
path() click to toggle source

Getter for the path on which the service is mapped. @return [String, Boolean] the absolute path in the URL on which the service is mapped upon, or FALSE if it's not set already.

# File lib/virtuaservices/utils/micro_service.rb, line 51
def path
  return service ? service.path : false
end
register_as(service_name) click to toggle source

Look for the service and sets it if it's found in the database, or set it to nil if not found. @param [String] service_name - the name of the service to look for in the database. @return [Virtuaservices::utils::MicroService] the instance of the micro-service to chain other calls.

# File lib/virtuaservices/utils/micro_service.rb, line 58
def register_as(service_name)
  @name = service_name
  return self
end

Private Instance Methods

load_application(test_mode: false) click to toggle source

Loads the configuration for Mongoid, the files of the application, and registers the service and the instance in the database. @param test_mode [Boolean] TRUE to run in test mode (from /spec), FALSE otherwise. @return [Virtuaservices::Utils::MicroService] the current instance of the micro service to chain other calls.

# File lib/virtuaservices/utils/micro_service.rb, line 121
def load_application(test_mode: false)
  Dotenv.load
  load_mongoid_configuration(test_mode: test_mode)
  if !!(@name && location)
    @service = Virtuaservices::Monitoring::Service.where(key: @name).first
    register_service if @service.nil?
    if ENV['TEST_MODE']
      @service.update_attribute(:test_mode, true)
    end
    register_instance
    load_plugin!
    if service
      load_standard_files
      load_test_files if test_mode
    end
  end
  return self
end
load_mongoid_configuration(test_mode: false) click to toggle source
# File lib/virtuaservices/utils/micro_service.rb, line 147
def load_mongoid_configuration(test_mode: false)
  environment = test_mode ? 'test' : (ENV['RACK_ENV'] || 'development')
  Mongoid.load!('config/mongoid.yml', environment)
end
load_plugin!() click to toggle source
# File lib/virtuaservices/utils/micro_service.rb, line 140
def load_plugin!
  plugin = ENV['ADDITIONAL_PLUGIN']
  if !plugin.nil? && Virtuaservices::Utils::Plugins.constants.include?(plugin)
    Virtuaservices::Utils::Plugins.const_get(plugin).load!(@instance)
  end
end
load_standard_files() click to toggle source
# File lib/virtuaservices/utils/micro_service.rb, line 152
def load_standard_files
  require_folder('decorators')
  require_folder('services')
  require_folder('controllers')
end
load_test_files() click to toggle source
# File lib/virtuaservices/utils/micro_service.rb, line 158
def load_test_files
  require_folder('spec', 'support')
  require_folder('spec', 'shared')
end
register_instance() click to toggle source

Register the instance of the currently deployed service in the database. @return [Virtuaservices::Monitoring::Instance] the instance of the micro service currently running.

# File lib/virtuaservices/utils/micro_service.rb, line 109
def register_instance
  @instance = @service.instances.where(url: ENV['SERVICE_URL']).first
  if @instance.nil?
    @instance = Virtuaservices::Monitoring::Instance.create(service: @service, url: ENV['SERVICE_URL'], type: type)
  end
  @instance.update_attribute(:running, true)
  return @instance
end
register_service() click to toggle source

Registers the service in the database if it has not been created already.

# File lib/virtuaservices/utils/micro_service.rb, line 103
def register_service
  @service = Virtuaservices::Monitoring::Service.create(key: @name, path: "/#{@name}")
end
require_folder(*folders) click to toggle source
# File lib/virtuaservices/utils/micro_service.rb, line 163
def require_folder(*folders)
  Dir[File.join(location, folders, '**', '*.rb')].each do |filename|
    require filename
  end
end