Skip to content

Commit

Permalink
Record DB-related events only after changes are committed
Browse files Browse the repository at this point in the history
  • Loading branch information
SpecLad committed Feb 12, 2024
1 parent e8384f8 commit d8a8c78
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
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
5 changes: 5 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 @@ -405,6 +407,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 +430,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 +459,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 d8a8c78

Please sign in to comment.