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

chore: Move function out of receivers #76255

Merged
merged 6 commits into from
Aug 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
27 changes: 4 additions & 23 deletions src/sentry/receivers/core.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import logging
from functools import wraps

from click import echo
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from django.db import connections, router, transaction
from django.db.models.signals import post_save
from django.db.utils import OperationalError, ProgrammingError

from sentry.hybridcloud.models.outbox import outbox_context
from sentry.loader.dynamic_sdk_options import get_default_loader_data
Expand All @@ -19,6 +15,7 @@
from sentry.signals import post_upgrade, project_created
from sentry.silo.base import SiloMode, region_silo_function
from sentry.users.services.user.service import user_service
from sentry.utils.db import handle_db_failure
from sentry.utils.env import in_test_environment
from sentry.utils.settings import is_self_hosted

Expand All @@ -30,22 +27,6 @@
DEFAULT_SENTRY_PROJECT_ID = 1


def handle_db_failure(func, using=None, wrap_in_transaction=True):
@wraps(func)
def wrapped(*args, **kwargs):
try:
if wrap_in_transaction:
with transaction.atomic(using or router.db_for_write(Organization)):
return func(*args, **kwargs)
else:
return func(*args, **kwargs)
except (ProgrammingError, OperationalError):
logging.exception("Failed processing signal %s", func.__name__)
return

return wrapped


def create_default_projects(**kwds):
if not (in_test_environment() or is_self_hosted() or settings.DEBUG):
# No op in production SaaS environments.
Expand Down Expand Up @@ -151,20 +132,20 @@ def freeze_option_epoch_for_project(instance, created, app=None, **kwargs):
# Anything that relies on default objects that may not exist with default
# fields should be wrapped in handle_db_failure
post_upgrade.connect(
handle_db_failure(create_default_projects, wrap_in_transaction=False),
handle_db_failure(create_default_projects, model=Organization, wrap_in_transaction=False),
dispatch_uid="create_default_project",
weak=False,
sender=SiloMode.MONOLITH,
)

post_save.connect(
handle_db_failure(freeze_option_epoch_for_project),
handle_db_failure(freeze_option_epoch_for_project, model=Organization),
sender=Project,
dispatch_uid="freeze_option_epoch_for_project",
weak=False,
)
post_save.connect(
handle_db_failure(create_keys_for_project),
handle_db_failure(create_keys_for_project, model=Organization),
sender=Project,
dispatch_uid="create_keys_for_project",
weak=False,
Expand Down
21 changes: 20 additions & 1 deletion src/sentry/utils/db.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
import logging
from collections.abc import Sequence
from contextlib import ExitStack
from functools import wraps

import sentry_sdk
from django.db import DEFAULT_DB_ALIAS, connections, transaction
from django.db import DEFAULT_DB_ALIAS, connections, router, transaction
from django.db.utils import OperationalError, ProgrammingError
from sentry_sdk.integrations import Integration


def handle_db_failure(func, model, wrap_in_transaction: bool = True):
@wraps(func)
def wrapped(*args, **kwargs):
try:
if wrap_in_transaction:
with transaction.atomic(router.db_for_write(model)):
return func(*args, **kwargs)
else:
return func(*args, **kwargs)
except (ProgrammingError, OperationalError):
logging.exception("Failed processing signal %s", func.__name__)
return

return wrapped


def atomic_transaction(
using: str | Sequence[str], savepoint: bool = True
) -> transaction.Atomic | ExitStack:
Expand Down
Loading