Skip to content

Commit f97b3dc

Browse files
author
Alan Christie
committed
fix: Introduces 'ping_configured_connector'
1 parent 4b4f0f8 commit f97b3dc

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

api/remote_ispyb_connector.py

+18
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
ISPyBRetrieveFailed,
1313
)
1414

15+
logger: logging.Logger = logging.getLogger(__name__)
16+
1517

1618
class SSHConnector(Connector):
1719
def __init__(
@@ -34,6 +36,7 @@ def __init__(
3436

3537
self.conn_inactivity = conn_inactivity
3638
self.lock = threading.Lock()
39+
self.conn = None
3740
self.server = None
3841
self.last_activity_ts = None
3942

@@ -49,6 +52,12 @@ def __init__(
4952
'db_name': db,
5053
}
5154
self.remote_connect(**creds)
55+
logger.debug(
56+
"Started host=%s username=%s local_bind_port=%s",
57+
ssh_host,
58+
ssh_user,
59+
self.server.local_bind_port,
60+
)
5261

5362
else:
5463
self.connect(
@@ -59,6 +68,7 @@ def __init__(
5968
port=port,
6069
conn_inactivity=conn_inactivity,
6170
)
71+
logger.debug("Started host=%s user=%s port=%s", host, user, port)
6272

6373
def remote_connect(
6474
self, ssh_host, ssh_user, ssh_pass, db_host, db_port, db_user, db_pass, db_name
@@ -125,3 +135,11 @@ def call_sp_retrieve(self, procname, args):
125135
if result == []:
126136
raise ISPyBNoResultException
127137
return result
138+
139+
def stop(self):
140+
if self.server is not None:
141+
self.server.stop()
142+
self.server = None
143+
self.conn = None
144+
self.last_activity_ts = None
145+
logger.debug("Server stopped")

api/security.py

+15
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,21 @@ def get_configured_connector() -> Optional[Union[Connector, SSHConnector]]:
116116
return None
117117

118118

119+
def ping_configured_connector() -> bool:
120+
"""Pings the connector. If a connection can be obtained it is immediately closed.
121+
The ping simply provides a way to check the credentials are valid and
122+
a connection can be made.
123+
"""
124+
conn: Optional[Union[Connector, SSHConnector]] = None
125+
if connector == 'ispyb':
126+
conn = get_conn()
127+
elif connector == 'ssh_ispyb':
128+
conn = get_remote_conn()
129+
if conn is not None:
130+
conn.stop()
131+
return conn is not None
132+
133+
119134
class ISpyBSafeQuerySet(viewsets.ReadOnlyModelViewSet):
120135
def get_queryset(self):
121136
"""

viewer/services.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from frag.utils.network_utils import get_driver
1010
from pydiscourse import DiscourseClient
1111

12-
from api.security import get_configured_connector
12+
from api.security import ping_configured_connector
1313
from viewer.squonk2_agent import get_squonk2_agent
1414

1515
logger = logging.getLogger(__name__)
@@ -156,7 +156,7 @@ def ispyb(func_id, name, ispyb_host=None) -> bool:
156156
del func_id, name, ispyb_host
157157

158158
logger.debug("+ ispyb")
159-
return get_configured_connector() != None
159+
return ping_configured_connector()
160160

161161

162162
@service_query

0 commit comments

Comments
 (0)