From 7722eef0a3461e2d1c9f521c17720a548e147106 Mon Sep 17 00:00:00 2001 From: Egor fureunoir Gorbunov Date: Wed, 2 Jul 2025 15:36:52 +0300 Subject: [PATCH] Features: 1) Added custom queues to all Celery tasks for more granular processing; 2) Implemented sensitive data scrubbing in Sentry error reporting. Fixes: None; Extra: Refactored Sentry initialization to include `before_send` callback and improved settings structure. --- core/tasks.py | 10 +++++----- core/utils/emailing.py | 6 +++--- evibes/settings/base.py | 33 ++++++++++++++++++++++++++++----- payments/utils/emailing.py | 2 +- vibes_auth/tasks.py | 4 ++-- vibes_auth/utils/emailing.py | 4 ++-- 6 files changed, 41 insertions(+), 18 deletions(-) diff --git a/core/tasks.py b/core/tasks.py index 3bbe5f33..d5910a58 100644 --- a/core/tasks.py +++ b/core/tasks.py @@ -19,7 +19,7 @@ from evibes.settings import MEDIA_ROOT logger = get_task_logger(__name__) -@shared_task +@shared_task(queue="stock_updater") def update_products_task(): """ Run a background task to update product data and manage stale products. @@ -54,7 +54,7 @@ def update_products_task(): return True, "Success" -@shared_task +@shared_task(queue="default") def update_orderproducts_task(): """ Updates the statuses of order products for all vendors listed in the @@ -76,7 +76,7 @@ def update_orderproducts_task(): return True, "Success" -@shared_task +@shared_task(queue="default") def set_default_caches_task(): """ Task to set default caches in the application's memory. @@ -92,7 +92,7 @@ def set_default_caches_task(): return True, "Success" -@shared_task +@shared_task(queue="default") def remove_stale_product_images(): """ Removes stale product images from the products directory by identifying directories @@ -139,7 +139,7 @@ def remove_stale_product_images(): logger.error("Error removing directory %s: %s", entry_path, e) -@shared_task +@shared_task(queue="default") def process_promotions() -> tuple[bool, str]: """ Processes and updates promotions based on holiday data or default settings. diff --git a/core/utils/emailing.py b/core/utils/emailing.py index a1ec3c9e..db43d824 100644 --- a/core/utils/emailing.py +++ b/core/utils/emailing.py @@ -12,7 +12,7 @@ from core.models import Order, OrderProduct from core.utils.constance import set_email_settings -@shared_task +@shared_task(queue="default") def contact_us_email(contact_info): set_email_settings() connection = mail.get_connection() @@ -40,7 +40,7 @@ def contact_us_email(contact_info): return True, str(contact_info.get("email")) -@shared_task +@shared_task(queue="default") def send_order_created_email(order_pk: str) -> tuple[bool, str]: try: order = Order.objects.get(pk=order_pk) @@ -74,7 +74,7 @@ def send_order_created_email(order_pk: str) -> tuple[bool, str]: return True, str(order.uuid) -@shared_task +@shared_task(queue="default") def send_order_finished_email(order_pk: str) -> tuple[bool, str]: def send_digital_assets_email(ops: list[OrderProduct]): if len(ops) <= 0: diff --git a/evibes/settings/base.py b/evibes/settings/base.py index 30e0288d..ee4115fe 100644 --- a/evibes/settings/base.py +++ b/evibes/settings/base.py @@ -281,13 +281,32 @@ if getenv("SENTRY_DSN"): from sentry_sdk.integrations.logging import LoggingIntegration from sentry_sdk.integrations.redis import RedisIntegration - ignore_errors: list[str] = [ - ] + def scrub_sensitive(data): + if isinstance(data, dict): + cleaned = {} + for key, value in data.items(): + if key.lower() in ("password", "confirm_password"): + cleaned[key] = "[FILTERED]" + else: + cleaned[key] = scrub_sensitive(value) + return cleaned + if isinstance(data, list): + return [scrub_sensitive(item) for item in data] + return data + + def before_send(event, hint): + if hint: + pass + request = event.get("request", {}) + if "data" in request: + request["data"] = scrub_sensitive(request["data"]) + event["request"] = request + return event + + ignore_errors: list[str] = [] sentry_sdk.init( dsn=getenv("SENTRY_DSN"), - traces_sample_rate=1.0 if DEBUG else 0.2, - profiles_sample_rate=1.0 if DEBUG else 0.1, integrations=[ DjangoIntegration(), LoggingIntegration(level=logging.INFO, event_level=logging.ERROR), @@ -295,9 +314,13 @@ if getenv("SENTRY_DSN"): RedisIntegration(), ], environment="development" if DEBUG else "production", - debug=False, release=f"evibes@{EVIBES_VERSION}", + traces_sample_rate=1.0 if DEBUG else 0.2, + profiles_sample_rate=1.0 if DEBUG else 0.1, + max_request_body_size="always", + before_send=before_send, ignore_errors=ignore_errors, + debug=False, ) SESSION_COOKIE_HTTPONLY: bool = True diff --git a/payments/utils/emailing.py b/payments/utils/emailing.py index 7aa537c8..068ddd7d 100644 --- a/payments/utils/emailing.py +++ b/payments/utils/emailing.py @@ -12,7 +12,7 @@ from core.utils.constance import set_email_settings from payments.models import Transaction -@shared_task +@shared_task(queue="default") def balance_deposit_email(transaction_pk: str) -> tuple[bool, str]: try: transaction = Transaction.objects.get(pk=transaction_pk) diff --git a/vibes_auth/tasks.py b/vibes_auth/tasks.py index 8f782fbf..aa103eb6 100644 --- a/vibes_auth/tasks.py +++ b/vibes_auth/tasks.py @@ -4,7 +4,7 @@ from core.models import Order, Wishlist from vibes_auth.models import User -@shared_task +@shared_task(queue="default") def create_pending_order(user_uuid): try: user = User.objects.get(uuid=user_uuid) @@ -14,7 +14,7 @@ def create_pending_order(user_uuid): return False, f"Bad uuid was given: {user_uuid}" -@shared_task +@shared_task(queue="default") def create_wishlist(user_uuid): try: user = User.objects.get(uuid=user_uuid) diff --git a/vibes_auth/utils/emailing.py b/vibes_auth/utils/emailing.py index 25f262ad..55949924 100644 --- a/vibes_auth/utils/emailing.py +++ b/vibes_auth/utils/emailing.py @@ -13,7 +13,7 @@ from core.utils.constance import set_email_settings from vibes_auth.models import User -@shared_task +@shared_task(queue="default") def send_verification_email_task(user_pk: str) -> tuple[bool, str]: try: user = User.objects.get(pk=user_pk) @@ -55,7 +55,7 @@ def send_verification_email_task(user_pk: str) -> tuple[bool, str]: return True, user.uuid -@shared_task +@shared_task(queue="default") def send_reset_password_email_task(user_pk: str) -> tuple[bool, str]: try: user = User.objects.get(pk=user_pk)