Class: God::Watch

Inherits:
Task
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/god/watch.rb

Constant Summary collapse

VALID_STATES =
[:init, :up, :start, :restart]
INITIAL_STATE =
:init

Instance Attribute Summary collapse

Attributes inherited from Task

#autostart, #behaviors, #directory, #driver, #group, #initial_state, #interval, #metrics, #name, #state, #valid_states

Instance Method Summary collapse

Methods inherited from Task

#attach, #autostart?, #canonical_hash_form, #dest_desc, #detach, #handle_event, #handle_poll, #lifecycle, #log_line, #method_missing, #move, #notify, #prepare, #signal, #transition, #trigger, #trigger?, #unmonitor

Constructor Details

#initializeWatch

Returns a new instance of Watch.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/god/watch.rb', line 23

def initialize
  super
  
  @process = God::Process.new
  
  # valid states
  self.valid_states = VALID_STATES
  self.initial_state = INITIAL_STATE
  
  # no grace period by default
  self.grace = self.start_grace = self.stop_grace = self.restart_grace = 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class God::Task

Instance Attribute Details

#graceObject

config



11
12
13
# File 'lib/god/watch.rb', line 11

def grace
  @grace
end

#restart_graceObject

config



11
12
13
# File 'lib/god/watch.rb', line 11

def restart_grace
  @restart_grace
end

#start_graceObject

config



11
12
13
# File 'lib/god/watch.rb', line 11

def start_grace
  @start_grace
end

#stop_graceObject

config



11
12
13
# File 'lib/god/watch.rb', line 11

def stop_grace
  @stop_grace
end

Instance Method Details

#action(a, c = nil) ⇒ Object

Actions



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/god/watch.rb', line 110

def action(a, c = nil)
  if !self.driver.in_driver_context?
    # called from outside Driver
    
    # send an async message to Driver
    self.driver.message(:action, [a, c])
  else
    # called from within Driver
    
    case a
    when :start
      call_action(c, :start)
      sleep(self.start_grace + self.grace)
    when :restart
      if self.restart
        call_action(c, :restart)
      else
        action(:stop, c)
        action(:start, c)
      end
      sleep(self.restart_grace + self.grace)
    when :stop
      call_action(c, :stop)
      sleep(self.stop_grace + self.grace)
    end
  end
  
  self
end

#behavior(kind) {|b| ... } ⇒ Object

Behavior

Yields:

  • (b)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/god/watch.rb', line 46

def behavior(kind)
  # create the behavior
  begin
    b = Behavior.generate(kind, self)
  rescue NoSuchBehaviorError => e
    abort e.message
  end
  
  # send to block so config can set attributes
  yield(b) if block_given?
  
  # abort if the Behavior is invalid, the Behavior will have printed
  # out its own error messages by now
  abort unless b.valid?
  
  self.behaviors << b
end

#call_action(condition, action) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/god/watch.rb', line 140

def call_action(condition, action)
  # before
  before_items = self.behaviors
  before_items += [condition] if condition
  before_items.each do |b|
    info = b.send("before_#{action}")
    if info
      msg = "#{self.name} before_#{action}: #{info} (#{b.base_name})"
      applog(self, :info, msg)
    end
  end
  
  # log
  if self.send(action)
    msg = "#{self.name} #{action}: #{self.send(action).to_s}"
    applog(self, :info, msg)
  end
  
  @process.call_action(action)
  
  # after
  after_items = self.behaviors
  after_items += [condition] if condition
  after_items.each do |b|
    info = b.send("after_#{action}")
    if info
      msg = "#{self.name} after_#{action}: #{info} (#{b.base_name})"
      applog(self, :info, msg)
    end
  end
end

#monitorObject

Enable monitoring



95
96
97
98
99
100
101
102
# File 'lib/god/watch.rb', line 95

def monitor
  # start monitoring at the first available of the init or up states
  if !self.metrics[:init].empty?
    self.move(:init)
  else
    self.move(:up)
  end
end

#register!Object

Registration



178
179
180
# File 'lib/god/watch.rb', line 178

def register!
  God.registry.add(@process)
end

#restart_ifObject



76
77
78
79
80
# File 'lib/god/watch.rb', line 76

def restart_if
  self.transition(:up, :restart) do |on|
    yield(on)
  end
end

#start_ifObject

Simple mode



70
71
72
73
74
# File 'lib/god/watch.rb', line 70

def start_if
  self.transition(:up, :start) do |on|
    yield(on)
  end
end

#stop_ifObject



82
83
84
85
86
# File 'lib/god/watch.rb', line 82

def stop_if 
  self.transition(:up, :stop) do |on| 
    yield(on) 
  end 
end

#unregister!Object



182
183
184
185
# File 'lib/god/watch.rb', line 182

def unregister!
  God.registry.remove(@process)
  super
end

#valid?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/god/watch.rb', line 36

def valid?
  super && @process.valid?
end