Class: Perennial::Loader

Inherits:
Object show all
Includes:
Hookable, Singleton
Defined in:
lib/perennial/loader.rb

Constant Summary collapse

@@controllers =
{}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Hookable

included

Class Method Details

.register_controller(name, controller) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/perennial/loader.rb', line 13

def self.register_controller(name, controller)
  return if name.blank? || controller.blank?
  name = name.to_sym
  @@controllers[name] = controller
  metaclass.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
    def #{name}?                 # def client?
      @@current_type == :#{name} #   @@current_type == :client
    end                          # end
  RUBY
end

.run!(type = self.default_type, options = {}) ⇒ Object



24
25
26
27
# File 'lib/perennial/loader.rb', line 24

def self.run!(type = self.default_type, options = {})
  @@current_type = type.to_sym
  self.instance.run!(options)
end

.stop!(force = false) ⇒ Object



29
30
31
# File 'lib/perennial/loader.rb', line 29

def self.stop!(force = false)
  self.instance.stop!(force)
end

Instance Method Details

#current_controllerObject



55
56
57
58
59
60
# File 'lib/perennial/loader.rb', line 55

def current_controller
  @current_controller ||= begin
    c = @@controllers[@@current_type.to_sym]
    c.is_a?(String) ? eval(c) : c
  end
end

#run!(options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/perennial/loader.rb', line 33

def run!(options = {})
  self.register_signals
  self.class.invoke_hooks! :before_setup
  Daemon.daemonize! if Settings.daemon?
  Logger.log_name = "#{@@current_type.to_s}.log"
  Logger.setup
  Settings.setup
  self.load_custom_code
  self.class.invoke_hooks!        :before_run
  self.attempt_controller_action! :run, options
end

#stop!(force = false) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/perennial/loader.rb', line 45

def stop!(force = false)
  if force || !@attempted_stop
    self.class.invoke_hooks!        :before_stop
    self.attempt_controller_action! :stop
    self.class.invoke_hooks!        :after_stop
    @attempted_stop = true
  end
  Daemon.cleanup! if Settings.daemon?
end