Method: UState::AutoState#initialize

Defined in:
lib/ustate/auto_state.rb

#initialize(client = Client.new, state = State.new) ⇒ AutoState

Binds together a State and a Client. Any change made here sends the state to the client. Useful when updates to a state are made decoherently, e.g. across many methods. Combine with MetricThread (or just Thread.new { loop { autostate.flush; sleep n } }) to ensure regular updates.

example:

class Job

def initialize
  @state = AutoState.new
  @state.service = 'job'
  @state.state = 'starting up'

  run
end

def run
  loop do
    begin
      a
      b
    rescue Exception => e
      @state.once(
        state: 'error',
        description: e.to_s
      )
    end
  end
end

def a
  @state.state = 'heavy lifting a'
  ...
end

def b
  @state.state = 'heavy lifting b'
  ...
end


44
45
46
47
48
49
50
51
52
# File 'lib/ustate/auto_state.rb', line 44

def initialize(client = Client.new, state = State.new)
  @client = client
  @state = case state
           when State
             state
           else
             State.new state
           end
end