Class: Cognizant::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/cognizant/application.rb,
lib/cognizant/application/dsl_proxy.rb

Defined Under Namespace

Classes: DSLProxy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, options = {}, &block) ⇒ Application

Returns a new instance of Application.


17
18
19
# File 'lib/cognizant/application.rb', line 17

def initialize(name = nil, options = {}, &block)
  self.setup({ name: name }.merge(options), &block)
end

Instance Attribute Details

#logs_dirObject

Returns the value of attribute logs_dir


14
15
16
# File 'lib/cognizant/application.rb', line 14

def logs_dir
  @logs_dir
end

#nameObject

Returns the value of attribute name


14
15
16
# File 'lib/cognizant/application.rb', line 14

def name
  @name
end

#pids_dirObject

Returns the value of attribute pids_dir


14
15
16
# File 'lib/cognizant/application.rb', line 14

def pids_dir
  @pids_dir
end

#processesObject

Returns the value of attribute processes


15
16
17
# File 'lib/cognizant/application.rb', line 15

def processes
  @processes
end

Instance Method Details

#create_process(process_name, options = {}, &block) ⇒ Object


107
108
109
110
111
112
# File 'lib/cognizant/application.rb', line 107

def create_process(process_name, options = {}, &block)
  p = Cognizant::Process.new(process_name, options, &block)
  p.instance_variable_set(:@application, self)
  self.processes[p.name.to_sym] = p
  p
end

#handle_initialize_block(&block) ⇒ Object


36
37
38
39
40
41
42
43
44
# File 'lib/cognizant/application.rb', line 36

def handle_initialize_block(&block)
  if block.arity == 0
    dsl_proxy = Cognizant::Application::DSLProxy.new(self, &block)
    attributes = dsl_proxy.attributes
    set_attributes(attributes)
  else
    instance_exec(self, &block)
  end
end

#monitor(process_name = nil, attributes = {}, &block) ⇒ Object


72
73
74
75
76
# File 'lib/cognizant/application.rb', line 72

def monitor(process_name = nil, attributes = {}, &block)
  proc = create_process(process_name, attributes, &block)
  proc.monitor
  proc
end

#process(process_name = nil, attributes = {}, &block) ⇒ Object


78
79
80
# File 'lib/cognizant/application.rb', line 78

def process(process_name = nil, attributes = {}, &block)
  create_process(process_name, attributes, &block)
end

#reset!Object Also known as: shutdown!


82
83
84
85
86
87
88
# File 'lib/cognizant/application.rb', line 82

def reset!
  self.name      = nil
  self.pids_dir  = nil
  self.logs_dir  = nil
  self.processes.values.each(&:reset!) if self.processes.is_a?(Hash)
  self.processes = {}
end

#set_attributes(attributes) ⇒ Object


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cognizant/application.rb', line 46

def set_attributes(attributes)
  procs = {}
  procs = attributes.delete(:processes) if attributes.has_key?(:processes)

  mons = {}
  mons = attributes.delete(:monitor) if attributes.has_key?(:monitor)

  # Attributes.
  attributes.each do |key, value|
    if self.respond_to?("#{key}=")
      value = attributes.delete(key)
      self.send("#{key}=", value)
    end
  end

  # Processes.
  procs.each do |key, value|
    process(key, value)
  end

  # Automatically monitor these.
  mons.each do |key, value|
    monitor(key, value)
  end
end

#setup(options = {}, &block) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cognizant/application.rb', line 21

def setup(options = {}, &block)
  self.reset!

  set_attributes(options)

  handle_initialize_block(&block) if block

  raise "Application name is missing. Aborting." unless self.name
  Log[self].info "Loading application #{self.name}..."

  raise "Application processes are missing. Aborting." unless self.processes.keys.size > 0

  self.setup_directories
end

#setup_directoriesObject


118
119
120
# File 'lib/cognizant/application.rb', line 118

def setup_directories
  Cognizant::System.mkdir(self.pids_dir, self.logs_dir)
end

#tickObject


114
115
116
# File 'lib/cognizant/application.rb', line 114

def tick
  self.processes.values.each(&:tick)
end