简体   繁体   English

无法让 Celery 使用 prod 设置处理数字海洋水滴

[英]Can't get Celery to work on digital ocean droplet with prod settings

It is working fine on local server, but when I try to start a worker after ssh I get an error.它在本地服务器上运行良好,但是当我尝试在 ssh 之后启动工作人员时出现错误。

/var/www/bin/celery -A stock worker -l info

I know DJANGO_SETTINGS_MODULE is set correctly as I have a print statement showing it is set, (and the rest of the server is live, using production settings).我知道 DJANGO_SETTINGS_MODULE 设置正确,因为我有一个打印语句显示它已设置(并且服务器的 rest 是实时的,使用生产设置)。 I've also tried using this command, which gives the same error.我也试过使用这个命令,它给出了同样的错误。

DJANGO_SETTINGS_MODULE="stock.settings.pro" /var/www/bin/celery -A stock worker -l info

I have a celery directory that is in my main directory (beside my settings directory).我的主目录中有一个 celery 目录(在我的设置目录旁边)。 It contains an init .py file and a conf.py (that sets the results backend).它包含一个init .py 文件和一个 conf.py(设置结果后端)。 Here is the init file:这是初始化文件:

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from celery.schedules import crontab

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'stock.settings.pro')
DJANGO_SETTINGS_MODULE = os.environ.get('DJANGO_SETTINGS_MODULE')
print("celery - ", DJANGO_SETTINGS_MODULE)
BASE_REDIS_URL = os.environ.get('REDIS_URL', 'redis://localhost:6379')

app = Celery('stock')
app.config_from_object('django.conf:settings', namespace='CELERY')

app.autodiscover_tasks()

app.conf.broker_url = BASE_REDIS_URL

app.conf.beat_scheduler = 'django_celery_beat.schedulers:DatabaseScheduler'

Here is the traceback error:这是回溯错误:

Traceback (most recent call last):
File "/var/www/lib/python3.8/site-packages/kombu/utils/objects.py", line 42, in __get__
return obj.__dict__[self.__name__]
KeyError: 'data'

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/var/www/bin/celery", line 8, in <module>
sys.exit(main())
File "/var/www/lib/python3.8/site-packages/celery/__main__.py", line 16, in main
_main()
File "/var/www/lib/python3.8/site-packages/celery/bin/celery.py", line 322, in main
cmd.execute_from_commandline(argv)
File "/var/www/lib/python3.8/site-packages/celery/bin/celery.py", line 499, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "/var/www/lib/python3.8/site-packages/celery/bin/base.py", line 305, in execute_from_commandline
return self.handle_argv(self.prog_name, argv[1:])
File "/var/www/lib/python3.8/site-packages/celery/bin/celery.py", line 491, in handle_argv
return self.execute(command, argv)
File "/var/www/lib/python3.8/site-packages/celery/bin/celery.py", line 415, in execute
return cls(
File "/var/www/lib/python3.8/site-packages/celery/bin/worker.py", line 221, in run_from_argv
*self.parse_options(prog_name, argv, command))
File "/var/www/lib/python3.8/site-packages/celery/bin/base.py", line 428, in parse_options
self.parser = self.create_parser(prog_name, command)
File "/var/www/lib/python3.8/site-packages/celery/bin/base.py", line 444, in create_parser
self.add_arguments(parser)
File "/var/www/lib/python3.8/site-packages/celery/bin/worker.py", line 278, in add_arguments
default=conf.worker_state_db,
File "/var/www/lib/python3.8/site-packages/celery/utils/collections.py", line 134, in __getattr__
return self[k]
File "/var/www/lib/python3.8/site-packages/celery/utils/collections.py", line 444, in __getitem__
return getitem(k)
File "/var/www/lib/python3.8/site-packages/celery/utils/collections.py", line 287, in __getitem__
return mapping[_key]
File "/usr/lib/python3.8/collections/__init__.py", line 1006, in __getitem__
if key in self.data:
File "/var/www/lib/python3.8/site-packages/kombu/utils/objects.py", line 44, in __get__
value = obj.__dict__[self.__name__] = self.__get(obj)
File "/var/www/lib/python3.8/site-packages/celery/app/base.py", line 141, in data
return self.callback()
File "/var/www/lib/python3.8/site-packages/celery/app/base.py", line 920, in _finalize_pending_conf
conf = self._conf = self._load_config()
File "/var/www/lib/python3.8/site-packages/celery/app/base.py", line 930, in _load_config
self.loader.config_from_object(self._config_source)
File "/var/www/lib/python3.8/site-packages/celery/loaders/base.py", line 131, in config_from_object
self._conf = force_mapping(obj)
File "/var/www/lib/python3.8/site-packages/celery/utils/collections.py", line 54, in force_mapping
if isinstance(m, (LazyObject, LazySettings)):
File "/var/www/lib/python3.8/site-packages/django/utils/functional.py", line 224, in inner
self._setup()
File "/var/www/lib/python3.8/site-packages/django/conf/__init__.py", line 63, in _setup
self._wrapped = Settings(settings_module)
File "/var/www/lib/python3.8/site-packages/django/conf/__init__.py", line 161, in __init__
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

Thanks a lot for any help, I've tried solving this for a couple days now and haven't really gotten anywhere!非常感谢您的帮助,我已经尝试解决这个问题几天了,但还没有真正得到任何帮助!

I randomly came across your questions when looking for something else.我在寻找其他东西时偶然发现了你的问题。 The problem as per your log is obviously: " The SECRET_KEY setting must not be empty. "根据您的日志,问题显然是:“ SECRET_KEY 设置不能为空。

At the time when celery runs your secret key is not yet 'present' in your environment it seems.在 celery 运行时,您的密钥似乎尚未“存在”在您的环境中。 Do you have the same in development?你有同样的发展吗? For testing try to set a random Secret key on top of your settings.py file and/or top of your celery init file as you have above.为了进行测试,请尝试在您的 settings.py 文件和/或 celery 初始化文件的顶部设置一个随机密钥,如上所述。 If that issue goes away then you know what the problem is.如果这个问题消失了,那么你就知道问题所在了。 You can fix it from there then.你可以从那里修复它。

Usually the SECRET_KEY should be part of your environment and populated before celery is running.通常 SECRET_KEY 应该是您环境的一部分并在 celery 运行之前填充。 In that case your run order might be wrong or the env vars are not set correctly (or after celery is initialized).在这种情况下,您的运行顺序可能是错误的,或者环境变量设置不正确(或者在 celery 初始化之后)。

I hope that makes sense.我希望这是有道理的。

In my own development (NOT production) environment I have all my vars including the SECRET_KEY in an.env file and would come across the same issue.在我自己的开发(非生产)环境中,我的所有变量都包含在一个 .env 文件中,包括 SECRET_KEY 并且会遇到同样的问题。 Basically the celery init.py file will load before SECRET_KEY is initialized.基本上 celery init.py 文件将在 SECRET_KEY 初始化之前加载。 To circumvent that problem I simply put a flag on top of my settings.py file along the lines of:为了规避这个问题,我只是在我的 settings.py 文件顶部放置了一个标志,如下所示:

if development:
   import dotenv
   dotenv.load_dotenv()

Where I set 'development' to True or False depending on what environment it is.我在哪里将“开发”设置为 True 或 False,具体取决于它是什么环境。 Again: if this error happens in production something is wrong with your initialization order most likely.再说一遍:如果这个错误发生在生产中,你的初始化顺序很可能有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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