英文:
No hostname was supplied. Reverting to default 'localhost' - Celery Redis Django Ubuntu 22.04
问题
I built an application with django. There are some background tasks that run using celery and redis.
在 Django 中构建了一个应用程序,使用 Celery 和 Redis 运行一些后台任务。
On windows, everything works fine. However when I deploy on Ubuntu 22.04, I get the following error in the log.
在 Windows 上,一切正常。但是当我部署到 Ubuntu 22.04 时,在日志中出现以下错误。
settings.py
settings.py 文件:
CELERY SETTINGS
CELERY_BROKER_URL = 'redis://127.0.0.1:6379/0'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = TIME_ZONE or 'UTC'
CELERY_RESULT_BACKEND = 'django-db'
CELERY BEAT SETTINGS
CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'
celery.py
celery.py 文件:
from future import absolute_import, unicode_literals
import os
from celery import Celery
from django.conf import settings
from celery.schedules import crontab
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dear.settings')
app = Celery('dear')
app.conf.broker_url = 'redis://localhost:6379/0'
app.conf.result_backend = 'redis://localhost:6379/0'
app.conf.enable_utc = False
app.conf.update(timezone = 'Asia/Dubai')
app.config_from_object(settings, namespace='CELERY')
celery.conf
celery.conf 文件:
[program:dearapi]
command=/home/djangoadmin/pyapps/venv/bin/celery -A celery worker --loglevel=INFO
directory=/home/djangoadmin/pyapps/dearapi/dear
user=djangoadmin
numprocs=1
stdout_logfile=/var/log/supervisor/celery.log
stderr_logfile=/var/log/supervisor/celery.log
autostart=true
autorestart=true
startsecs=0
stopwaitsecs = 600
killasgroup=true
priority=998
celerybeat.conf
celerybeat.conf 文件:
[program:dearapibeat]
command=/home/djangoadmin/pyapps/venv/bin/celery -A celery beat --loglevel=INFO
directory=/home/djangoadmin/pyapps/dearapi/dear
user=djangoadmin
numprocs=1
stdout_logfile=/var/log/supervisor/celerybeat.log
stderr_logfile=/var/log/supervisor/celerybeat.log
autostart=true
autorestart=true
startsecs=0
priority=999
Can anyone please tell me what I am doing wrong?
有人可以告诉我我做错了什么吗?
I use supervisor to start all processes but it cannot find the redis broker. The program reverts to default and tries to find rabbitmq, which is not installed.
我使用 supervisor 启动所有进程,但它找不到 Redis 代理。程序恢复到默认设置,尝试查找未安装的 RabbitMQ。
Let me know if you need any other code that I may have missed here.
如果您需要其他可能被遗漏的代码,请告诉我。
英文:
I built an application with django. There are some background tasks that run using celery and redis.
On windows, everything works fine. However when I deploy on Ubuntu 22.04, I get the following error in the log.
settings.py
# CELERY SETTINGS
CELERY_BROKER_URL = 'redis://127.0.0.1:6379/0'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = TIME_ZONE or 'UTC'
CELERY_RESULT_BACKEND = 'django-db'
# CELERY BEAT SETTINGS
CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'
celery.py
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from django.conf import settings
from celery.schedules import crontab
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dear.settings')
app = Celery('dear')
app.conf.broker_url = 'redis://localhost:6379/0'
app.conf.result_backend = 'redis://localhost:6379/0'
app.conf.enable_utc = False
app.conf.update(timezone = 'Asia/Dubai')
app.config_from_object(settings, namespace='CELERY')
celery.conf
[program:dearapi]
command=/home/djangoadmin/pyapps/venv/bin/celery -A celery worker --loglevel=INFO
directory=/home/djangoadmin/pyapps/dearapi/dear
user=djangoadmin
numprocs=1
stdout_logfile=/var/log/supervisor/celery.log
stderr_logfile=/var/log/supervisor/celery.log
autostart=true
autorestart=true
startsecs=0
; Need to wait for currently executing tasks to finish at shutdown.
; Increase this if you have very long running tasks.
stopwaitsecs = 600
; When resorting to send SIGKILL to the program to terminate it
; send SIGKILL to its whole process group instead,
; taking care of its children as well.
killasgroup=true
; if rabbitmq is supervised, set its priority higher
; so it starts first
priority=998
celerybeat.conf
; ========================
; celery beat supervisor
; ========================
; the name of your supervisord program
[program:dearapibeat]
; Set full path to celery program if using virtualenv
command=/home/djangoadmin/pyapps/venv/bin/celery -A celery beat --loglevel=INFO
; The directory to your Django project
directory=/home/djangoadmin/pyapps/dearapi/dear
; If supervisord is run as the root user, switch users to this UNIX user account
; before doing any processing.
user=djangoadmin
; Supervisor will start as many instances of this program as named by numprocs
numprocs=1
; Put process stdout output in this file
stdout_logfile=/var/log/supervisor/celerybeat.log
; Put process stderr output in this file
stderr_logfile=/var/log/supervisor/celerybeat.log
; If true, this program will start automatically when supervisord is started
autostart=true
; May be one of false, unexpected, or true. If false, the process will never
; be autorestarted. If unexpected, the process will be restart when the program
; exits with an exit code that is not one of the exit codes associated with this
; process’ configuration (see exitcodes). If true, the process will be
; unconditionally restarted when it exits, without regard to its exit code.
autorestart=true
; The total number of seconds which the program needs to stay running after
; a startup to consider the start successful.
startsecs=0
; if your broker is supervised, set its priority higher
; so it starts first
priority=999
Can anyone please tell me what I am doing wrong?
I use supervisor to start all processes but it cannot find the redis broker. The program reverts to default and tries to find rabbitmq, which is not installed.
Let me know if you need any other code that I may have missed here.
答案1
得分: 0
我找到了错误。我的 celery.conf 和 celerybeat.conf 中的路径不正确。
celery.conf
command=/home/<user>/pyapps/venv/bin/celery -A <project settings folder> worker --loglevel=INFO
directory=/home/<user>/pyapps/<project folder>/
celerybeat.conf
command=/home/<user>/pyapps/venv/bin/celery -A <project settings folder> worker --loglevel=INFO
directory=/home/<user>/pyapps/<project folder>/
在这里,我指的 "project settings folder" 是包含 celery.py 的文件夹。
而 "project folder" 则是包含 manage.py 的文件夹。
我另外犯的一个错误是将 supervisor 安装在虚拟环境之外。我认为这可能产生了影响。
我已经创建了一个包含在 Ubuntu 22.04 上部署 Django、Redis 和 Celery 步骤的 Git 仓库,希望对其他人有所帮助。
https://github.com/krishnasoyjaudah/django-celery-redis-ubuntu
感谢大家的回复。
英文:
I found the error. My paths in celery.conf and celerybeat.conf were wrong.
celery.conf
command=/home/<user>/pyapps/venv/bin/celery -A <project settings folder> worker --loglevel=INFO
directory=/home/<user>/pyapps/<project folder>/
celerybeat.conf
command=/home/<user>/pyapps/venv/bin/celery -A <project settings folder> worker --loglevel=INFO
directory=/home/<user>/pyapps/<project folder>/
By project settings folder, I mean the folder that contains celery.py
By project folder, I mean the folder that contains manage.py
Another mistake I had made was I installed supervisor outside of the virtual environment. I think that had an impact.
I have created a git with the steps to deploy django, redis, celery on Ubuntu 22.04. Hopefully it will help others.
https://github.com/krishnasoyjaudah/django-celery-redis-ubuntu
Thank you all for your replies.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论