Search code examples
pythonflaskcroncelerycelerybeat

How to start Celery Beat on Flask


I currently have a flask code that looks like this app.py

from services.celery_maker import make_celery
from flask import Flask
from datetime import timedelta

template_dir = os.path.abspath('./build/')
app = Flask(__name__, template_folder=template_dir, static_folder=os.path.abspath("./build/static"))

app.config['ERROR_404_HELP'] = False
app.config['SECRET_KEY'] = config.get("DEFAULT", "SECRET_KEY")
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(minutes=5)

app.config.update(
    CELERY_BROKER_URL='redis://127.0.0.1:6379/0',
    CELERY_RESULT_BACKEND='redis://127.0.0.1:6379/0',
)

app.config['CELERYBEAT_SCHEDULE'] = {
    # Executes every minute
    'periodic_task-every-minute': {
        'task': 'periodic_task',
        'schedule': timedelta(seconds=30)
    }
}



@app.route('/')
def view():
    return "Hello, Flask is up and running!"

@celery.task(name ="periodic_task")
def periodic_task():
    print('Hi! from periodic_task')
    logger.info("Hello! from periodic task")

if __name__ == "__main__":
    app.run(debug = True)

I have my celery maker in a different file to stop relative import errors

services.celery_maker.py

from celery import Celery


def make_celery(app_name=__name__):
    backend = "redis://localhost:6379/0"
    broker = backend.replace("0", "1")

    return Celery(app_name, backend=backend, broker=broker)


celery = make_celery()

celery worker sees my task but it doesnt run at all I don't know what is going on


Solution

  • for the periodic tasks in celery, you need to use celery beat also, beats will schedule the tasks and workers will execute the task, in short along with the worker you need to start the celery-beat also

    celery beat -A <path_to_worker_created_under_celery_app> -l info
    

    for eg. in your casecelery beat -A services.celery_maker.celery -l info