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

Commit04f0dd1

Browse files
committed
introduce clean_on_error() for safer node handling
1 parent06ce6e2 commit04f0dd1

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

‎testgres/backup.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
from .utilsimport \
2323
get_bin_path, \
24-
execute_utility
24+
execute_utility, \
25+
clean_on_error
2526

2627

2728
classNodeBackup(object):
@@ -56,8 +57,8 @@ def __init__(self,
5657
try:
5758
xlog_method=XLogMethod(xlog_method)
5859
exceptValueError:
59-
raiseBackupException(
60-
'Invalid xlog_method "{}"'.format(xlog_method))
60+
msg='Invalid xlog_method "{}"'.format(xlog_method)
61+
raiseBackupException(msg)
6162

6263
# Set default arguments
6364
username=usernameordefault_username()
@@ -144,15 +145,16 @@ def spawn_primary(self, name=None, destroy=True):
144145

145146
# Build a new PostgresNode
146147
from .nodeimportPostgresNode
147-
node=PostgresNode(name=name,base_dir=base_dir)
148+
withclean_on_error(PostgresNode(name=name,
149+
base_dir=base_dir))asnode:
148150

149-
# New nodes should always remove dir tree
150-
node._should_rm_dirs=True
151+
# New nodes should always remove dir tree
152+
node._should_rm_dirs=True
151153

152-
node.append_conf(PG_CONF_FILE,"\n")
153-
node.append_conf(PG_CONF_FILE,"port = {}".format(node.port))
154+
node.append_conf(PG_CONF_FILE,"\n")
155+
node.append_conf(PG_CONF_FILE,"port = {}".format(node.port))
154156

155-
returnnode
157+
returnnode
156158

157159
defspawn_replica(self,name=None,destroy=True,slot=None):
158160
"""
@@ -168,13 +170,14 @@ def spawn_replica(self, name=None, destroy=True, slot=None):
168170
"""
169171

170172
# Build a new PostgresNode
171-
node=self.spawn_primary(name=name,destroy=destroy)
173+
withclean_on_error(self.spawn_primary(name=name,
174+
destroy=destroy))asnode:
172175

173-
# Assign it a master and a recovery file (private magic)
174-
node._assign_master(self.original_node)
175-
node._create_recovery_conf(username=self.username,slot=slot)
176+
# Assign it a master and a recovery file (private magic)
177+
node._assign_master(self.original_node)
178+
node._create_recovery_conf(username=self.username,slot=slot)
176179

177-
returnnode
180+
returnnode
178181

179182
defcleanup(self):
180183
ifself._available:

‎testgres/node.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
pg_version_ge, \
6262
reserve_port, \
6363
release_port, \
64-
execute_utility
64+
execute_utility, \
65+
clean_on_error
6566

6667
from .backupimportNodeBackup
6768

@@ -979,10 +980,9 @@ def replicate(self, name=None, slot=None, **kwargs):
979980
base_dir: the base directory for data files and logs
980981
"""
981982

982-
backup=self.backup(**kwargs)
983-
984983
# transform backup into a replica
985-
returnbackup.spawn_replica(name=name,destroy=True,slot=slot)
984+
withclean_on_error(self.backup(**kwargs))asbackup:
985+
returnbackup.spawn_replica(name=name,destroy=True,slot=slot)
986986

987987
defcatchup(self,dbname=None,username=None):
988988
"""

‎testgres/utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
importsubprocess
1010
importsys
1111

12+
fromcontextlibimportcontextmanager
1213
fromdistutils.versionimportLooseVersion
1314

1415
from .configimporttestgres_config
@@ -217,4 +218,23 @@ def file_tail(f, num_lines):
217218

218219

219220
defeprint(*args,**kwargs):
221+
"""
222+
Print stuff to stderr.
223+
"""
224+
220225
print(*args,file=sys.stderr,**kwargs)
226+
227+
228+
@contextmanager
229+
defclean_on_error(node):
230+
"""
231+
Context manager to wrap PostgresNode and such.
232+
Calls cleanup() method when underlying code raises an exception.
233+
"""
234+
235+
try:
236+
yieldnode
237+
exceptException:
238+
# TODO: should we wrap this in try-block?
239+
node.cleanup()
240+
raise

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp