Skip to content

Commit 98b5370

Browse files
jakduchadamruzicka
authored andcommitted
Fixes #38238 - Mark timed out Ansible jobs as failed
Assisted-By: Codex 5.6 Sol High
1 parent 969ffff commit 98b5370

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

‎lib/smart_proxy_ansible/runner/ansible_runner.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def run_refresh_output
4848

4949
def timeout
5050
logger.debug('job timed out')
51+
broadcast_data('Timeout for execution passed, stopping the job', 'stderr')
5152
super
5253
end
5354

@@ -57,9 +58,8 @@ def timeout_interval
5758

5859
def kill
5960
::Process.kill('SIGTERM', @process_manager.pid)
61+
@inventory['all']['hosts'].each_key { |hostname| @exit_statuses[hostname] = 2 }
6062
publish_exit_status(2)
61-
@inventory['all']['hosts'].each { |hostname| @exit_statuses[hostname] = 2 }
62-
broadcast_data('Timeout for execution passed, stopping the job', 'stderr')
6363
close
6464
end
6565

‎test/ansible_runner_test.rb‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,39 @@ def setup
2828
end
2929
end
3030

31+
describe '#kill' do
32+
let(:runner) { ::Proxy::Ansible::Runner::AnsibleRunner.allocate }
33+
let(:hosts) { ['host1.example.com', 'host2.example.com'] }
34+
35+
test 'marks every host failed before publishing exit statuses' do
36+
expected_exit_statuses = hosts.to_h { |host| [host, 2] }
37+
runner.instance_variable_set(:@process_manager, stub(:pid => 1234))
38+
runner.instance_variable_set(:@inventory, { 'all' => { 'hosts' => hosts.to_h { |host| [host, {}] } } })
39+
runner.instance_variable_set(:@exit_statuses, hosts.to_h { |host| [host, 0] })
40+
41+
::Process.expects(:kill).with('SIGTERM', 1234)
42+
runner.expects(:publish_exit_status).with do |status|
43+
status == 2 && runner.instance_variable_get(:@exit_statuses) == expected_exit_statuses
44+
end
45+
runner.expects(:broadcast_data).never
46+
runner.expects(:close)
47+
48+
runner.kill
49+
end
50+
end
51+
52+
describe '#timeout' do
53+
let(:runner) { ::Proxy::Ansible::Runner::AnsibleRunner.allocate }
54+
55+
test 'broadcasts the timeout message before killing the job' do
56+
runner.stubs(:logger).returns(stub(:debug => nil))
57+
runner.expects(:broadcast_data).with('Timeout for execution passed, stopping the job', 'stderr')
58+
runner.expects(:kill)
59+
60+
runner.timeout
61+
end
62+
end
63+
3164
describe '#rebuild_secrets' do
3265
let(:inventory) { { 'all' => { 'hosts' => { 'foreman.example.com' => {} } } } }
3366
let(:host_secrets) { { 'ansible_password' => 'letmein', 'ansible_become_password' => 'iamroot' } }

0 commit comments

Comments
 (0)