class AmpelExtase::SemaphoreStateObserver
Attributes
build_state[R]
state_changed_at[R]
Public Class Methods
for_url(semaphore_url)
click to toggle source
# File lib/ampel_extase/semaphore_state_observer.rb, line 5 def self.for_url(semaphore_url) if semaphore_url client = AmpelExtase::SemaphoreClient.new(semaphore_url) new(client) else Tins::NULL end end
new(client)
click to toggle source
# File lib/ampel_extase/semaphore_state_observer.rb, line 14 def initialize(client) @client = client reset end
Public Instance Methods
building?()
click to toggle source
# File lib/ampel_extase/semaphore_state_observer.rb, line 27 def building? !@client.fetch_build(:last_build)['finished_at'] end
expired?(duration)
click to toggle source
# File lib/ampel_extase/semaphore_state_observer.rb, line 64 def expired?(duration) Time.now > @state_changed_at + duration end
fetch_new_state()
click to toggle source
# File lib/ampel_extase/semaphore_state_observer.rb, line 45 def fetch_new_state AmpelExtase::BuildState.for [ last_result, building? ] end
last_result()
click to toggle source
# File lib/ampel_extase/semaphore_state_observer.rb, line 23 def last_result @client.fetch_build(:last_completed_build)['result'] end
on_state_change() { |new_state| ... }
click to toggle source
# File lib/ampel_extase/semaphore_state_observer.rb, line 49 def on_state_change new_state = fetch_new_state if state_changed?(new_state) puts "state changed from #@build_state to #{new_state} => taking action" begin yield new_state ensure set_state new_state end else puts "state did not change, is still #@build_state => do nothing" end self end
reset()
click to toggle source
# File lib/ampel_extase/semaphore_state_observer.rb, line 19 def reset set_state AmpelExtase::BuildState.for end
set_state(state)
click to toggle source
# File lib/ampel_extase/semaphore_state_observer.rb, line 35 def set_state(state) @build_state = state @state_changed_at = Time.now self end
state_changed?(new_state)
click to toggle source
# File lib/ampel_extase/semaphore_state_observer.rb, line 31 def state_changed?(new_state) new_state != @build_state end