Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Celery-Django as Daemon: Settings not found

By following this tutorial, I have now a Celery-Django app that is working fine if I launch the worker with this command: celery -A myapp worker -n worker1.%h

in my Django settings.py, I set all parameters for Celery (IP of the messages broker, etc...). Everything is working well.

My next step now, is to run this app as a Daemon. So I have followed this second tutorial and everything is simple, except now, my Celery parameters included in settings.py are not loaded. By example, messages broker IP is set to 127.0.0.1 but in my settings.py, I set it at an other IP address.

In the tutorial, they say:

make sure that the module that defines your Celery app instance also sets a default value for DJANGO_SETTINGS_MODULE as shown in the example Django project in First steps with Django.

So I made it sure. I had in /etc/default/celeryd this:

export DJANGO_SETTINGS_MODULE="myapp.settings"

Still not working... So I also, had this line in /etc/init.d/celeryd, again not working. I don't know what to do anymore. Is someone has a clue?

EDIT: Here is my celery.py:

from __future__ import absolute_import

import os
from django.conf import settings
from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings')

app = Celery('myapp')

# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

EDIT #2: Here is my /etc/default/celeryd:

# Names of nodes to start
#   most will only start one node:
CELERYD_NODES="worker1.%h"

# Absolute or relative path to the 'celery' command:
CELERY_BIN="/usr/local/bin/celery"

# App instance to use
# comment out this line if you don't use an app
CELERY_APP="myapp"

# Where to chdir at start.
CELERYD_CHDIR="/home/ubuntu/myapp-folder/"

# Extra command-line arguments to the worker
CELERYD_OPTS=""

# %N will be replaced with the first part of the nodename.
CELERYD_LOG_FILE="/var/log/celery/%N.log"
CELERYD_PID_FILE="/var/run/celery/%N.pid"

# Workers should run as an unprivileged user.
#   You need to create this user manually (or you can choose
#   a user/group combination that already exists, e.g. nobody).
CELERYD_USER="ubuntu"
CELERYD_GROUP="ubuntu"

# If enabled pid and log directories will be created if missing,
# and owned by the userid/group configured.
CELERY_CREATE_DIRS=1

# Name of the projects settings module.
export DJANGO_SETTINGS_MODULE=myapp.settings
export PYTHONPATH=$PYTHONPATH:/home/ubuntu/myapp-folder
like image 891
ZouBi Avatar asked Jul 11 '14 07:07

ZouBi


1 Answers

All answers here could be a part of the solution but at the end, it was still not working. But I finally succeeded to make it work.

First of all, in /etc/init.d/celeryd, I have changed this line:

CELERYD_MULTI=${CELERYD_MULTI:-"celeryd-multi"}

by:

CELERYD_MULTI=${CELERYD_MULTI:-"celery multi"}

The first one was tagged as deprecated, could be the problem.

Moreover, I put this as option: CELERYD_OPTS="--app=myapp"

And don't forget to export some environments variables:

# Name of the projects settings module.
export DJANGO_SETTINGS_MODULE="myapp.settings"
export PYTHONPATH="$PYTHONPATH:/home/ubuntu/myapp-folder"

With all of this, it's now working on my side.

like image 65
ZouBi Avatar answered Sep 24 '22 02:09

ZouBi