Skip to content

Commit

Permalink
Record DB-related events only after changes are committed (#7477)
Browse files Browse the repository at this point in the history
This is a small addendum to #7460, which I couldn't include in that PR,
because it was blocked by #7430.
  • Loading branch information
SpecLad authored Feb 15, 2024
1 parent 6673bb1 commit 6260232
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
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
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

0 comments on commit 6260232

Please sign in to comment.