简体   繁体   中英

Django celery .delay() in requests get stucked

I have a project running on 3 different environments, on 2 environments it works on production for some reason stopped working (was working before).

The code is pretty simple I have a view which runs a task in delay and should return immediately some message to the user:

class PrintTaskView(View):
    def get(self, request):
        logger.debug('ABOUT TO ADD A TASK')
        tasks.print_task.delay()
        logger.debug('TASK WAS ADDED')
        messages.info(request, _('Print task added to queue'))
        return HttpResponseRedirect(reverse_lazy('admin:index'))

I am so confused because this works smooth on my local machine and on the test environment while on production request gets stuck on loading after the first log has been executed. What is even more confusing is that if I run the code from the shell (manage.py shell) then the print task gets created and picked up by a worker and executed. Also celery beat tasks runs smoothly.

My celery settings:

# Celery
CELERY_BROKER_URL = os.getenv('CELERY_BROKER_URL')
CELERY_RESULT_BACKEND = os.getenv('CELERY_RESULT_BACKEND')
CELERY_BEAT_SCHEDULE = {
    ...
}

website/__init__.py

from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app

__all__ = ('celery_app',)

celery.py

from __future__ import absolute_import

from celery import Celery
from django.conf import settings

app = Celery('somename')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS, related_name='tasks')

Any idea or suggestion is more than welcome :)

I actually found out by chance that there is a bug in the connection to Redis in the celery library or somewhere around there and you actually do not get back any error when credentials are wrong while accessing Redis. Someone on the server deleted the credential env variables so the App was not able to connect to Redis and no error was returned. While on the virtual env I had them I was able to connect and everything was running smooth there.

Be warned if you are having same issue ensure your credentials to access Redis are set up correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM