I have a celery task which creates and sends a monthy report for every user through SMTP. But after the task is received, sometime later, I get the error message from celery saying,
Traceback (most recent call last):
File "/home/mahi/Desktop/Project_new/backend/flask/lib/python3.10/site-packages/celery/app/trace.py", line 451, in trace_task
R = retval = fun(*args, **kwargs)
File "/home/mahi/Desktop/Project_new/backend/flask/lib/python3.10/site-packages/celery/app/trace.py", line 734, in __protected_call__
return self.run(*args, **kwargs)
File "/home/mahi/Desktop/Quantified Self V2/backend/application.py", line 383, in daily_alert
with smtplib.SMTP("smtp.mail.yahoo.com") as connection:
File "/usr/lib/python3.10/smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.10/smtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.10/smtplib.py", line 312, in _get_socket
return socket.create_connection((host, port), timeout,
File "/usr/lib/python3.10/socket.py", line 845, in create_connection
raise err
File "/usr/lib/python3.10/socket.py", line 833, in create_connection
sock.connect(sa)
TimeoutError: [Errno 110] Connection timed out
This is the code of my celery task and the setup:
application.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/1'
celery = Celery(application.name , broker=application.config['CELERY_BROKER_URL'])
celery.conf.update(application.config)
@celery.task()
def monthly_report():
with application.app_context():
file_loader = PackageLoader("application", "templates")
env = Environment(loader=file_loader)
# Extracting user
users = user.query.all()
# Creating charts for the trackers
for ak in users:
uid = ak.uid
udata = ak.query.filter_by(uid=uid).first()
trackers = tracker.query.filter_by(u_id=uid).all()
data={}
l=[]
for i in trackers:
logs = logtable.query.filter_by(t_id=i.tracker_id).all()
if(i.tracker_type == 'Numerical' ):
sum,cnt = 0,0
for j in logs:
sum+=int(j.value)
cnt+=1
res = sum/cnt
else:
d,highest,res = {},0,''
for j in logs:
if (str(j.value) in d.keys()):
d[j.value]+=1
else:
d[j.value]=1
for j,k in d.items():
if(k>highest):
highest = k
res = j
data['tracker_name'] = i.tracker_name
data['res'] = res
l.append(data)
# Creating Monthly Report in Html
rendered = env.get_template("report.html").render(udata=udata,trackerdata=l)
filename = "report.html"
with open(f"c{filename}", "w") as f:
f.write(rendered)
msg = MIMEMultipart()
msg["From"] = 'quantified.self.v2@gmail.com'
msg["To"] = ak.mail
msg["Subject"] = "Monthly Report"
body = MIMEText("Inside Body, Testing", "plain")
msg.attach(body)
with open(f"{filename}", "r") as f:
attachment = MIMEApplication(f.read(), Name=basename(filename))
attachment["Content-Disposition"] = 'attachment; filename="{}"'.format(basename(filename))
msg.attach(attachment)
with smtplib.SMTP("smtp.mail.yahoo.com") as connection:
connection.starttls()
connection.login(user='quantified.self.v2@gmail.com', password='mahee@154')
connection.send_message(
msg=msg,
from_addr='quantified.self.v2@gmail.com',
to_addrs=[ak.mail],
)
return "Monthly Report Send"
@celery.on_after_finalize.connect
def setup_periodic_tasks(sender, **kwargs):
sender.add_periodic_task(crontab(hour=10, minute=0), daily_alert.s(), name='Daily Alert')
sender.add_periodic_task(crontab(hour=11, minute=0, day_of_month=1), monthly_report.s(), name="Monthly Reports")
Here user is my sqlite table that contains user data, tracker is the table that contains tracker data, logtable is the table that contains logs for each tracker. So can someone throw light on what's the problem here?