Class: Arkaan::Utils::MicroService

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/arkaan/utils/micro_service.rb

Overview

This class is a singleton to load and save parameters for the whole application.

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMicroService

Returns a new instance of MicroService.



24
25
26
27
28
29
30
# File 'lib/arkaan/utils/micro_service.rb', line 24

def initialize
  @location = false
  @service = false
  @instance = false
  @name = false
  @type = ENV['INSTANCE_TYPE'] || :heroku
end

Instance Attribute Details

#instanceObject (readonly)

Returns the value of attribute instance.



19
20
21
# File 'lib/arkaan/utils/micro_service.rb', line 19

def instance
  @instance
end

#locationObject (readonly)

Returns the value of attribute location.



13
14
15
# File 'lib/arkaan/utils/micro_service.rb', line 13

def location
  @location
end

#nameObject (readonly)

Returns the value of attribute name.



10
# File 'lib/arkaan/utils/micro_service.rb', line 10

attr_reader :service

#serviceObject (readonly)

Returns the value of attribute service.



10
11
12
# File 'lib/arkaan/utils/micro_service.rb', line 10

def service
  @service
end

#typeObject (readonly)

Returns the value of attribute type.



22
23
24
# File 'lib/arkaan/utils/micro_service.rb', line 22

def type
  @type
end

Instance Method Details

#deactivate!Object

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



84
85
86
# File 'lib/arkaan/utils/micro_service.rb', line 84

def deactivate!
  instance.update_attribute(:running, false)
end

#from_location(filename) ⇒ Arkaan::utils::MicroService

Sets the location of the file calling the micro service and initializing it so that it’s used as root.

Parameters:

  • filename (String)

    the full naame of the file with the extension.

Returns:

  • (Arkaan::utils::MicroService)

    the instance of the micro-service to chain other calls.



55
56
57
58
# File 'lib/arkaan/utils/micro_service.rb', line 55

def from_location(filename)
  @location = File.dirname(filename)
  return self
end

#in_standard_modeArkaan::utils::MicroService

Loads the application in standard (production/development) mode, without the test files.

Returns:

  • (Arkaan::utils::MicroService)

    the instance of the micro-service to chain other calls.



62
63
64
# File 'lib/arkaan/utils/micro_service.rb', line 62

def in_standard_mode
  return load_application(test_mode: false)
end

#in_test_modeArkaan::utils::MicroService

Loads the application in test mode, by adding the needed files to run the test suite to the standard loading process.

Returns:

  • (Arkaan::utils::MicroService)

    the instance of the micro-service to chain other calls.



68
69
70
71
# File 'lib/arkaan/utils/micro_service.rb', line 68

def in_test_mode
  @location = File.join(location, '..')
  return load_application(test_mode: true)
end

#in_websocket_modeArkaan::utils::MicroService

Loads the application as a websockets service. Only the websockets application should use that.

Returns:

  • (Arkaan::utils::MicroService)

    the instance of the micro-service to chain other calls.



75
76
77
78
79
80
81
# File 'lib/arkaan/utils/micro_service.rb', line 75

def in_websocket_mode
  load_mongoid_configuration
  load_standard_files

  Arkaan::Monitoring::Websocket.find_or_create_by(url: ENV['WEBSOCKET_URL']).save
  return self
end

#loadable?Boolean

Determines if the application can be loaded (all the parameters have been correctly set)

Returns:

  • (Boolean)

    TRUE if the application can be safely loaded, FALSE otherwise.



34
35
36
# File 'lib/arkaan/utils/micro_service.rb', line 34

def loadable?
  return !!(service && location)
end

#pathString, Boolean

Getter for the path on which the service is mapped.

Returns:

  • (String, Boolean)

    the absolute path in the URL on which the service is mapped upon, or FALSE if it’s not set already.



40
41
42
# File 'lib/arkaan/utils/micro_service.rb', line 40

def path
  return service ? service.path : false
end

#register_as(service_name) ⇒ Arkaan::utils::MicroService

Look for the service and sets it if it’s found in the database, or set it to nil if not found.

Parameters:

  • service_name (String)
    • the name of the service to look for in the database.

Returns:

  • (Arkaan::utils::MicroService)

    the instance of the micro-service to chain other calls.



47
48
49
50
# File 'lib/arkaan/utils/micro_service.rb', line 47

def register_as(service_name)
  @name = service_name
  return self
end