Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure connection is closed after CLI commands #2635

Merged
merged 3 commits into from
Oct 27, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cg/cli/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import click
import coloredlogs
from sqlalchemy.orm import scoped_session

import cg
from cg.cli.add import add as add_cmd
Expand All @@ -26,11 +27,19 @@
from cg.io.controller import ReadFile
from cg.models.cg_config import CGConfig
from cg.store.database import create_all_tables, drop_all_tables, get_tables
from cg.store.database import get_scoped_session_registry

LOG = logging.getLogger(__name__)
LEVELS = ["DEBUG", "INFO", "WARNING", "ERROR"]


def teardown_session():
"""Ensure that the session is closed and all resources are released to the connection pool."""
registry: Optional[scoped_session] = get_scoped_session_registry()
seallard marked this conversation as resolved.
Show resolved Hide resolved
if registry:
registry.remove()


@click.group()
@click.option("-c", "--config", type=click.Path(exists=True), help="path to config file")
@click.option("-d", "--database", help="path/URI of the SQL database")
Expand Down Expand Up @@ -60,6 +69,7 @@ def base(
else {"database": database}
)
context.obj = CGConfig(**raw_config)
context.call_on_close(teardown_session)


@base.command()
Expand Down