Skip to content

Commit 32b2588

Browse files
committed
added DB test
1 parent 30a4d87 commit 32b2588

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

scripts/maintenance/computational-clusters/autoscaled_monitor/cli.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def _parse_environment(deploy_config: Path) -> dict[str, str | None]:
5050
def main(
5151
deploy_config: Annotated[
5252
Path, typer.Option(help="path to the deploy configuration")
53-
]
53+
],
5454
):
5555
"""Manages external clusters"""
5656

@@ -157,5 +157,11 @@ def trigger_cluster_termination(
157157
asyncio.run(api.trigger_cluster_termination(state, user_id, wallet_id))
158158

159159

160+
@app.command()
161+
def test_database_connection() -> None:
162+
"""this will check the connection to simcore database is ready"""
163+
asyncio.run(api.test_database_connection(state))
164+
165+
160166
if __name__ == "__main__":
161167
app()

scripts/maintenance/computational-clusters/autoscaled_monitor/core.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,3 +638,7 @@ async def trigger_cluster_termination(
638638
)
639639
else:
640640
rich.print("not deleting anything")
641+
642+
643+
async def test_database_connection(state: AppState) -> None:
644+
await db.test_db_connection(state)

scripts/maintenance/computational-clusters/autoscaled_monitor/db.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,23 @@ async def abort_job_in_db(
5757
rich.print(f"set comp_tasks for {project_id=}/{node_id=} set to ABORTED")
5858

5959

60+
async def test_db_connection(state: AppState) -> bool:
61+
try:
62+
async with contextlib.AsyncExitStack() as stack:
63+
engine = await stack.enter_async_context(db_engine(state))
64+
db_connection = await stack.enter_async_context(engine.connect())
65+
# Perform a simple query to test the connection
66+
result = await db_connection.execute(sa.text("SELECT 1"))
67+
result.one()
68+
rich.print(
69+
"[green]Database connection test completed successfully![/green]"
70+
)
71+
return True
72+
except Exception as e: # pylint: disable=broad-exception-caught
73+
rich.print(f"[red]Database connection test failed: {e}[/red]")
74+
return False
75+
76+
6077
async def list_computational_tasks_from_db(
6178
state: AppState, user_id: int
6279
) -> list[ComputationalTask]:

0 commit comments

Comments
 (0)