diff --git a/workers/base.rb b/workers/base.rb index b397c61..d9acfbb 100644 --- a/workers/base.rb +++ b/workers/base.rb @@ -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| diff --git a/workers/watch_dog.rb b/workers/watch_dog.rb index f537e5a..1653e14 100644 --- a/workers/watch_dog.rb +++ b/workers/watch_dog.rb @@ -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)