Module: Berkshelf::API::Application

Extended by:
Logging, Mixin::Services, Forwardable
Defined in:
lib/berkshelf/api/application.rb

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Mixin::Services

extended, included

Methods included from Logging

init, logger

Class Attribute Details

.start_timeObject (readonly)

Returns the value of attribute start_time.



31
32
33
# File 'lib/berkshelf/api/application.rb', line 31

def start_time
  @start_time
end

Class Method Details

.configBerkshelf::API::Config



35
36
37
38
39
40
41
# File 'lib/berkshelf/api/application.rb', line 35

def config
  @config ||= begin
    Berkshelf::API::Config.from_file(Berkshelf::API::Config.default_path)
  rescue
    Berkshelf::API::Config.new
  end
end

.configure(options = {}) ⇒ Berkshelf::API::Config

Configure the application

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :config_file (String)

    filepath to a configuration file to use

  • :log_location (String, Fixnum) — default: STDOUT
  • :log_level (String, nil) — default: "INFO"
    • “DEBUG

    • “INFO”

    • “WARN”

    • “ERROR”

    • “FATAL”

Returns:

Raises:



63
64
65
66
67
68
69
70
71
72
# File 'lib/berkshelf/api/application.rb', line 63

def configure(options = {})
  unless options[:config_file].nil?
    set_config Berkshelf::API::Config.from_file(options[:config_file])
  end

  configure_logger(options)
  config
rescue Buff::Errors::ConfigNotFound => ex
  raise ConfigNotFoundError, ex
end

.configure_logger(options = {}) ⇒ Object

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :log_location (String, Fixnum) — default: STDOUT
  • :log_level (String, nil) — default: "INFO"
    • “DEBUG

    • “INFO”

    • “WARN”

    • “ERROR”

    • “FATAL”



81
82
83
# File 'lib/berkshelf/api/application.rb', line 81

def configure_logger(options = {})
  Logging.init(level: options[:log_level], location: options[:log_location])
end

.home_pathString

Returns:

  • (String)


86
87
88
# File 'lib/berkshelf/api/application.rb', line 86

def home_path
  ENV['BERKSHELF_API_PATH'] || config.home_path
end

.instanceBerkshelf::API::Application

Retrieve the running instance of the Application



95
96
97
98
99
# File 'lib/berkshelf/api/application.rb', line 95

def instance
  return @instance if @instance

  raise NotStartedError, "application not running"
end

.registryCelluloid::Registry

Note:

Berkshelf::API uses it’s own registry instead of Celluloid::Registry.root to avoid conflicts in the larger namespace. Use Berkshelf::API::Application[] to access Berkshelf::API actors instead of Celluloid::Actor[].

The Actor registry for Berkshelf::API.

Returns:

  • (Celluloid::Registry)


108
109
110
# File 'lib/berkshelf/api/application.rb', line 108

def registry
  @registry ||= Celluloid::Registry.new
end

.run(options = {}) ⇒ Object

Run the application in the foreground (sleep on main thread)

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :disable_http (Boolean) — default: false

    run the application without the rest gateway



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/berkshelf/api/application.rb', line 116

def run(options = {})
  @start_time = Time.now.utc
  loop do
    supervisor = run!(options)

    while supervisor.alive?
      sleep 0.1
      instance.terminate if @shutdown
    end

    break if @shutdown

    log.error "!!! #{self} crashed. Restarting..."
  end
end

.run!(options = {}) ⇒ Berkshelf::API::Application

Run the application in the background

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :disable_http (Boolean) — default: false

    run the application without the rest gateway

  • :eager_build (Boolean) — default: false

    automatically begin and loop all cache builders

Returns:



140
141
142
143
144
145
146
# File 'lib/berkshelf/api/application.rb', line 140

def run!(options = {})
  options = { disable_http: false, eager_build: false }.merge(options)
  configure(options)
  @instance = ApplicationSupervisor.new(registry, options)
  cache_builder.async(:build_loop) if options[:eager_build]
  @instance
end

.running?Boolean

Returns:

  • (Boolean)


149
150
151
152
153
# File 'lib/berkshelf/api/application.rb', line 149

def running?
  instance.alive?
rescue NotStartedError
  false
end

.set_config(config) ⇒ Object

Parameters:



44
45
46
# File 'lib/berkshelf/api/application.rb', line 44

def set_config(config)
  @config = config
end

.shutdownObject

Shutdown the running instance

Raises:



159
160
161
# File 'lib/berkshelf/api/application.rb', line 159

def shutdown
  @shutdown = true
end