Skip to content

Commit 78603f8

Browse files
authored
Fix node.py: try_shutdown and cleanup (#144)
1 parent 98b0286 commit 78603f8

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

Diff for: testgres/node.py

+30-29
Original file line numberDiff line numberDiff line change
@@ -334,37 +334,38 @@ def _try_shutdown(self, max_attempts, with_force=False):
334334
attempts = 0
335335
node_pid = self.pid
336336

337-
# try stopping server N times
338-
while attempts < max_attempts:
339-
try:
340-
self.stop()
341-
break # OK
342-
except ExecUtilException:
343-
pass # one more time
344-
except Exception:
345-
eprint('cannot stop node {}'.format(self.name))
346-
break
347-
348-
attempts += 1
349-
350-
# If force stopping is enabled and PID is valid
351-
if with_force and node_pid != 0:
352-
# If we couldn't stop the node
353-
p_status_output = self.os_ops.exec_command(cmd=f'ps -p {node_pid}', shell=True).decode('utf-8')
354-
if self.status() != NodeStatus.Stopped and p_status_output and str(node_pid) in p_status_output:
337+
if node_pid > 0:
338+
# try stopping server N times
339+
while attempts < max_attempts:
355340
try:
356-
eprint(f'Force stopping node {self.name} with PID {node_pid}')
357-
self.os_ops.kill(node_pid, signal.SIGKILL, expect_error=False)
341+
self.stop()
342+
break # OK
343+
except ExecUtilException:
344+
pass # one more time
358345
except Exception:
359-
# The node has already stopped
360-
pass
361-
362-
# Check that node stopped - print only column pid without headers
363-
p_status_output = self.os_ops.exec_command(f'ps -o pid= -p {node_pid}', shell=True, ignore_errors=True).decode('utf-8')
364-
if p_status_output and str(node_pid) in p_status_output:
365-
eprint(f'Failed to stop node {self.name}.')
366-
else:
367-
eprint(f'Node {self.name} has been stopped successfully.')
346+
eprint('cannot stop node {}'.format(self.name))
347+
break
348+
349+
attempts += 1
350+
351+
# If force stopping is enabled and PID is valid
352+
if with_force and node_pid != 0:
353+
# If we couldn't stop the node
354+
p_status_output = self.os_ops.exec_command(cmd=f'ps -p {node_pid}', shell=True).decode('utf-8')
355+
if self.status() != NodeStatus.Stopped and p_status_output and str(node_pid) in p_status_output:
356+
try:
357+
eprint(f'Force stopping node {self.name} with PID {node_pid}')
358+
self.os_ops.kill(node_pid, signal.SIGKILL, expect_error=False)
359+
except Exception:
360+
# The node has already stopped
361+
pass
362+
363+
# Check that node stopped - print only column pid without headers
364+
p_status_output = self.os_ops.exec_command(f'ps -o pid= -p {node_pid}', shell=True, ignore_errors=True).decode('utf-8')
365+
if p_status_output and str(node_pid) in p_status_output:
366+
eprint(f'Failed to stop node {self.name}.')
367+
else:
368+
eprint(f'Node {self.name} has been stopped successfully.')
368369

369370
def _assign_master(self, master):
370371
"""NOTE: this is a private method!"""

0 commit comments

Comments
 (0)