Search code examples
flaskherokuceleryworker

Error starting Celery Worker from Parent Folder


I'm trying to deploy my Flask project on Heroku, but I'm having problems starting my Celery worker from other directory other than the src.

I've got my Flask project with the following tree:

.project
├── Procfile
├── README.md
├── requirements.txt
└── src
    ├── app.py
    ├── blueprints
    │   ├── __init__.py
    │   ├── error_pages.py
    │   └── profile.py
    ├── static
    │   └── ...
    └── templates
        └── ...

In app.py I have:

from flask import Flask
from celery import Celery
import blueprints

app = Flask(__name__)

# Load the config file
app.config.from_pyfile('config.cfg')

# Initialises celery background tasks handler
celery = Celery(main=app.name, backend=app.config['REDIS_URL'], broker=app.config['REDIS_URL'],
                include=['blueprints.profile'])
celery.conf.update(app.config)
app.celery = celery

If I am at .project/src and I use:

celery -A app.celery worker -l INFO

My worker starts with no problem. But I cannot figure out how to start my worker from the root, for example.

This is how my Procfile is looking at the moment.

web: gunicorn --chdir ./src app:app
worker: celery -A ??????

Any thoughts?


Solution

  • I solved my problem by adding the following __init__.py in my src directory:

    import sys
    import os
    
    # Adding the 'src' directory to the python path
    sys.path.insert(0, os.path.join(os.getcwd(), 'src'))
    

    And then calling the worker using:

    celery -A src.app.celery worker -l INFO
    

    Now, this is my Heroku Procfile

    web: gunicorn --chdir ./src app:app
    worker: celery -A src.app.celery worker