Skip to content

Commit 56955f8

Browse files
committed
Handle for SIGTERM
1 parent ae1c787 commit 56955f8

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

Diff for: lib/rake/application.rb

+14
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ def init(app_name="rake", argv = ARGV)
9494
# Backward compatibility for capistrano
9595
args = handle_options
9696
end
97+
98+
setup_signal_handling do
99+
load_debug_at_stop_feature
100+
collect_command_line_tasks(args)
101+
end
102+
97103
load_debug_at_stop_feature
98104
collect_command_line_tasks(args)
99105
end
@@ -857,5 +863,13 @@ def set_default_options # :nodoc:
857863
options.trace_rules = false
858864
end
859865

866+
def setup_signal_handling(&block)
867+
Signal.trap("TERM") do
868+
puts "SIGTERM received, starting cleanup..."
869+
yield
870+
exit 143 # 128 + Signal.list["TERM"] (15)
871+
end
872+
end
873+
860874
end
861875
end

Diff for: test/support/rakefile_definitions.rb

+14
Original file line numberDiff line numberDiff line change
@@ -514,4 +514,18 @@ def rakefile_stand_alone_filelist
514514
io << "puts FL\n"
515515
end
516516
end
517+
518+
def rakefile_with_long_running_task
519+
rakefile <<-TEST_TASK
520+
require 'rake/testtask'
521+
522+
task :default => :test
523+
Rake::TestTask.new(:test) do |t|
524+
t.test_files = ['a_test.rb']
525+
end
526+
TEST_TASK
527+
open "a_test.rb", "w" do |io|
528+
io << "sleep 20"
529+
end
530+
end
517531
end

Diff for: test/test_rake_functional.rb

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require File.expand_path("../helper", __FILE__)
33
require "fileutils"
44
require "open3"
5+
require "stringio"
56

67
class TestRakeFunctional < Rake::TestCase # :nodoc:
78
include RubyRunner
@@ -516,6 +517,18 @@ def test_stand_alone_filelist
516517
assert_equal 0, @exit.exitstatus unless uncertain_exit_status?
517518
end
518519

520+
# Test that SIGTERM is handled gracefully
521+
def test_sigterm_handling
522+
rakefile_with_long_running_task
523+
pid = Process.spawn('rake')
524+
Process.detach(pid)
525+
sleep 1
526+
Process.kill("TERM", pid)
527+
_, status = Process.wait2(pid)
528+
529+
assert_equal(143, status.exitstatus, "Process should exit with status 130")
530+
end
531+
519532
private
520533

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

0 commit comments

Comments
 (0)