Class: Isono::Models::NodeState

Inherits:
Sequel::Model
  • Object
show all
Includes:
Logger
Defined in:
lib/isono/models/node_state.rb

Instance Method Summary collapse

Methods included from Logger

included, initialize

Instance Method Details

#state_machineObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/isono/models/node_state.rb', line 32

def state_machine
  model = self
  st = Statemachine.build do
    startstate :init
    trans :init,    :on_ping,      :online, proc {model.last_ping_at = Time.now}
    trans :online,  :on_timeout,   :timeout
    trans :timeout, :on_ping,      :online, proc {model.last_ping_at = Time.now}
    trans :online,  :on_unmonitor, :offline
    trans :timeout, :on_unmonitor, :offline
    # do nothing on transition from and to the same state
    trans :online,  :on_ping,      :online, proc {model.last_ping_at = Time.now}
    trans :timeout, :on_timeout,   :timeout

    on_entry_of :online, proc {
      model.state = :online
    }
    on_entry_of :timeout, proc {
      model.state = :timeout
    }
    on_entry_of :offline, proc {
      model.state = :offline
    }
  end

  if self[:state]
    if st.has_state(self[:state].to_sym)
      st.state = self[:state].to_sym
    else
      raise "Unknown state: #{self[:state]}"
    end
  else
    st.reset
  end
  st
end