Skip to content

Commit 1b1ee63

Browse files
shayonjhsbt
authored andcommitted
Handle for SIGTERM
1 parent a4454e6 commit 1b1ee63

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

lib/rake/application.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ def init(app_name="rake", argv = ARGV)
9494
# Backward compatibility for capistrano
9595
args = handle_options
9696
end
97+
98+
setup_signal_handling
9799
load_debug_at_stop_feature
98100
collect_command_line_tasks(args)
99101
end
@@ -850,5 +852,12 @@ def set_default_options # :nodoc:
850852
options.trace_rules = false
851853
end
852854

855+
def setup_signal_handling
856+
Signal.trap("TERM") do
857+
puts "SIGTERM received, exiting..."
858+
exit 128 + Signal.list["TERM"] # 143
859+
end
860+
end
861+
853862
end
854863
end

test/support/rakefile_definitions.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,4 +498,18 @@ def rakefile_stand_alone_filelist
498498
puts FL
499499
STAND_ALONE
500500
end
501+
502+
def rakefile_with_long_running_task
503+
rakefile <<-TEST_TASK
504+
require 'rake/testtask'
505+
506+
task :default => :test
507+
Rake::TestTask.new(:test) do |t|
508+
t.test_files = ['a_test.rb']
509+
end
510+
TEST_TASK
511+
open "a_test.rb", "w" do |io|
512+
io << "sleep 20"
513+
end
514+
end
501515
end

test/support/ruby_runner.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def run_ruby(option_list)
2121

2222
Open3.popen3(RUBY, *option_list) do |inn, out, err, wait|
2323
inn.close
24-
24+
@pid = wait.pid
2525
@exit = wait ? wait.value : $?
2626
@out = out.read
2727
@err = err.read

test/test_rake_functional.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,20 @@ def test_stand_alone_filelist
516516
assert_equal 0, @exit.exitstatus unless uncertain_exit_status?
517517
end
518518

519+
# Test that SIGTERM is handled gracefully
520+
def test_sigterm_handling
521+
if !jruby? && can_detect_signals?
522+
rakefile_with_long_running_task
523+
Thread.new { rake }
524+
sleep 0.5
525+
Process.kill("TERM", @pid)
526+
_, status = Process.wait2(@pid)
527+
assert_equal(143, status.exitstatus, "Process should exit with status 143")
528+
else
529+
omit "Signal detection seems broken on this system"
530+
end
531+
end
532+
519533
private
520534

521535
# We are unable to accurately verify that Rake returns a proper

0 commit comments

Comments
 (0)