djangoubunturediscelery

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.

On windows, everything works fine. However when I deploy on Ubuntu 22.04, I get the following error in the log.

enter image description here

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.


Solution

  • 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.

    Thank you all for your replies.