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

Getting exception "select_for_update cannot be used outside of a transaction" #422

Open
rehbarkhan opened this issue Mar 1, 2024 · 2 comments

Comments

@rehbarkhan
Copy link

django-celery-results is not able to save the tasks results, getting below error. :

{"exc_type": "TransactionManagementError", "exc_message": ["select_for_update cannot be used outside of a transaction."], "exc_module": "django.db.transaction"}

My App Config:

  • django==4.2.*
  • django-celery-beat==2.5.0
  • django-celery-results==2.5.1

My database config:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'production',
        'USER': 'production_user',
        'PASSWORD': env('MYSQL_PASSWORD'),
        'HOST': env('MYSQL_HOST'),
        'CONN_MAX_AGE': 600,
        # 'ATOMIC_REQUESTS': True,
    }
}

DATABASES['production_db'] = DATABASES['default']

I have tried to enable "ATOMIC_REQUESTS: True" but no luck, still getting the TransactionManagementError

@auvipy
Copy link
Member

auvipy commented Mar 1, 2024

@rehbarkhan
Copy link
Author

rehbarkhan commented Mar 1, 2024

I have tried this, but it didn't work.
I went to the django-celery-results code base and updated the blow code

def on_chord_part_return(self, request, state, result, **kwargs):
        """Called on finishing each part of a Chord header"""
        tid, gid = request.id, request.group
        if not gid or not tid:
            return
        call_callback = False
        with transaction.atomic():
            # We need to know if `count` hits 0.
            # wrap the update in a transaction

with

def on_chord_part_return(self, request, state, result, **kwargs):
        """Called on finishing each part of a Chord header"""
        tid, gid = request.id, request.group
        if not gid or not tid:
            return
        call_callback = False
        with transaction.atomic(using = 'production_db'):
            # We need to know if `count` hits 0.
            # wrap the update in a transaction

I have passed using='production_db' to atomic method.

After making these changes, results are getting stored in the django database however after 30 to 40mins, celery is exiting automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants