Skip to content

Commit 636d0f9

Browse files
authored
Merge branch 'master' into unique_system_ids_for_cached_initdb
2 parents c2f0156 + 5bd8d48 commit 636d0f9

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

testgres/backup.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
from .consts import \
1010
DATA_DIR, \
11+
TMP_NODE, \
12+
TMP_BACKUP, \
1113
PG_CONF_FILE, \
1214
BACKUP_LOG_FILE, \
1315
DEFAULT_XLOG_METHOD
@@ -49,7 +51,7 @@ def __init__(self,
4951

5052
# Set default arguments
5153
username = username or default_username()
52-
base_dir = base_dir or tempfile.mkdtemp()
54+
base_dir = base_dir or tempfile.mkdtemp(prefix=TMP_BACKUP)
5355

5456
# public
5557
self.original_node = node
@@ -96,7 +98,7 @@ def _prepare_dir(self, destroy):
9698
available = not destroy
9799

98100
if available:
99-
dest_base_dir = tempfile.mkdtemp()
101+
dest_base_dir = tempfile.mkdtemp(prefix=TMP_NODE)
100102

101103
data1 = os.path.join(self.base_dir, DATA_DIR)
102104
data2 = os.path.join(dest_base_dir, DATA_DIR)

testgres/config.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
from contextlib import contextmanager
99

10+
from .consts import TMP_CACHE
11+
1012

1113
class GlobalConfig(object):
1214
"""
@@ -103,6 +105,7 @@ def copy(self):
103105
config_stack = []
104106

105107

108+
@atexit.register
106109
def rm_cached_initdb_dirs():
107110
for d in cached_initdb_dirs:
108111
shutil.rmtree(d, ignore_errors=True)
@@ -163,8 +166,5 @@ def configure_testgres(**options):
163166
testgres_config.update(options)
164167

165168

166-
# NOTE: to be executed at exit()
167-
atexit.register(rm_cached_initdb_dirs)
168-
169169
# NOTE: assign initial cached dir for initdb
170-
testgres_config.cached_initdb_dir = tempfile.mkdtemp()
170+
testgres_config.cached_initdb_dir = tempfile.mkdtemp(prefix=TMP_CACHE)

testgres/consts.py

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
DATA_DIR = "data"
55
LOGS_DIR = "logs"
66

7+
# prefixes for temp dirs
8+
TMP_NODE = 'tgsn_'
9+
TMP_DUMP = 'tgsd_'
10+
TMP_CACHE = 'tgsc_'
11+
TMP_BACKUP = 'tgsb_'
12+
713
# path to control file
814
XLOG_CONTROL_FILE = "global/pg_control"
915

testgres/node.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
from .consts import \
2424
DATA_DIR, \
2525
LOGS_DIR, \
26+
TMP_NODE, \
27+
TMP_DUMP, \
2628
PG_CONF_FILE, \
2729
PG_AUTO_CONF_FILE, \
2830
HBA_CONF_FILE, \
@@ -106,7 +108,9 @@ def __enter__(self):
106108
def __exit__(self, type, value, traceback):
107109
self.free_port()
108110

109-
got_exception = value is not None
111+
# NOTE: ctrl+C does not count!
112+
got_exception = type is not None and type != KeyboardInterrupt
113+
110114
c1 = self.cleanup_on_good_exit and not got_exception
111115
c2 = self.cleanup_on_bad_exit and got_exception
112116

@@ -152,8 +156,9 @@ def _try_shutdown(self, max_attempts):
152156
except ExecUtilException:
153157
pass # one more time
154158
except Exception:
155-
# TODO: probably kill stray instance
159+
# TODO: probably should kill stray instance
156160
eprint('cannot stop node {}'.format(self.name))
161+
break
157162

158163
attempts += 1
159164

@@ -195,7 +200,7 @@ def _create_recovery_conf(self, username):
195200

196201
def _prepare_dirs(self):
197202
if not self.base_dir:
198-
self.base_dir = tempfile.mkdtemp()
203+
self.base_dir = tempfile.mkdtemp(prefix=TMP_NODE)
199204

200205
if not os.path.exists(self.base_dir):
201206
os.makedirs(self.base_dir)
@@ -713,7 +718,7 @@ def dump(self, filename=None, dbname=None, username=None):
713718
"""
714719

715720
def tmpfile():
716-
fd, fname = tempfile.mkstemp()
721+
fd, fname = tempfile.mkstemp(prefix=TMP_DUMP)
717722
os.close(fd)
718723
return fname
719724

0 commit comments

Comments
 (0)