Celery-Django as Daemon: Settings not found
Asked Answered
B

5

8

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
Brotherson answered 11/7, 2014 at 7:38 Comment(4)
How does your celery.py file looks like?Bustee
You can see in my first post the celery.py. Have you an idea?Brotherson
Do you have this line [CELERYD_CHDIR="/some_dir/myapp/"] in /etc/default/celeryd ?Charlacharlady
It's been a few years since I tried to configure Celery. I was really hoping it had gotten better, but looks like it's the same horribleness as always.Savarin
B
5

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.

Brotherson answered 23/7, 2014 at 5:42 Comment(0)
M
4

The problem is most likely that celeryd can't find your Django settings file because myapp.settings isn't in the the $PYTHONPATH then the application runs.

From what I recall, Python will look in the $PYTHONPATH as well as the local folder when importing files. When celeryd runs, it likely checks the path for a module app, doesn't find it, then looks in the current folder for a folder app with an __init__.py (i.e. a python module).

I think that all you should need to do is add this to your /etc/default/celeryd file:

export $PYTHONPATH:path/to/your/app
Marozas answered 19/7, 2014 at 13:39 Comment(0)
C
1

Below method does not helps to run celeryd, rather helps to run celery worker as a service which will be started at boot time.

commands like this sudo service celery status also works.

celery.conf

# This file sits in /etc/init
description "Celery for example"
start on runlevel [2345]
stop on runlevel [!2345]
#Send KILL after 10 seconds
kill timeout 10

script

#project(working_ecm) and Vitrualenv(working_ecm/env) settings
chdir /home/hemanth/working_ecm
exec /home/hemanth/working_ecm/env/bin/python manage.py celery worker -B -c 2 -f /var/log/celery-ecm.log --loglevel=info >> /tmp/upstart-celery-job.log 2>&1
end script

respawn
Charlacharlady answered 19/7, 2014 at 14:48 Comment(2)
Why after I do service celeryd stop, and then service celeryd status, it says that my worker is still running?Brotherson
It may be a different service from a package you installed. to confirm try renaming the /etc/init/celery.conf to /etc/init/testxyz.conf ... and try [ sudo service testxyz start].Charlacharlady
J
1

In your second tutorial they set the django_settings variable to:

export DJANGO_SETTINGS_MODULE="settings"

This could be a reason why your settings is not found in case it changes to directory "/home/ubuntu/myapp-folder/" Then you defined your app with "myapp" and then you say settings is in "myapp.settings"

This could lead to the fact that it searchs the settings file in

"/home/ubuntu/myapp-folder/myapp/myapp/settings"

So my suggestion is to remove the "myapp." in the DJANGO_SETTINGS_MODULE variable and dont forget quotation marks

Jon answered 22/7, 2014 at 8:58 Comment(0)
S
0

I'd like to add an answer for anyone stumbling on this more recently.

I followed the getting started First Steps guide to a tee with Celery 4.4.7, as well as the Daemonization tutorial without luck.

My initial issue:

  • celery -A app_name worker -l info works without issue (actual celery configuration is OK).
  • I could start celeryd as daemon and status command would show OK, but couldn't receive tasks. Checking logs, I saw the following: [2020-11-01 09:33:15,620: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 111] Connection refused.
  • This was an indication that celeryd was not connecting to my broker (redis). Given CELERY_BROKER_URL was already set in my configuration, this meant my celery app settings were not being pulled in for the daemon process.
  • I tried sudo C_FAKEFORK=1 sh -x -l -E /etc/init.d/celeryd start to see if any of my celery settings were pulled in, and i noticed that app was set to default default (not the app name specified as CELERY_APP in /etc/default/celeryd

Since celery -A app_name worker -l info worked, fixed the issue by exporting CELERY_APP in /etc/default/celeryd/, instead of just setting the variable per documentation.

TL;DR If celery -A app_name worker -l info works (replace app_name with what you've defined in the Celery first steps guide), and sudo C_FAKEFORK=1 sh -x -l -E /etc/init.d/celeryd start does not show your celery app settings being pulled in, add the following to the end of your /etc/default/celeryd:

export CELERY_APP="app_name"

Strephonn answered 1/11, 2020 at 15:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.