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

Record DB-related events only after changes are committed #7477

Merged
merged 1 commit into from
Feb 15, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
- Side effects of data changes, such as the sending of webhooks,
are no longer triggered until after the changes have been committed
to the database
(<https://github.com/opencv/cvat/pull/7460>)
(<https://github.com/opencv/cvat/pull/7460>,
<https://github.com/opencv/cvat/pull/7477>)
10 changes: 9 additions & 1 deletion cvat/apps/events/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from datetime import datetime, timezone
from typing import Optional

from django.db import transaction

from cvat.apps.engine.log import vlogger

def event_scope(action, resource):
Expand Down Expand Up @@ -39,6 +41,7 @@ def record_server_event(
scope: str,
request_id: Optional[str],
payload: Optional[dict] = None,
on_commit: bool = False,
**kwargs,
) -> None:
payload = payload or {}
Expand All @@ -59,7 +62,12 @@ def record_server_event(
**kwargs,
}

vlogger.info(JSONRenderer().render(data).decode('UTF-8'))
rendered_data = JSONRenderer().render(data).decode('UTF-8')

if on_commit:
transaction.on_commit(lambda: vlogger.info(rendered_data), robust=True)
else:
vlogger.info(rendered_data)


class EventScopeChoice:
Expand Down
6 changes: 6 additions & 0 deletions cvat/apps/events/handlers.py
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I missed it. Thanks for catching it; fixed now.

Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ def handle_create(scope, instance, **kwargs):
record_server_event(
scope=scope,
request_id=request_id(),
on_commit=True,
obj_id=getattr(instance, 'id', None),
obj_name=_get_object_name(instance),
org_id=oid,
Expand Down Expand Up @@ -313,6 +314,7 @@ def handle_update(scope, instance, old_instance, **kwargs):
record_server_event(
scope=scope,
request_id=request_id(),
on_commit=True,
obj_name=prop,
obj_id=getattr(instance, f'{prop}_id', None),
obj_val=str(change["new_value"]),
Expand Down Expand Up @@ -364,6 +366,7 @@ def handle_delete(scope, instance, store_in_deletion_cache=False, **kwargs):
record_server_event(
scope=scope,
request_id=request_id(),
on_commit=True,
obj_id=getattr(instance, 'id', None),
obj_name=_get_object_name(instance),
org_id=oid,
Expand Down Expand Up @@ -405,6 +408,7 @@ def filter_shape_data(shape):
record_server_event(
scope=event_scope(action, "tags"),
request_id=request_id(),
on_commit=True,
count=len(tags),
org_id=oid,
org_slug=oslug,
Expand All @@ -427,6 +431,7 @@ def filter_shape_data(shape):
record_server_event(
scope=scope,
request_id=request_id(),
on_commit=True,
obj_name=shape_type,
count=len(shapes),
org_id=oid,
Expand Down Expand Up @@ -455,6 +460,7 @@ def filter_shape_data(shape):
record_server_event(
scope=scope,
request_id=request_id(),
on_commit=True,
obj_name=track_type,
count=len(tracks),
org_id=oid,
Expand Down
Loading