Skip to content

Commit

Permalink
Ensure connection is closed after CLI commands (#2635)(patch)
Browse files Browse the repository at this point in the history
  • Loading branch information
seallard authored Oct 27, 2023
1 parent 4e1fe00 commit 3943157
Showing 1 changed file with 10 additions and 0 deletions.
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: scoped_session | None = get_scoped_session_registry()
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

0 comments on commit 3943157

Please sign in to comment.