简体   繁体   中英

Celery 3 worker with a Django Herokuapp

I have a Django app that's got a project layout as described in the two scoops of django book.

├── CONTRIBUTORS.txt
├── LICENSE.txt
├── Procfile
├── README.rst
├── docs
├── requirements
│   ├── base.txt
│   ├── local.txt
│   ├── production.txt
│   └── test.txt
├── requirements.txt
└── PROJECT_NAME
    ├── __init__.py
    ├── app_name
    │   ├── __init__.py
    │   ├── models.py
    │   ├── views.py
    ├── manage.py
    ├── PROJECT_NAME_APP
    │   ├── __init__.py
    │   ├── celery.py
    │   ├── settings
    │   │   ├── __init__.py
    │   │   ├── base.py
    │   │   ├── local.py
    │   │   ├── production.py
    │   │   ├── test.py
    │   ├── urls.py
    │   ├── wsgi.py
    ├── static
    │   ├── css
    │   ├── fonts
    │   └── js
    └── templates

Deploying this to Heroku was slightly different to the Heroku docs as the file paths to the application were slightly different. However I modified them to the appropriate paths and things worked fine. Notice that my PROJECT_NAME_APP folder is actually named differently to my top level project folder, this is a change I made in order for the app to run on Heroku

I'm now attempting to add a Celery worker. I'm using a version of Celery that doesn't require the django-celery package. In my Procfile I have this line to define the worker:

worker: celery worker --app=project_name.project_name_app.celery

The celery file I created follows the celery docs and is located in PROJECT_NAME_APP.

import os

from __future__ import absolute_import
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name_app.settings.local')
app = Celery('project_name_app')
app.config_from_object('django.conf:settings')

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

I've come across some advice about setting DJANGO_SETTINGS_MODULE in Heroku which I did but that seemed to break other things so I unset it.

The error I'm getting at the moment is:

ImportError: Could not import settings 'project_name_app.settings.local' (Is it on sys.path?): No module named project_name_app.settings.local

I understand what the error is saying but I'm finding it hard to debug on Heroku . I've tried using foreman start locally to debug my Procfile but again due to the local vs production settings it's throwing errors to do with my SECRET_KEY .

Can anyone see why my settings are throwing errors when running in production?

So having played with the settings for hours I noticed in my wsgi.py file

import os
from os.path import abspath, dirname
from sys import path

SITE_ROOT = dirname(dirname(abspath(__file__)))
path.append(SITE_ROOT)

# We defer to a DJANGO_SETTINGS_MODULE already in the environment. This breaks
# if running multiple sites in the same mod_wsgi process. To fix this, use
# mod_wsgi daemon mode with each site in its own daemon process, or use
# os.environ["DJANGO_SETTINGS_MODULE"] = "jajaja.settings"

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "restaurants_app.settings.production")

# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
application = Cling(get_wsgi_application())

It creates a SITE_ROOT var then adds it to the path. I did the same thing in my celery.py file and it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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