Module: MotherBrain::Application

Extended by:
Forwardable, MB::Mixin::Services, Logging
Defined in:
lib/mb/application.rb

Overview

Main application supervisor for motherbrain

Examples:

running the application in the foreground

MB::Application.run(config)

running the application in the background

MB::Application.run!(config)

Defined Under Namespace

Classes: SupervisionGroup

Class Method Summary collapse

Methods included from Logging

add_argument_header, dev, filename, log_exception, logger, logger, reset, set_logger

Class Method Details

.instanceCelluloid::SupervisionGroup(MB::Application::SupervisionGroup)

Returns:

  • (Celluloid::SupervisionGroup(MB::Application::SupervisionGroup))

Raises:

  • (Celluloid::DeadActorError)

    if Application has not been started



44
45
46
47
48
# File 'lib/mb/application.rb', line 44

def instance
  return @instance unless @instance.nil?

  raise Celluloid::DeadActorError, "application not running"
end

.registryCelluloid::Registry

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[].

The Actor registry for motherbrain.

Returns:

  • (Celluloid::Registry)


57
58
59
# File 'lib/mb/application.rb', line 57

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

.run(config) ⇒ Object

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

Parameters:



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mb/application.rb', line 74

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) ⇒ Object

Run the application asynchronously (terminate after execution)

Parameters:



64
65
66
67
68
69
# File 'lib/mb/application.rb', line 64

def run!(config)
  Celluloid.boot
  log.info { "motherbrain starting..." }
  setup
  @instance = Application::SupervisionGroup.new(config)
end

.setupObject

Prepare the application and environment to run motherbrain



87
88
89
90
# File 'lib/mb/application.rb', line 87

def setup
  MB::Test.mock(:setup) if MB.testing?
  MB::FileSystem.init
end

.stopObject

Stop the running application



93
94
95
# File 'lib/mb/application.rb', line 93

def stop
  instance.terminate
end