I’ve been working on getting redbeat setup & working with my Flask app but I cant seem to get it to start correctly. When I run the command
celery -A celery_config.celery beat --scheduler redbeat.RedBeatScheduler
I get this error originating from here which makes it seem like redis_url in conf is None
File "/Users/test/Documents/Projects/Project/.venv/lib/python3.9/site-packages/redbeat/schedulers.py", line 401, in setup_schedule
client = get_redis(self.app)
File "/Users/test/Documents/Projects/Project/.venv/lib/python3.9/site-packages/redbeat/schedulers.py", line 124, in get_redis
elif conf.redis_url.startswith('redis-sentinel') and 'sentinels' in redis_options:
AttributeError: 'NoneType' object has no attribute 'startswith'
I believe my setup has to be wrong somehow for this error to occur, but I’m not sure where/how. Any pointers/help is greatly appreciated to get this thing up and running. Here is some trimmed-down relevant code for how I’ve set it up & how I’m trying to use it.
application.py file where I create the Flask App and tie application.celery_app to the celery instance I create in the celery_config file
from flask import Flask, redirect, url_for
from extensions import ma, jwt, db, socketio
import config.const as CONSTANTS
from web.authentication import auth
from web.tasks import tasks
from flask_cors import CORS
from celery_config import init_celery
def create_app():
application = Flask(__name__)
configure_app(application)
application.celery_app = init_celery(application)
return application
def configure_app(application):
application.config['SQLALCHEMY_DATABASE_URI'] = CONSTANTS.DB_CONNECTION_STRING
application.config['SECRET_KEY'] = CONSTANTS.SECRET
application.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
application.config['PREFERRED_URL_SCHEME'] = 'https'
application = create_app()
if __name__ == '__main__':
application.run(debug=True, use_reloader=False, port=5009)
Here is celery_config.py where celery is created and configured. I’m running redis locally in a terminal window.
from celery import Celery
celery = None
def make_celery(app):
app.config.update(
CELERY_BROKER_URL='redis://localhost:6379/0',
CELERY_RESULT_BACKEND='redis://localhost:6379/0',
REDBEAT_REDIS_URL='redis://localhost:6379/1'
)
celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update({
'beat_scheduler': 'redbeat.RedBeatScheduler',
'beat_schedule': {},
'redbeat_key_prefix': 'redbeat:',
'redbeat_redis_url': app.config['REDBEAT_REDIS_URL']
})
return celery
def init_celery(app):
global celery
celery = make_celery(app)
return celery
And lastly this is the file I would be adding, editing, and removing tasks. I’m guessing the error is coming from here where I create new entries using RedBeatSchedulerEntry()
from celery.schedules import crontab
from flask import Blueprint, jsonify, request
from models.task import Task
from services.login_decorator import duo_login_required
import services.helpers as helpers
from datetime import datetime, timedelta
from redbeat import RedBeatSchedulerEntry
from celery_config import celery as celery_app
tasks = Blueprint('tasks', __name__)
@tasks.route('/create_task', methods=['POST'])
def create_new_task():
try:
body = request.json
task = Task()
if body['interval'] == True:
task.name = body['name']
task.type = body['type']
task.url = body['url']
task.interval = True
task.interval_time = body['time']
task.update()
key = task.id
args = [task.id, task.url]
schedule = timedelta(minutes=int(task.interval_time))
entry = RedBeatSchedulerEntry(key, 'myapp.tasks.task_function', schedule, args=args, kwargs={},
app=celery_app)
entry.save()
return jsonify({'id': task.id, 'name': task.name, 'type': task.type, 'interval_time': body['time'],'interval': True, 'url': task.url}), 200