@@ -14,8 +14,9 @@ def log_contents
1414 end
1515
1616 let ( :pid ) { 23_456 }
17+ let ( :pid_name ) { 'pidfile' }
1718 let ( :pid_dir ) { Dir . mktmpdir }
18- let ( :pid_path ) { File . join ( pid_dir , 'pidfile' ) }
19+ let ( :pid_path ) { File . join ( pid_dir , pid_name ) }
1920
2021 before do
2122 File . write ( pid_path , pid )
@@ -38,7 +39,7 @@ def log_contents
3839 drain . shutdown_nginx ( pid_path )
3940
4041 log_contents do |log |
41- expect ( log ) . to match ( / Sending signal '\w + ' to process '\w + ' with pid '\d +'/ )
42+ expect ( log ) . to include ( " Sending signal 'QUIT ' to process '#{ pid_name } ' with pid '#{ pid } '" )
4243 end
4344 end
4445
@@ -50,8 +51,9 @@ def log_contents
5051
5152 expect ( drain ) . to have_received ( :sleep ) . twice
5253 log_contents do |log |
53- expect ( log ) . to match ( /Waiting \d +s for process '\w +' with pid '\d +' to shutdown/ )
54- expect ( log ) . to match ( /Process '\w +' with pid '\d +' is not running/ )
54+ expect ( log ) . to include ( "Waiting 30s for process '#{ pid_name } ' with pid '#{ pid } ' to shutdown" )
55+ expect ( log ) . to include ( "Waiting 29s for process '#{ pid_name } ' with pid '#{ pid } ' to shutdown" )
56+ expect ( log ) . to include ( "Process '#{ pid_name } ' with pid '#{ pid } ' is not running" )
5557 end
5658 end
5759
@@ -84,7 +86,7 @@ def log_contents
8486
8587 expect ( drain ) . to have_received ( :sleep ) . exactly ( 40 ) . times
8688 log_contents do |log |
87- expect ( log ) . to match ( / Process '\w + ' with pid '\d + ' is still running - this indicates an error in the shutdown procedure!/ )
89+ expect ( log ) . to include ( " Process '#{ pid_name } ' with pid '#{ pid } ' is still running - this indicates an error in the shutdown procedure!" )
8890 end
8991 end
9092 end
@@ -96,7 +98,7 @@ def log_contents
9698 drain . shutdown_cc ( pid_path )
9799
98100 log_contents do |log |
99- expect ( log ) . to match ( / Sending signal '\w + ' to process '\w + ' with pid '\d +'/ )
101+ expect ( log ) . to match ( " Sending signal 'TERM ' to process '#{ pid_name } ' with pid '#{ pid } '" )
100102 end
101103 end
102104
@@ -107,7 +109,53 @@ def log_contents
107109
108110 expect ( drain ) . to have_received ( :sleep ) . exactly ( 20 ) . times
109111 log_contents do |log |
110- expect ( log ) . to match ( /Process '\w +' with pid '\d +' is still running - this indicates an error in the shutdown procedure!/ )
112+ expect ( log ) . to match ( "Process '#{ pid_name } ' with pid '#{ pid } ' is still running - this indicates an error in the shutdown procedure!" )
113+ end
114+ end
115+ end
116+
117+ describe '#shutdown_delayed_worker' do
118+ it 'sends TERM to the delayed worker process specified in the pid file' do
119+ expect ( Process ) . to receive ( :kill ) . with ( 'TERM' , pid )
120+
121+ drain . shutdown_delayed_worker ( pid_path )
122+
123+ log_contents do |log |
124+ expect ( log ) . to include ( "Sending signal 'TERM' to process '#{ pid_name } ' with pid '#{ pid } '" )
125+ end
126+ end
127+
128+ it 'waits 15s after sending TERM' do
129+ allow ( Process ) . to receive ( :getpgid ) . with ( pid ) . and_return ( 1 )
130+
131+ drain . shutdown_delayed_worker ( pid_path )
132+
133+ expect ( drain ) . to have_received ( :sleep ) . exactly ( 15 ) . times
134+ log_contents do |log |
135+ expect ( log ) . to include ( "Process '#{ pid_name } ' with pid '#{ pid } ' is still running - this indicates an error in the shutdown procedure!" )
136+ end
137+ end
138+
139+ it 'waits for the given timeout after sending TERM' do
140+ allow ( Process ) . to receive ( :getpgid ) . with ( pid ) . and_return ( 1 )
141+
142+ drain . shutdown_delayed_worker ( pid_path , 30 )
143+
144+ expect ( drain ) . to have_received ( :sleep ) . exactly ( 30 ) . times
145+ log_contents do |log |
146+ expect ( log ) . to include ( "Process '#{ pid_name } ' with pid '#{ pid } ' is still running - this indicates an error in the shutdown procedure!" )
147+ end
148+ end
149+
150+ it 'sends KILL to the delayed worker process if it is still running after 15s' do
151+ allow ( Process ) . to receive ( :getpgid ) . with ( pid ) . and_return ( 1 )
152+ allow ( Process ) . to receive ( :kill ) . with ( 'TERM' , pid )
153+ expect ( Process ) . to receive ( :kill ) . with ( 'KILL' , pid )
154+
155+ drain . shutdown_delayed_worker ( pid_path )
156+
157+ log_contents do |log |
158+ expect ( log ) . to include ( "Forcefully shutting down process '#{ pid_name } ' with pid '#{ pid } '" )
111159 end
112160 end
113161 end
0 commit comments