Class: UState::AutoState

Inherits:
Object
  • Object
show all
Defined in:
lib/ustate/auto_state.rb

Instance Method Summary collapse

Constructor Details

#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

Instance Method Details

#descriptionObject



59
60
61
# File 'lib/ustate/auto_state.rb', line 59

def description
  @state.description
end

#description=(description) ⇒ Object



54
55
56
57
# File 'lib/ustate/auto_state.rb', line 54

def description=(description)
  @state.description = description
  flush
end

#flushObject

Send state to client



64
65
66
67
# File 'lib/ustate/auto_state.rb', line 64

def flush
  @state.time = Time.now.to_i
  @client << @state
end

#hostObject



74
75
76
# File 'lib/ustate/auto_state.rb', line 74

def host
  @state.host
end

#host=(host) ⇒ Object



69
70
71
72
# File 'lib/ustate/auto_state.rb', line 69

def host=(host)
  @state.host = host
  flush
end

#merge(opts) ⇒ Object Also known as: <<

Performs multiple updates, followed by flush. Example: merge state: critical, metric_f: 10235.3



89
90
91
92
93
94
# File 'lib/ustate/auto_state.rb', line 89

def merge(opts)
  opts.each do |k, v|
    @state.send "#{k}=", v
  end
  flush
end

#metric_fObject



83
84
85
# File 'lib/ustate/auto_state.rb', line 83

def metric_f
  @state.metric_f
end

#metric_f=(metric_f) ⇒ Object



78
79
80
81
# File 'lib/ustate/auto_state.rb', line 78

def metric_f=(metric_f)
  @state.metric_f = metric_f
  flush
end

#once(opts) ⇒ Object

Issues an immediate update of the state with the :once option set, but does not update the local state. Useful for transient errors. Opts are merged with the state.



100
101
102
103
104
105
106
107
108
# File 'lib/ustate/auto_state.rb', line 100

def once(opts)
  o = @state.dup
  opts.each do |k, v|
    o.send "#{k}=", v
  end
  o.time = Time.now.to_i
  o.once = true
  @client << o
end

#serviceObject



124
125
126
# File 'lib/ustate/auto_state.rb', line 124

def service
  @state.service
end

#service=(service) ⇒ Object



119
120
121
122
# File 'lib/ustate/auto_state.rb', line 119

def service=(service)
  @state.service = service
  flush
end

#stateObject



115
116
117
# File 'lib/ustate/auto_state.rb', line 115

def state
  @state.state
end

#state=(state) ⇒ Object



110
111
112
113
# File 'lib/ustate/auto_state.rb', line 110

def state=(state)
  @state.state = state
  flush
end