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

Modules: Status 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



50
51
52
53
54
# File 'lib/mb/application.rb', line 50

def instance
  return @instance unless @instance.nil?

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

.pauseObject

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



113
114
115
# File 'lib/mb/application.rb', line 113

def pause
  @status = Status::PAUSED
end

.paused?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/mb/application.rb', line 121

def paused?
  status == Status::PAUSED
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)


63
64
65
# File 'lib/mb/application.rb', line 63

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

.resumeObject



117
118
119
# File 'lib/mb/application.rb', line 117

def resume
  @status = Status::RUNNING
end

.run(config) ⇒ Object

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

Parameters:



84
85
86
87
88
89
90
91
92
93
94
# 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) ⇒ Object

Run the application asynchronously (terminate after execution)

Parameters:



70
71
72
73
74
75
76
77
78
79
# 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

.setupObject

Prepare the application and environment to run motherbrain



97
98
99
100
# File 'lib/mb/application.rb', line 97

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

.statusObject



125
126
127
# File 'lib/mb/application.rb', line 125

def status
  @status ||= Status::RUNNING
end

.stopObject

Stop the running application



103
104
105
106
# File 'lib/mb/application.rb', line 103

def stop
  instance.async_interrupt(3)
  @status = Status::STOPPING
end