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

Use dedicated database session for constraint runner #5718

Merged
merged 2 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions backend/infrahub/database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,19 @@ def __init__(
elif self.db_type == DatabaseType.MEMGRAPH:
self.manager = DatabaseManagerMemgraph(db=self)

def __del__(self) -> None:
if not self._session or not self._is_session_local or self._session.closed():
return

try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = None
if loop and loop.is_running():
loop.create_task(self._session.close())
else:
asyncio.run(self._session.close())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you explain what this change does? is it so that we always clean up a session created with start_session()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you got the point. The other way would have been to close the session at the end of the create mutation. Not sure what people like best, and I don't have a strong opinion on this myself.


@property
def is_session(self) -> bool:
if self._mode == InfrahubDatabaseMode.SESSION:
Expand Down
9 changes: 3 additions & 6 deletions backend/infrahub/graphql/mutations/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ async def mutate_create_object(
branch: Branch,
) -> Node:
component_registry = get_component_registry()
node_constraint_runner = await component_registry.get_component(
NodeConstraintRunner, db=db.start_session(), branch=branch
)
node_class = Node
if cls._meta.schema.kind in registry.node:
node_class = registry.node[cls._meta.schema.kind]
Expand All @@ -184,18 +187,12 @@ async def mutate_create_object(
if db.is_transaction:
obj = await node_class.init(db=db, schema=cls._meta.schema, branch=branch)
await obj.new(db=db, **data)
node_constraint_runner = await component_registry.get_component(
NodeConstraintRunner, db=db, branch=branch
)
await node_constraint_runner.check(node=obj, field_filters=fields_to_validate)
await obj.save(db=db)
else:
async with db.start_transaction() as dbt:
obj = await node_class.init(db=dbt, schema=cls._meta.schema, branch=branch)
await obj.new(db=dbt, **data)
node_constraint_runner = await component_registry.get_component(
NodeConstraintRunner, db=dbt, branch=branch
)
await node_constraint_runner.check(node=obj, field_filters=fields_to_validate)
await obj.save(db=dbt)

Expand Down
Loading