Sha256: a620e200ce9ccd107d099695dbb6d362af0c6670a745d96f398bd14aaf308fe0

Contents?: true

Size: 1.87 KB

Versions: 4

Compression:

Stored size: 1.87 KB

Contents

require 'state_machine'

class Eye::Process

  # do transition
  def switch(name, reason = nil)
    @state_reason = reason || last_scheduled_reason
    self.send("#{name}!")
  end

  state_machine :state, :initial => :unmonitored do
    state :unmonitored, :up, :down
    state :starting, :stopping, :restarting

    event :starting do
      transition [:unmonitored, :down] => :starting
    end

    event :already_running do
      transition [:unmonitored, :down, :up] => :up
    end

    event :started do
      transition :starting => :up
    end

    event :crashed do
      transition [:starting, :restarting, :up] => :down
    end

    event :stopping do
      transition [:up, :restarting] => :stopping
    end

    event :stopped do
      transition :stopping => :down
    end

    event :cant_kill do
      transition :stopping => :up
    end

    event :restarting do
      transition [:unmonitored, :up, :down] => :restarting
    end

    event :restarted do
      transition :restarting => :up
    end

    event :unmonitoring do
      transition any => :unmonitored
    end

    after_transition any => :unmonitored, :do => :on_unmonitored
    after_transition any-:up => :up, :do => :on_up
    after_transition :up => any-:up, :do => :from_up
    after_transition any => any, :do => :log_transition
    after_transition any => any, :do => :check_triggers
    after_transition :on => :crashed, :do => :on_crashed
  end

  def on_crashed
    schedule :check_crash, Eye::Reason.new(:crashed)
  end

  def on_unmonitored
    self.pid = nil
  end

  def on_up
    add_watchers
    add_childs
  end

  def from_up
    remove_watchers
    remove_childs
  end

  def log_transition(transition)
    @states_history.push transition.to_name, @state_reason
    info "switch :#{transition.event} [:#{transition.from_name} => :#{transition.to_name}] #{@state_reason ? "(reason: #{@state_reason})" : nil}"
  end

end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
reel-eye-0.3.2 lib/eye/process/states.rb
eye-0.3.2 lib/eye/process/states.rb
reel-eye-0.3.1 lib/eye/process/states.rb
eye-0.3.1 lib/eye/process/states.rb