celery / django-celery-results

Celery result back end with django

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

rehbarkhan opened this issue · comments

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

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.