MCPcopy Create free account
hub / github.com/MagicStack/asyncpg / Cluster

Class Cluster

asyncpg/cluster.py:77–620  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

75
76
77class Cluster:
78 def __init__(self, data_dir, *, pg_config_path=None):
79 self._data_dir = data_dir
80 self._pg_config_path = pg_config_path
81 self._pg_bin_dir = (
82 os.environ.get('PGINSTALLATION')
83 or os.environ.get('PGBIN')
84 )
85 self._pg_ctl = None
86 self._daemon_pid = None
87 self._daemon_process = None
88 self._connection_addr = None
89 self._connection_spec_override = None
90
91 def get_pg_version(self):
92 return self._pg_version
93
94 def is_managed(self):
95 return True
96
97 def get_data_dir(self):
98 return self._data_dir
99
100 def get_status(self):
101 if self._pg_ctl is None:
102 self._init_env()
103
104 process = subprocess.run(
105 [self._pg_ctl, 'status', '-D', self._data_dir],
106 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
107 stdout, stderr = process.stdout, process.stderr
108
109 if (process.returncode == 4 or not os.path.exists(self._data_dir) or
110 not os.listdir(self._data_dir)):
111 return 'not-initialized'
112 elif process.returncode == 3:
113 return 'stopped'
114 elif process.returncode == 0:
115 r = re.match(r'.*PID\s?:\s+(\d+).*', stdout.decode())
116 if not r:
117 raise ClusterError(
118 'could not parse pg_ctl status output: {}'.format(
119 stdout.decode()))
120 self._daemon_pid = int(r.group(1))
121 return self._test_connection(timeout=0)
122 else:
123 raise ClusterError(
124 'pg_ctl status exited with status {:d}: {}'.format(
125 process.returncode, stderr))
126
127 async def connect(self, loop=None, **kwargs):
128 conn_info = self.get_connection_spec()
129 conn_info.update(kwargs)
130 return await asyncpg.connect(loop=loop, **conn_info)
131
132 def init(self, **settings):
133 """Initialize cluster."""
134 if self.get_status() != 'not-initialized':

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…