Skip to content

Commit 1ef74b5

Browse files
author
Katyal
committed
added changes for graceful shutdown for agent in linux
cr https://code.amazon.com/reviews/CR-1313840
1 parent 3da1ae3 commit 1ef74b5

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

lib/instance_agent/plugins/codedeploy/command_poller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ def perform
8989
end
9090

9191
def graceful_shutdown
92-
log(:info, "Gracefully shutting down command execution threads now, will wait up to #{WAIT_FOR_ALL_THREADS_TIMEOUT_SECONDS} seconds")
92+
log(:info, "Gracefully shutting down agent child threads now, will wait up to #{WAIT_FOR_ALL_THREADS_TIMEOUT_SECONDS} seconds")
9393
# tell the pool to shutdown in an orderly fashion, allowing in progress work to complete
9494
@thread_pool.shutdown
9595
# now wait for all work to complete, wait till the timeout value
9696
# TODO: Make the timeout configurable in the agent configuration
9797
@thread_pool.wait_for_termination WAIT_FOR_ALL_THREADS_TIMEOUT_SECONDS
98-
log(:info, 'All command execution threads have been shut down')
98+
log(:info, 'All agent child threads have been shut down')
9999
end
100100

101101
def next_command

lib/instance_agent/runner/child.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# encoding: UTF-8
22
require 'process_manager/child'
3+
require 'thread'
34

45
module InstanceAgent
56
module Runner
@@ -38,6 +39,28 @@ def run
3839
runner.run
3940
end
4041
end
42+
43+
# Stops the master after recieving the kill signal
44+
# is overriden from ProcessManager::Daemon::Child
45+
def stop
46+
@runner.graceful_shutdown
47+
ProcessManager::Log.info('agent exiting now')
48+
super
49+
end
50+
51+
# Catches the trap signals and does a default or custom action
52+
# is overriden from ProcessManager::Daemon::Child
53+
def trap_signals
54+
[:INT, :QUIT, :TERM].each do |sig|
55+
trap(sig) do
56+
ProcessManager::Log.info "#{description}: Received #{sig} - setting internal shutting down flag and possibly finishing last run"
57+
stop_thread = Thread.new {stop}
58+
stop_thread.join
59+
end
60+
end
61+
# make sure we do not handle children like the master process
62+
trap(:CHLD, 'DEFAULT')
63+
end
4164

4265
def description
4366
if runner

lib/instance_agent/runner/master.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ def self.pid_file
2828
File.join(ProcessManager::Config.config[:pid_dir], "#{ProcessManager::Config.config[:program_name]}.pid")
2929
end
3030

31+
# Stops the master after recieving the kill signal
32+
# is overriden from ProcessManager::Daemon::Master
3133
def stop
3234
if (pid = self.class.find_pid)
3335
puts "Stopping #{description(pid)}"

lib/winagent.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def service_stop
5858
log(:info, 'stopping the agent')
5959
@polling_mutex.synchronize do
6060
@runner.graceful_shutdown
61-
log(:info, 'command execution threads shutdown, agent exiting now')
61+
log(:info, 'agent exiting now')
6262
end
6363
end
6464

0 commit comments

Comments
 (0)