Skip to content

Commit

Permalink
Merge pull request #16 from RodrigoMNardi/feature/watchog_build_status
Browse files Browse the repository at this point in the history
WatchDog - Change to use build status
  • Loading branch information
RodrigoMNardi authored Nov 2, 2023
2 parents 9908a02 + 19d16cc commit ae629ed
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
4 changes: 4 additions & 0 deletions workers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def fetch_ci_execution(check_suite)
@result = get_status(check_suite.bamboo_ci_ref)
end

def fetch_build_status(check_suite)
get_request(URI("https://127.0.0.1/rest/api/latest/result/status/#{check_suite.bamboo_ci_ref}"))
end

def check_stages
@result.dig('stages', 'stage').each do |stage|
stage.dig('results', 'result').each do |result|
Expand Down
40 changes: 35 additions & 5 deletions workers/watch_dog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,52 @@
class WatchDog < Base
def perform
@logger = Logger.new('watch_dog.log', 0, 1_024_000)
check_suites.each do |check_suite|
@logger.info '>>> Running watchdog'

suites = check_suites

@logger.info ">>> Suites that need to be updated: #{suites.size}"

check(suites)

@logger.info '>>> Stopping watchdog'
end

private

def check(suites)
suites.each do |check_suite|
@logger.info ">>> CheckSuite: #{check_suite.inspect}"

fetch_ci_execution(check_suite)
build_status = fetch_build_status(check_suite)

@logger.info ">>> Status: #{@result['state']}"
@logger.info ">>> Build status: #{build_status.inspect}"

next if @result['status-code'] == 404
next if @result['state'] == 'Unknown'
next if in_progress?(build_status)

@logger.info ">>> Updating suite: #{check_suite.inspect}"
check_stages(check_suite)
clear_deleted_jobs(check_suite)
end
end

private
def in_progress?(build_status)
return false if ci_stopped?(build_status)
return false if ci_hanged?(build_status)

true
end

def ci_stopped?(build_status)
build_status.key?('message') and !build_status.key? 'finished'
end

def ci_hanged?(build_status)
return false if build_status.key?('message') and !build_status.key? 'finished'

build_status.dig('progress', 'percentageCompleted').to_f >= 2.0
end

def check_suites
CheckSuite.where(id: check_suites_fetch_map)
Expand Down

0 comments on commit ae629ed

Please sign in to comment.