-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
51 lines (45 loc) · 1.96 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# from celery import Celery
# from email_utils import send_email
# from models import EmailSchedule
# from sqlalchemy.orm import sessionmaker
# from sqlalchemy import create_engine
# from celery_config import celery_app
# SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db"
# engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False})
# SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# @celery_app.task
# def send_scheduled_email(email_schedule_id: int):
# db = SessionLocal()
# email_schedule = db.query(EmailSchedule).filter(EmailSchedule.id == email_schedule_id).first()
# if email_schedule:
# send_email(email_schedule.recipient, email_schedule.subject, email_schedule.body)
# email_schedule.is_sent = True
# db.commit()
# db.close()
from celery import Celery
from email_utils import send_email
from models import EmailSchedule
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from celery_config import celery_app
import asyncio
SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db"
engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False})
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
@celery_app.task
def send_scheduled_email(email_schedule_id: int):
db = SessionLocal()
try:
email_schedule = db.query(EmailSchedule).filter(EmailSchedule.id == email_schedule_id).first()
if email_schedule:
# Run the asynchronous send_email function
asyncio.run(send_email(email_schedule.recipient, email_schedule.subject, email_schedule.body))
email_schedule.is_sent = True
db.commit()
print(f"Email sent successfully to {email_schedule.recipient}")
else:
print(f"Email schedule with id {email_schedule_id} not found")
except Exception as e:
print(f"Error sending email: {str(e)}")
finally:
db.close()