Skip to content

Commit 6edeecf

Browse files
committed
fix socket vs. tcp connection
1 parent 7d865bc commit 6edeecf

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

src/charm.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def unit_peer_data(self) -> Dict:
104104
def _database_config(self):
105105
"""Returns the database config to use to connect to the MySQL cluster."""
106106
data = list(self.database.fetch_relation_data().values())[0]
107+
107108
username, password, endpoints = (
108109
data.get("username"),
109110
data.get("password"),
@@ -119,7 +120,6 @@ def _database_config(self):
119120
}
120121
if endpoints.startswith("file://"):
121122
config["unix_socket"] = endpoints[7:]
122-
config["port"] = "socket"
123123
else:
124124
host, port = endpoints.split(":")
125125
config["host"] = host
@@ -138,20 +138,24 @@ def _start_continuous_writes(self, starting_number: int) -> None:
138138

139139
self._stop_continuous_writes()
140140

141+
command = [
142+
"/usr/bin/python3",
143+
"src/continuous_writes.py",
144+
self._database_config["user"],
145+
self._database_config["password"],
146+
self._database_config["database"],
147+
CONTINUOUS_WRITE_TABLE_NAME,
148+
str(starting_number),
149+
]
150+
151+
if "unix_socket" in self._database_config:
152+
command.append(self._database_config["unix_socket"])
153+
else:
154+
command.append(self._database_config["host"])
155+
command.append(self._database_config["port"])
156+
141157
# Run continuous writes in the background
142-
proc = subprocess.Popen(
143-
[
144-
"/usr/bin/python3",
145-
"src/continuous_writes.py",
146-
self._database_config["user"],
147-
self._database_config["password"],
148-
self._database_config.get("host", self._database_config["unix_socket"]),
149-
self._database_config["port"],
150-
self._database_config["database"],
151-
CONTINUOUS_WRITE_TABLE_NAME,
152-
str(starting_number),
153-
]
154-
)
158+
proc = subprocess.Popen(command)
155159

156160
# Store the continuous writes process id in stored state to be able to stop it later
157161
self.unit_peer_data[PROC_PID_KEY] = str(proc.pid)
@@ -270,6 +274,9 @@ def _on_stop_continuous_writes_action(self, event: ActionEvent) -> None:
270274

271275
def _on_database_created(self, _) -> None:
272276
"""Handle the database created event."""
277+
if not self._database_config:
278+
return
279+
273280
self._start_continuous_writes(1)
274281
value = self._write_random_value()
275282
if self.unit.is_leader():

src/continuous_writes.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,24 @@ def continuous_writes(database_config: Dict, table_name: str, starting_number: i
5151

5252
def main():
5353
"""Run the continuous writes script."""
54-
[_, username, password, host, port, database, table_name, starting_number] = sys.argv
54+
if len(sys.argv) == 8:
55+
[_, username, password, database, table_name, starting_number, host, port] = sys.argv
56+
socket = None
57+
else:
58+
[_, username, password, database, table_name, starting_number, socket] = sys.argv
59+
5560
database_config = {
5661
"user": username,
5762
"password": password,
5863
"database": database,
5964
"use_pure": True,
6065
"connection_timeout": 5,
6166
}
62-
if port == "socket":
63-
database_config["unix_socket"] = host
67+
if socket:
68+
database_config["unix_socket"] = socket
6469
else:
65-
database_config["port"] = port
6670
database_config["host"] = host
71+
database_config["port"] = port
6772

6873
continuous_writes(database_config, table_name, int(starting_number))
6974

0 commit comments

Comments
 (0)