lib/god/watch.rb in god-0.4.3 vs lib/god/watch.rb in god-0.5.0
- old
+ new
@@ -1,55 +1,37 @@
require 'etc'
require 'forwardable'
module God
- class Watch
+ class Watch < Task
VALID_STATES = [:init, :up, :start, :restart]
+ INITIAL_STATE = :init
# config
- attr_accessor :state, :interval, :group,
- :grace, :start_grace, :stop_grace, :restart_grace
-
+ attr_accessor :grace, :start_grace, :stop_grace, :restart_grace
- attr_writer :autostart
- def autostart?; @autostart; end
-
extend Forwardable
def_delegators :@process, :name, :uid, :gid, :start, :stop, :restart,
:name=, :uid=, :gid=, :start=, :stop=, :restart=,
- :pid_file, :pid_file=, :log, :log=, :alive?
-
- # api
- attr_accessor :behaviors, :metrics
-
- # internal
- attr_accessor :mutex
-
+ :pid_file, :pid_file=, :log, :log=, :alive?
#
def initialize
- @autostart ||= true
+ super
+
@process = God::Process.new
-
- # no grace period by default
- self.grace = self.start_grace = self.stop_grace = self.restart_grace = 0
- # the list of behaviors
- self.behaviors = []
+ # valid states
+ self.valid_states = VALID_STATES
+ self.initial_state = INITIAL_STATE
- # the list of conditions for each action
- self.metrics = {:init => [],
- :start => [],
- :restart => [],
- :up => []}
-
- # mutex
- self.mutex = Mutex.new
+ # no grace period by default
+ self.grace = self.start_grace = self.stop_grace = self.restart_grace = 0
end
def valid?
- @process.valid?
+ super && @process.valid?
end
###########################################################################
#
# Behavior
@@ -74,39 +56,10 @@
self.behaviors << b
end
###########################################################################
#
- # Advanced mode
- #
- ###########################################################################
-
- # Define a transition handler which consists of a set of conditions
- def transition(start_states, end_states)
- # convert to into canonical hash form
- canonical_end_states = canonical_hash_form(end_states)
-
- # for each start state do
- Array(start_states).each do |start_state|
- # validate start state
- unless VALID_STATES.include?(start_state)
- abort "Invalid state :#{start_state}. Must be one of the symbols #{VALID_STATES.map{|x| ":#{x}"}.join(', ')}"
- end
-
- # create a new metric to hold the watch, end states, and conditions
- m = Metric.new(self, canonical_end_states)
-
- # let the config file define some conditions on the metric
- yield(m)
-
- # record the metric
- self.metrics[start_state] << m
- end
- end
-
- ###########################################################################
- #
# Simple mode
#
###########################################################################
def start_if
@@ -124,104 +77,85 @@
###########################################################################
#
# Lifecycle
#
###########################################################################
-
+
# Enable monitoring
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
- # Disable monitoring
- def unmonitor
- self.move(nil)
- end
+ ###########################################################################
+ #
+ # Actions
+ #
+ ###########################################################################
- # Move from one state to another
- def move(to_state)
- msg = "#{self.name} move '#{self.state}' to '#{to_state}'"
- Syslog.debug(msg)
- LOG.log(self, :info, msg)
-
- # cleanup from current state
- from_state = self.state
- if from_state
- self.metrics[from_state].each { |m| m.disable }
- end
-
- # perform action
- self.action(to_state)
-
- # enable simple mode
- if [:start, :restart].include?(to_state) && self.metrics[to_state].empty?
- to_state = :up
- end
-
- # move to new state
- if to_state
- self.metrics[to_state].each { |m| m.enable }
- end
-
- # set state
- self.state = to_state
-
- # return self
- self
- end
-
def action(a, c = nil)
case a
when :start
- msg = "#{self.name} start: #{self.start.to_s}"
- Syslog.debug(msg)
- LOG.log(self, :info, msg)
call_action(c, :start)
sleep(self.start_grace + self.grace)
when :restart
if self.restart
- msg = "#{self.name} restart: #{self.restart.to_s}"
- Syslog.debug(msg)
- LOG.log(self, :info, msg)
call_action(c, :restart)
else
action(:stop, c)
action(:start, c)
end
sleep(self.restart_grace + self.grace)
when :stop
- if self.stop
- msg = "#{self.name} stop: #{self.stop.to_s}"
- Syslog.debug(msg)
- LOG.log(self, :info, msg)
- end
call_action(c, :stop)
sleep(self.stop_grace + self.grace)
- end
+ end
end
def call_action(condition, action)
# before
before_items = self.behaviors
before_items += [condition] if condition
- before_items.each { |b| b.send("before_#{action}") }
+ before_items.each do |b|
+ info = b.send("before_#{action}")
+ if info
+ msg = "#{self.name} before_#{action}: #{info} (#{b.base_name})"
+ Syslog.debug(msg)
+ LOG.log(self, :info, msg)
+ end
+ end
+ # log
+ if self.send(action)
+ msg = "#{self.name} #{action}: #{self.send(action).to_s}"
+ Syslog.debug(msg)
+ LOG.log(self, :info, msg)
+ end
+
@process.call_action(action)
-
+
# after
after_items = self.behaviors
after_items += [condition] if condition
- after_items.each { |b| b.send("after_#{action}") }
+ after_items.each do |b|
+ info = b.send("after_#{action}")
+ if info
+ msg = "#{self.name} after_#{action}: #{info} (#{b.base_name})"
+ Syslog.debug(msg)
+ LOG.log(self, :info, msg)
+ end
+ end
end
- def canonical_hash_form(to)
- to.instance_of?(Symbol) ? {true => to} : to
- end
+ ###########################################################################
+ #
+ # Registration
+ #
+ ###########################################################################
def register!
God.registry.add(@process)
end
\ No newline at end of file