Error
[2024-02-20 19:42:00,874: INFO/ForkPoolWorker-4] Task em_app.celery_file.mark_feedback_as_deleted[4914a06d-ea74-4d39-a3dc-58cd2c1240ed] succeeded in 0.05288426599872764s: UnboundExecutionError('Could not locate a bind configured on mapper Mapper[Question(em_questions)], SQL expression or this Session.')
My COde
import os
from datetime import datetime, timedelta
from celery import Celery
from sqlalchemy.orm import Session
from fastapi import Depends
from em_app.dependencies import get_db
from dotenv import load_dotenv
from em_app.models.feedback import EmployeeFeedBack, Answer, Question
load_dotenv()
broker_url = f"redis://:{os.getenv('REDIS_PASSWORD')}@{os.getenv('REDIS_HOST')}:{os.getenv('REDIS_PORT', '13226')}/0"
app = Celery('tasks', broker=broker_url)
broker_connection_retry_on_startup = True
@app.task
def mark_feedback_as_deleted():
try:
with Session() as db:
today = datetime.now()
cutoff_date = datetime(today.year, today.month, 19)
feedbacks_to_delete = db.query(Question).filter(
Question.is_deleted.is_(False),
Question.created_at <= cutoff_date
).all()
# Mark fetched feedbacks as deleted
for feedback in feedbacks_to_delete:
feedback.is_deleted = True
feedback.is_active = False
# Commit changes to the database
db.commit()
print("Feedbacks marked as deleted:", feedbacks_to_delete)
except Exception as e:
return e
app.conf.update(
broker_url=broker_url,
timezone='UTC',
beat_schedule={
'mark-feedback-as-deleted': {
'task': 'em_app.celery_file.mark_feedback_as_deleted', # Updated task reference
'schedule': timedelta(minutes=1), # Run the task daily
},
}
)
if __name__ == '__main__':
app.start()
How I Solve This Error