forked from FRRouting/netdef-ci-github-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request FRRouting#101 from RodrigoMNardi/feature/github/ca…
…ncel_by_timer Execution Hanging Detection
- Loading branch information
Showing
9 changed files
with
189 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# SPDX-License-Identifier: BSD-2-Clause | ||
# | ||
# ci_job_spec.rb | ||
# Part of NetDEF CI System | ||
# | ||
# Copyright (c) 2023 by | ||
# Network Device Education Foundation, Inc. ("NetDEF") | ||
# | ||
# frozen_string_literal: true | ||
|
||
describe CheckSuite do | ||
context '#execution_started?' do | ||
let(:check_suite) { create(:check_suite) } | ||
let(:check_suite_running) { create(:check_suite, :with_in_progress) } | ||
|
||
it 'returns true when there are less than 2 jobs in progress' do | ||
expect(check_suite.execution_started?).to be_truthy | ||
end | ||
|
||
it 'returns false' do | ||
expect(check_suite_running.execution_started?).to be_falsey | ||
end | ||
end | ||
|
||
context '#last_job_updated_at_timer? -> success' do | ||
let(:ci_job) { create(:ci_job) } | ||
let(:check_suite) { create(:check_suite, ci_jobs: [ci_job]) } | ||
|
||
it 'returns false' do | ||
expect(check_suite.last_job_updated_at_timer).not_to be_nil | ||
end | ||
end | ||
|
||
context '#last_job_updated_at_timer? -> error' do | ||
let(:ci_job) { create(:ci_job, updated_at: nil) } | ||
let(:check_suite) { create(:check_suite, ci_jobs: []) } | ||
|
||
it 'returns false' do | ||
expect(check_suite.last_job_updated_at_timer).to be_nil | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# SPDX-License-Identifier: BSD-2-Clause | ||
# | ||
# slack_bot_spec.rb | ||
# Part of NetDEF CI System | ||
# | ||
# Copyright (c) 2023 by | ||
# Network Device Education Foundation, Inc. ("NetDEF") | ||
# | ||
# frozen_string_literal: true | ||
|
||
describe TimeoutExecution do | ||
let(:timeout_execution) { described_class.instance } | ||
let(:finished_instance) { Github::PlanExecution::Finished.new({}) } | ||
|
||
before do | ||
allow(Github::PlanExecution::Finished).to receive(:new).and_return(finished_instance) | ||
end | ||
|
||
context 'when timeout is called, but still running' do | ||
let(:check_suite) { create(:check_suite) } | ||
|
||
before do | ||
allow(CheckSuite).to receive(:find).and_return(check_suite) | ||
allow(check_suite).to receive(:finished?).and_return(true) | ||
end | ||
|
||
it 'calls timeout job' do | ||
expect(described_class.timeout(check_suite.id)).to be_falsey | ||
end | ||
end | ||
|
||
context 'when timeout is called, but hanged' do | ||
let(:check_suite) { create(:check_suite) } | ||
|
||
before do | ||
allow(CheckSuite).to receive(:find).and_return(check_suite) | ||
allow(check_suite).to receive(:finished?).and_return(false) | ||
allow(check_suite).to receive(:last_job_updated_at_timer).and_return(Time.now.utc - 3.hours) | ||
allow(finished_instance).to receive(:finished).and_return([200, 'Finished']) | ||
end | ||
|
||
it 'calls timeout job' do | ||
expect(described_class.timeout(check_suite.id)).to be_truthy | ||
end | ||
end | ||
|
||
context 'when timeout is called and rescheduled' do | ||
let(:check_suite) { create(:check_suite) } | ||
|
||
before do | ||
allow(CheckSuite).to receive(:find).and_return(check_suite) | ||
allow(check_suite).to receive(:finished?).and_return(false) | ||
allow(check_suite).to receive(:last_job_updated_at_timer).and_return(Time.now.utc + 3.hours) | ||
allow(TimeoutExecution).to receive_message_chain(:delay, :timeout).and_return(true) | ||
end | ||
|
||
it 'calls timeout job' do | ||
expect(described_class.timeout(check_suite.id)).to be_falsey | ||
end | ||
end | ||
|
||
context 'when timeout is called, last update in 2 hour ago' do | ||
let(:check_suite) { create(:check_suite) } | ||
|
||
before do | ||
allow(CheckSuite).to receive(:find).and_return(check_suite) | ||
allow(check_suite).to receive(:finished?).and_return(false) | ||
allow(check_suite).to receive(:last_job_updated_at_timer).and_return(Time.now.utc - 3.hours) | ||
end | ||
|
||
it 'calls timeout job' do | ||
expect(TimeoutExecution).to receive(:timeout) | ||
expect(described_class.timeout(check_suite.id)).to be_falsey | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# SPDX-License-Identifier: BSD-2-Clause | ||
# | ||
# ci_job_status.rb | ||
# Part of NetDEF CI System | ||
# | ||
# Copyright (c) 2024 by | ||
# Network Device Education Foundation, Inc. ("NetDEF") | ||
# | ||
# frozen_string_literal: true | ||
|
||
require_relative '../config/setup' | ||
|
||
class TimeoutExecution | ||
class << self | ||
def timeout(check_suite_id) | ||
@logger = GithubLogger.instance.create('timeout_execution_worker.log', Logger::INFO) | ||
check_suite = CheckSuite.find(check_suite_id) | ||
|
||
@logger.info("Timeout execution for check_suite_id: #{check_suite_id} -> finished? #{check_suite.finished?}") | ||
|
||
return false if check_suite.finished? | ||
return rescheduling([], check_suite_id) if check_suite.last_job_updated_at_timer > 2.hour.ago.utc | ||
|
||
@logger.info("Calling Github::PlanExecution::Finished.new(#{check_suite.bamboo_ci_ref}).finished") | ||
|
||
rescheduling(finished(check_suite), check_suite_id) | ||
end | ||
|
||
def finished(check_suite) | ||
Github::PlanExecution::Finished | ||
.new({ 'bamboo_ref' => check_suite.bamboo_ci_ref, hanged: true }) | ||
.finished | ||
end | ||
|
||
def rescheduling(resp, check_suite_id) | ||
return true if resp == [200, 'Finished'] | ||
|
||
@logger.info("Rescheduling check_suite_id: #{check_suite_id}") | ||
|
||
Delayed::Job.where('handler LIKE ?', "%TimeoutExecution%args%-%#{check_suite_id}%").delete_all | ||
|
||
TimeoutExecution | ||
.delay(run_at: 2.hours.from_now.utc, queue: 'timeout_execution') | ||
.timeout(check_suite_id) | ||
|
||
false | ||
end | ||
end | ||
end |