summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/chef/application.rb4
-rw-r--r--lib/chef/application/client.rb7
-rw-r--r--spec/unit/application/client_spec.rb27
3 files changed, 32 insertions, 6 deletions
diff --git a/lib/chef/application.rb b/lib/chef/application.rb
index 0ed8d3dd9f..8fe8b87963 100644
--- a/lib/chef/application.rb
+++ b/lib/chef/application.rb
@@ -34,10 +34,6 @@ class Chef::Application
@chef_client = nil
@chef_client_json = nil
- trap("TERM") do
- Chef::Application.fatal!("SIGTERM received, stopping", 1)
- end
-
trap("INT") do
Chef::Application.fatal!("SIGINT received, stopping", 2)
end
diff --git a/lib/chef/application/client.rb b/lib/chef/application/client.rb
index 393ff29834..45a4ac3063 100644
--- a/lib/chef/application/client.rb
+++ b/lib/chef/application/client.rb
@@ -274,6 +274,12 @@ class Chef::Application::Client < Chef::Application
Chef::Log.info("SIGUSR1 received, waking up")
SELF_PIPE[1].putc('.') # wakeup master process from select
end
+
+ trap("TERM") do
+ Chef::Log.info("SIGTERM received, exiting gracefully")
+ @exit_gracefully = true
+ SELF_PIPE[1].putc('.')
+ end
end
if Chef::Config[:version]
@@ -286,6 +292,7 @@ class Chef::Application::Client < Chef::Application
loop do
begin
+ Chef::Application.exit!("Exiting", 0) if @exit_gracefully
if Chef::Config[:splay]
splay = rand Chef::Config[:splay]
Chef::Log.debug("Splay sleep #{splay} seconds")
diff --git a/spec/unit/application/client_spec.rb b/spec/unit/application/client_spec.rb
index 04a86bee8d..e894b8f702 100644
--- a/spec/unit/application/client_spec.rb
+++ b/spec/unit/application/client_spec.rb
@@ -19,7 +19,7 @@ require 'spec_helper'
describe Chef::Application::Client, "reconfigure" do
before do
- @original_config = Chef::Config.configuration
+ @original_config = Chef::Config.configuration.dup
@app = Chef::Application::Client.new
@app.stub!(:configure_opt_parser).and_return(true)
@@ -148,4 +148,27 @@ describe Chef::Application::Client, "configure_chef" do
Chef::Config[:color].should be_true
end
end
-end \ No newline at end of file
+end
+
+describe Chef::Application::Client, "run_application", :unix_only do
+ before do
+ @pipe = IO.pipe
+ @app = Chef::Application::Client.new
+ @app.stub(:run_chef_client) do
+ @pipe[1].puts 'started'
+ sleep 1
+ @pipe[1].puts 'finished'
+ end
+ end
+
+ it "should exit gracefully when sent SIGTERM" do
+ pid = fork do
+ @app.run_application
+ end
+ @pipe[0].gets.should == "started\n"
+ Process.kill("TERM", pid)
+ Process.wait
+ IO.select([@pipe[0]], nil, nil, 0).should_not be_nil
+ @pipe[0].gets.should == "finished\n"
+ end
+end