Skip to content

Commit

Permalink
Handle for SIGTERM
Browse files Browse the repository at this point in the history
  • Loading branch information
shayonj committed Apr 29, 2024
1 parent ae1c787 commit 12c7795
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/rake/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ def init(app_name="rake", argv = ARGV)
# Backward compatibility for capistrano
args = handle_options
end

setup_signal_handling
load_debug_at_stop_feature
collect_command_line_tasks(args)
end
Expand Down Expand Up @@ -857,5 +859,12 @@ def set_default_options # :nodoc:
options.trace_rules = false
end

def setup_signal_handling
Signal.trap("TERM") do
puts "SIGTERM received, exiting..."
exit 143 # 128 + Signal.list["TERM"] (15)
end
end

end
end
14 changes: 14 additions & 0 deletions test/support/rakefile_definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -514,4 +514,18 @@ def rakefile_stand_alone_filelist
io << "puts FL\n"
end
end

def rakefile_with_long_running_task
rakefile <<-TEST_TASK
require 'rake/testtask'
task :default => :test
Rake::TestTask.new(:test) do |t|
t.test_files = ['a_test.rb']
end
TEST_TASK
open "a_test.rb", "w" do |io|
io << "sleep 20"
end
end
end
14 changes: 14 additions & 0 deletions test/test_rake_functional.rb
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,20 @@ def test_stand_alone_filelist
assert_equal 0, @exit.exitstatus unless uncertain_exit_status?
end

# Test that SIGTERM is handled gracefully
def test_sigterm_handling
if !jruby? && can_detect_signals?
rakefile_with_long_running_task
pid = Process.spawn("rake")
sleep 1
Process.kill("TERM", pid)
_, status = Process.wait2(pid)
assert_equal(143, status.exitstatus, "Process should exit with status 143")
else
omit "Signal detection seems broken on this system"
end
end

private

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

0 comments on commit 12c7795

Please sign in to comment.