2026-02-21 18:38:19 +00:00
|
|
|
"""
|
|
|
|
|
Celery application factory for the Crafting Shop application.
|
|
|
|
|
Follows the same application factory pattern as Flask.
|
|
|
|
|
"""
|
|
|
|
|
from celery import Celery
|
|
|
|
|
from flask import Flask
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def make_celery(app: Flask) -> Celery:
|
|
|
|
|
"""
|
|
|
|
|
Create and configure a Celery application with Flask context.
|
2026-02-24 16:19:15 +00:00
|
|
|
|
2026-02-21 18:38:19 +00:00
|
|
|
Args:
|
|
|
|
|
app: Flask application instance
|
2026-02-24 16:19:15 +00:00
|
|
|
|
2026-02-21 18:38:19 +00:00
|
|
|
Returns:
|
|
|
|
|
Configured Celery application instance.
|
|
|
|
|
"""
|
|
|
|
|
# Create Celery app with Flask app name
|
|
|
|
|
celery_app = Celery(
|
|
|
|
|
app.import_name,
|
|
|
|
|
broker=app.config["CELERY"]["broker_url"],
|
2026-02-24 16:19:15 +00:00
|
|
|
backend=app.config["CELERY"]["result_backend"],
|
2026-02-21 18:38:19 +00:00
|
|
|
)
|
2026-02-24 16:19:15 +00:00
|
|
|
|
2026-02-21 18:38:19 +00:00
|
|
|
# Update configuration from Flask config
|
|
|
|
|
celery_app.conf.update(app.config["CELERY"])
|
2026-02-24 16:19:15 +00:00
|
|
|
|
2026-02-21 18:38:19 +00:00
|
|
|
# Set up Flask application context for tasks
|
|
|
|
|
# This ensures tasks have access to Flask extensions (db, etc.)
|
|
|
|
|
class ContextTask(celery_app.Task):
|
|
|
|
|
"""Celery task that runs within Flask application context."""
|
2026-02-24 16:19:15 +00:00
|
|
|
|
2026-02-21 18:38:19 +00:00
|
|
|
def __call__(self, *args, **kwargs):
|
|
|
|
|
with app.app_context():
|
|
|
|
|
return self.run(*args, **kwargs)
|
2026-02-24 16:19:15 +00:00
|
|
|
|
2026-02-21 18:38:19 +00:00
|
|
|
celery_app.Task = ContextTask
|
2026-02-24 16:19:15 +00:00
|
|
|
|
2026-02-21 18:38:19 +00:00
|
|
|
# Auto-discover tasks in the tasks module
|
2026-02-24 16:19:15 +00:00
|
|
|
celery_app.autodiscover_tasks(["app.celery.tasks"])
|
|
|
|
|
|
2026-02-21 18:38:19 +00:00
|
|
|
# Configure Beat schedule
|
|
|
|
|
from .beat_schedule import configure_beat_schedule
|
2026-02-24 16:19:15 +00:00
|
|
|
|
2026-02-21 18:38:19 +00:00
|
|
|
configure_beat_schedule(celery_app)
|
2026-02-24 16:19:15 +00:00
|
|
|
|
2026-02-21 18:38:19 +00:00
|
|
|
# Import tasks to ensure they're registered
|
2026-02-24 16:19:15 +00:00
|
|
|
from .tasks import example_tasks # noqa: F401
|
|
|
|
|
|
2026-02-21 18:38:19 +00:00
|
|
|
print(f"✅ Celery configured with broker: {celery_app.conf.broker_url}")
|
|
|
|
|
print(f"✅ Celery configured with backend: {celery_app.conf.result_backend}")
|
|
|
|
|
print(f"✅ Beat schedule configured with {len(celery_app.conf.beat_schedule)} tasks")
|
2026-02-24 16:19:15 +00:00
|
|
|
|
2026-02-21 18:38:19 +00:00
|
|
|
return celery_app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Global Celery instance
|
|
|
|
|
celery = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def init_celery(app: Flask) -> Celery:
|
|
|
|
|
"""
|
|
|
|
|
Initialize the global celery instance with Flask app.
|
|
|
|
|
This should be called in create_app() after Flask app is created.
|
2026-02-24 16:19:15 +00:00
|
|
|
|
2026-02-21 18:38:19 +00:00
|
|
|
Args:
|
|
|
|
|
app: Flask application instance
|
2026-02-24 16:19:15 +00:00
|
|
|
|
2026-02-21 18:38:19 +00:00
|
|
|
Returns:
|
|
|
|
|
Configured Celery application instance
|
|
|
|
|
"""
|
|
|
|
|
global celery
|
|
|
|
|
celery = make_celery(app)
|
|
|
|
|
return celery
|