class AmpelExtase::JenkinsStateObserver

Attributes

build_state[R]
state_changed_at[R]

Public Class Methods

for_url(jenkins_url) click to toggle source
# File lib/ampel_extase/jenkins_state_observer.rb, line 5
def self.for_url(jenkins_url)
  if jenkins_url
    jenkins = AmpelExtase::JenkinsClient.new(jenkins_url)
    new(jenkins)
  else
    Tins::NULL
  end
end
new(jenkins) click to toggle source
# File lib/ampel_extase/jenkins_state_observer.rb, line 14
def initialize(jenkins)
  @jenkins = jenkins
  reset
  check
end

Public Instance Methods

building?() click to toggle source
# File lib/ampel_extase/jenkins_state_observer.rb, line 34
def building?
  @jenkins.fetch_build(:last_build)['building']
end
check() click to toggle source
# File lib/ampel_extase/jenkins_state_observer.rb, line 24
def check
  puts "checking jenkins configuration for #{@jenkins.url.to_s.inspect}"
  @jenkins.fetch and puts "OK"
  self
end
expired?(duration) click to toggle source
# File lib/ampel_extase/jenkins_state_observer.rb, line 71
def expired?(duration)
  Time.now > @state_changed_at + duration
end
fetch_new_state() click to toggle source
# File lib/ampel_extase/jenkins_state_observer.rb, line 52
def fetch_new_state
  AmpelExtase::BuildState.for [ last_result, building? ]
end
last_result() click to toggle source
# File lib/ampel_extase/jenkins_state_observer.rb, line 30
def last_result
  @jenkins.fetch_build(:last_completed_build)['result']
end
on_state_change() { |new_state| ... } click to toggle source
# File lib/ampel_extase/jenkins_state_observer.rb, line 56
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/jenkins_state_observer.rb, line 20
def reset
  set_state AmpelExtase::BuildState.for
end
set_state(state) click to toggle source
# File lib/ampel_extase/jenkins_state_observer.rb, line 42
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/jenkins_state_observer.rb, line 38
def state_changed?(new_state)
  new_state != @build_state
end