module MotherBrain::Application

Main application supervisor for motherbrain

@example running the application in the foreground

MB::Application.run(config)

@example running the application in the background

MB::Application.run!(config)

Public Class Methods

instance() click to toggle source

@raise [Celluloid::DeadActorError] if Application has not been started

@return [Celluloid::SupervisionGroup(MB::Application::SupervisionGroup)]

# File lib/mb/application.rb, line 50
def instance
  return @instance unless @instance.nil?

  raise Celluloid::DeadActorError, "application not running"
end
pause() click to toggle source

Set the application state to paused. This allows actors to continue processing, but causes the RestGateway not to accept new requests.

See: MotherBrain::API::V1 L51, ‘before’ block

# File lib/mb/application.rb, line 113
def pause
  @status = Status::PAUSED
end
paused?() click to toggle source
# File lib/mb/application.rb, line 121
def paused?
  status == Status::PAUSED
end
registry() click to toggle source

The Actor registry for motherbrain.

@note motherbrain uses it’s own registry instead of Celluloid::Registry.root to

avoid conflicts in the larger namespace. Use MB::Application[] to access motherbrain
actors instead of Celluloid::Actor[].

@return [Celluloid::Registry]

# File lib/mb/application.rb, line 63
def registry
  @registry ||= Celluloid::Registry.new
end
resume() click to toggle source
# File lib/mb/application.rb, line 117
def resume
  @status = Status::RUNNING
end
run(config) click to toggle source

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

@param [MB::Config] config

# File lib/mb/application.rb, line 84
def run(config)
  loop do
    supervisor = run!(config)

    sleep 0.1 while supervisor.alive?

    break if supervisor.interrupted

    log.fatal { "!!! #{self} crashed. Restarting..." }
  end
end
run!(config) click to toggle source

Run the application asynchronously (terminate after execution)

@param [MB::Config] config

# File lib/mb/application.rb, line 70
def run!(config)
  Celluloid.boot
  Celluloid.exception_handler do |ex|
    log.fatal { "Application received unhandled exception: #{ex.class} - #{ex.message}" }
    log.fatal { ex.backtrace.join("\n\t") }
  end
  log.info { "motherbrain starting..." }
  setup
  @instance = Application::SupervisionGroup.new(config)
end
setup() click to toggle source

Prepare the application and environment to run motherbrain

# File lib/mb/application.rb, line 97
def setup
  MB::Test.mock(:setup) if MB.testing?
  MB::FileSystem.init
end
status() click to toggle source
# File lib/mb/application.rb, line 125
def status
  @status ||= Status::RUNNING
end
stop() click to toggle source

Stop the running application

# File lib/mb/application.rb, line 103
def stop
  instance.async_interrupt(3)
  @status = Status::STOPPING
end