Module: Pazuzu::Utility::Runnable

Included in:
Application, Control::SocketServer, Instance, Supervisor, RunnablePool, Worker
Defined in:
lib/pazuzu/utility/runnable.rb

Overview

Mixin that provides a state machine for running state.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#started_atObject (readonly)

Returns the value of attribute started_at.



60
61
62
# File 'lib/pazuzu/utility/runnable.rb', line 60

def started_at
  @started_at
end

#stopped_atObject (readonly)

Returns the value of attribute stopped_at.



61
62
63
# File 'lib/pazuzu/utility/runnable.rb', line 61

def stopped_at
  @stopped_at
end

Instance Method Details

#initializeObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pazuzu/utility/runnable.rb', line 7

def initialize
  _self = self
  @started_at = nil
  @stopped_at = nil
  @state_condition = ConditionVariable.new
  @mutex = Mutex.new
  @runnable_state = Statemachine.build do
    state :stopped do
      event :start!, :starting
      on_entry :enter_stopped
    end      
    state :starting do
      event :started!, :running
      event :failed!, :failed
      event :stop!, :stopping
      on_entry :enter_starting
    end
    state :running do
      event :stop!, :stopping
      event :starting!, :starting
      on_entry :enter_started
    end
    state :stopping do
      event :stopped!, :stopped
      on_entry :enter_stopping
    end
    state :failed do
      on_entry :enter_failed
    end
    context _self
  end
end

#run_stateObject



52
53
54
# File 'lib/pazuzu/utility/runnable.rb', line 52

def run_state
  @runnable_state.state
end

#start!Object



40
41
42
43
44
# File 'lib/pazuzu/utility/runnable.rb', line 40

def start!
  if [:stopped, :failed].include?(@runnable_state.state)
    @runnable_state.start!
  end
end

#stop!Object



46
47
48
49
50
# File 'lib/pazuzu/utility/runnable.rb', line 46

def stop!
  if [:starting, :running].include?(@runnable_state.state)
    @runnable_state.stop!
  end
end

#wait_for_state_change!Object



56
57
58
# File 'lib/pazuzu/utility/runnable.rb', line 56

def wait_for_state_change!
  @mutex.synchronize { @state_condition.wait(@mutex) }
end