Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1d68e91

Browse files
authored
Fix node cleanup (#135)
* Add expect_error to pg_upgrade* Fix node cleanup - rmdirs* Add cleanup parameter - clean full dir
1 parenta128b12 commit1d68e91

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

‎testgres/node.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -914,13 +914,14 @@ def free_port(self):
914914
self._should_free_port=False
915915
release_port(self.port)
916916

917-
defcleanup(self,max_attempts=3):
917+
defcleanup(self,max_attempts=3,full=False):
918918
"""
919919
Stop node if needed and remove its data/logs directory.
920920
NOTE: take a look at TestgresConfig.node_cleanup_full.
921921
922922
Args:
923923
max_attempts: how many times should we try to stop()?
924+
full: clean full base dir
924925
925926
Returns:
926927
This instance of :class:`.PostgresNode`.
@@ -929,12 +930,12 @@ def cleanup(self, max_attempts=3):
929930
self._try_shutdown(max_attempts)
930931

931932
# choose directory to be removed
932-
iftestgres_config.node_cleanup_full:
933+
iftestgres_config.node_cleanup_fullorfull:
933934
rm_dir=self.base_dir# everything
934935
else:
935936
rm_dir=self.data_dir# just data, save logs
936937

937-
self.os_ops.rmdirs(rm_dir,ignore_errors=True)
938+
self.os_ops.rmdirs(rm_dir,ignore_errors=False)
938939

939940
returnself
940941

@@ -1629,7 +1630,7 @@ def set_auto_conf(self, options, config='postgresql.auto.conf', rm_options={}):
16291630

16301631
self.os_ops.write(path,auto_conf,truncate=True)
16311632

1632-
defupgrade_from(self,old_node,options=None):
1633+
defupgrade_from(self,old_node,options=None,expect_error=False):
16331634
"""
16341635
Upgrade this node from an old node using pg_upgrade.
16351636
@@ -1657,11 +1658,11 @@ def upgrade_from(self, old_node, options=None):
16571658
"--old-datadir",old_node.data_dir,
16581659
"--new-datadir",self.data_dir,
16591660
"--old-port",str(old_node.port),
1660-
"--new-port",str(self.port),
1661+
"--new-port",str(self.port)
16611662
]
16621663
upgrade_command+=options
16631664

1664-
returnself.os_ops.exec_command(upgrade_command)
1665+
returnself.os_ops.exec_command(upgrade_command,expect_error=expect_error)
16651666

16661667
def_get_bin_path(self,filename):
16671668
ifself.bin_dir:

‎testgres/operations/local_ops.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
importstat
55
importsubprocess
66
importtempfile
7+
importtime
78

89
importpsutil
910

@@ -19,13 +20,18 @@
1920

2021
CMD_TIMEOUT_SEC=60
2122
error_markers= [b'error',b'Permission denied',b'fatal']
23+
err_out_markers= [b'Failure']
2224

2325

24-
defhas_errors(output):
26+
defhas_errors(output=None,error=None):
2527
ifoutput:
2628
ifisinstance(output,str):
2729
output=output.encode(get_default_encoding())
28-
returnany(markerinoutputformarkerinerror_markers)
30+
returnany(markerinoutputformarkerinerr_out_markers)
31+
iferror:
32+
ifisinstance(error,str):
33+
error=error.encode(get_default_encoding())
34+
returnany(markerinerrorformarkerinerror_markers)
2935
returnFalse
3036

3137

@@ -107,8 +113,8 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False,
107113
process,output,error=self._run_command(cmd,shell,input,stdin,stdout,stderr,get_process,timeout,encoding)
108114
ifget_process:
109115
returnprocess
110-
ifprocess.returncode!=0or(has_errors(error)andnotexpect_error):
111-
self._raise_exec_exception('Utility exited with non-zero code. Error `{}`',cmd,process.returncode,error)
116+
if(process.returncode!=0orhas_errors(output=output,error=error))andnotexpect_error:
117+
self._raise_exec_exception('Utility exited with non-zero code. Error `{}`',cmd,process.returncode,errororoutput)
112118

113119
ifverbose:
114120
returnprocess.returncode,output,error
@@ -142,8 +148,27 @@ def makedirs(self, path, remove_existing=False):
142148
exceptFileExistsError:
143149
pass
144150

145-
defrmdirs(self,path,ignore_errors=True):
146-
returnrmtree(path,ignore_errors=ignore_errors)
151+
defrmdirs(self,path,ignore_errors=True,retries=3,delay=1):
152+
"""
153+
Removes a directory and its contents, retrying on failure.
154+
155+
:param path: Path to the directory.
156+
:param ignore_errors: If True, ignore errors.
157+
:param retries: Number of attempts to remove the directory.
158+
:param delay: Delay between attempts in seconds.
159+
"""
160+
forattemptinrange(retries):
161+
try:
162+
rmtree(path,ignore_errors=ignore_errors)
163+
ifnotos.path.exists(path):
164+
returnTrue
165+
exceptFileNotFoundError:
166+
returnTrue
167+
exceptExceptionase:
168+
print(f"Error: Failed to remove directory{path} on attempt{attempt+1}:{e}")
169+
time.sleep(delay)
170+
print(f"Error: Failed to remove directory{path} after{retries} attempts.")
171+
returnFalse
147172

148173
deflistdir(self,path):
149174
returnos.listdir(path)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp