Skip to content

Commit c3b0c64

Browse files
committed
Send Profile Export kafka message async
Send Profile Export kafka message async
1 parent ff4fa4f commit c3b0c64

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

django/thunderstore/modpacks/api/experimental/tests/test_legacyprofile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ def test_experimental_api_legacyprofile_create_sends_kafka_event(
137137
result = response.json()
138138
profile_key = result["key"]
139139

140-
mock_send_kafka_message.assert_called_once()
141-
call_args = mock_send_kafka_message.call_args
140+
mock_send_kafka_message.delay.assert_called_once()
141+
call_args = mock_send_kafka_message.delay.call_args
142142

143143
assert call_args.kwargs["topic"] == KafkaTopic.A_LEGACY_PROFILE_EXPORT_V1
144144

django/thunderstore/modpacks/api/experimental/views/legacyprofile.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,6 @@ class AnalyticsEventLegacyProfileExport(BaseModel):
4444
file_size_bytes: int
4545

4646

47-
def push_export_creation_to_kafka(key: str, file_size_bytes: int):
48-
try:
49-
send_kafka_message(
50-
topic=KafkaTopic.A_LEGACY_PROFILE_EXPORT_V1,
51-
payload_string=AnalyticsEventLegacyProfileExport(
52-
id=key,
53-
timestamp=timezone.now(),
54-
file_size_bytes=file_size_bytes,
55-
).json(),
56-
)
57-
except Exception:
58-
logger.warning(
59-
"Failed to send legacy profile export event to Kafka", exc_info=True
60-
)
61-
62-
6347
class LegacyProfileCreateApiView(APIView):
6448
permission_classes = []
6549
parser_classes = [LegacyProfileFileUploadParser]
@@ -74,15 +58,24 @@ class LegacyProfileCreateApiView(APIView):
7458
def post(self, request, *args, **kwargs):
7559
if "file" not in self.request.data or not self.request.data["file"]:
7660
raise ValidationError(detail="Request body was empty")
77-
key = LegacyProfile.objects.get_or_create_from_upload(
78-
content=self.request.data["file"]
79-
)
80-
serializer = LegacyProfileCreateResponseSerializer({"key": key})
61+
62+
file_obj = self.request.data["file"]
63+
file_size = file_obj.size
64+
65+
key = LegacyProfile.objects.get_or_create_from_upload(content=file_obj)
66+
8167
transaction.on_commit(
82-
lambda: push_export_creation_to_kafka(
83-
key=str(key), file_size_bytes=self.request.data["file"].size
68+
lambda: send_kafka_message.delay(
69+
topic=KafkaTopic.A_LEGACY_PROFILE_EXPORT_V1,
70+
payload_string=AnalyticsEventLegacyProfileExport(
71+
id=str(key),
72+
timestamp=timezone.now(),
73+
file_size_bytes=file_size,
74+
).json(),
8475
)
8576
)
77+
78+
serializer = LegacyProfileCreateResponseSerializer({"key": key})
8679
return Response(
8780
serializer.data,
8881
status=status.HTTP_200_OK,

0 commit comments

Comments
 (0)