Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start celery worker throws "no attribute 'worker_state_db'"

When I am trying to start celery worker in Django app as:

celery -A myApp worker -l info

I get following error:

File "/home/alexander/.pyenv/versions/3.5.1/envs/myApp/lib/python3.5/site-packages/celery/utils/collections.py", line 134, in __getattr__
    type(self).__name__, k))

    AttributeError: 'Settings' object has no attribute 'worker_state_db'

If you know how to solve it please write your idea!

like image 557
Alexander Tyapkov Avatar asked Dec 01 '16 16:12

Alexander Tyapkov


4 Answers

The bug appears if an exception is raised while parsing settings. Such as when we set Django's SECRET_KEY (or any other setting) via an environment variable:

SECRET_KEY = os.environ['SECRET_KEY'] 

To solve the problem you can switch back to:

SECRET_KEY = "asdfasdfasdf" 

or use:

SECRET_KEY = os.environ.get('SECRET_KEY', '') 

You can also find which setting caused the problem if you comment our the following line in celery.py file and start the worker again:

app.config_from_object('django.conf:settings', namespace='CELERY') 
like image 186
Alexander Tyapkov Avatar answered Sep 18 '22 12:09

Alexander Tyapkov


I would like to add two things:

  1. This is also true when you load settings from any configuration file, not essentially django's. The question is related purely to Celery.

  2. Some explanation on origins of this cryptic error:

worker_state_db is a setting with a default value, so you shouldn't have to set it manually. An exception is raised because Settings are just empty and don't have any values, even the default ones. That said, we don't have the default config loaded. Somehow in Celery, the exception from parsing (the original one which caused the problem) is not propagated to the stderr when starting a worker. Hence, you get a message which doesn't tell you anything about a possible solution.

How to fix it? For example, if you have celeryconfig.py where your celery app module is placed and you load settings from there via:

app.config_from_object('path.to.your.celery.module.celeryconfig') 

Check your whole celeryconfig.py file for anything that could crash or cause the parser to crash (incompatible settings values?).

like image 36
Wladek Surala Avatar answered Sep 20 '22 12:09

Wladek Surala


I got the same error when one of the keys was missing in my config.json which was being loaded in settings.py (basically a missing key in settings.py).

Error text is totally irrelevant. Hope that helps!

like image 26
Kuldip Patel Avatar answered Sep 20 '22 12:09

Kuldip Patel


I got this over and over while trying to configure celery with a dockerized django project. The solution was including env_file in docker-compose.yaml:

  celery:
    ...
    env_file: .env
like image 40
Mic Avatar answered Sep 19 '22 12:09

Mic