[print_status] On évite de crasher si le serveur SMTP nous demande d'attendre parce qu'on envoie trop de mails.4~

Ignore-this: f87d2d2dcc2cbbad5371478782157c66

darcs-hash:20121112184906-8ef3c-b962b40959a3bf17bc25a6b908f57579b5a3c9e4.gz
This commit is contained in:
Sylvain Boilard 2012-11-12 19:49:06 +01:00
parent 53430d490e
commit 80be53e2f5

View file

@ -19,7 +19,7 @@ sys.path.append("/usr/scripts/gestion")
import time
import httplib2
import re
from smtplib import SMTP
import smtplib
from ldap_crans import crans_ldap
last_print_filename = "/var/run/print_status/last_print.txt"
@ -84,6 +84,19 @@ def buildDate(date):
hour, minute, _ = hms.split(':')
return u"le " + day + u" " + litMonths[int(month) - 1] + u" " + year + u" à " + hour + u":" + minute
def sendMail(from_addr, to_addrs, mail_content):
"""Wrapper pour smtplib.sendmail(). Permet d'attendre un peu en cas d'erreur temporaire (typiquement, si on essaie d'envoyer trop de mails à la minute)."""
conn = SMTP(smtp_server)
try:
conn.sendmail(from_addr, to_addrs, mail_content)
except smtplib.SMTPSenderRefused as (code, _, _):
if code == 450:
time.sleep(65)
conn.sendmail(from_addr, to_addrs, mail_content)
else:
raise
conn.quit()
http = httplib2.Http()
# On récupère la liste des tâches et on enlève les entêtes.
@ -128,24 +141,19 @@ for item in task_list:
else:
target = db_query['club'][0]
full_name = u"Club " + target.nom()
send_to = []
for aid in target.imprimeurs():
send_to.append(db.search("aid=" + aid)['adherent'][0].mail() + u"@crans.org")
send_to = map(lambda aid : db.search("aid=" + aid)['adherent'][0].mail() + u"@crans.org", target.imprimeurs())
historique = target.historique()
historique.reverse() # Ce qu'on cherche a des chances d'être récent et donc d'être à la fin de l'historique.
for hist_line in historique:
if match_taskID.search(hist_line):
filename = match_doc_name.search(hist_line).group()[1:]
break
conn = SMTP(smtp_server)
if result == "OK":
for dest in send_to:
mail_content = ok_mail % (error_send_to, dest, filename, full_name, filename, date, error_send_to)
conn.sendmail(error_send_to, dest, mail_content.encode("utf-8"))
mail_content = ok_mail % (error_send_to, u", ".join(send_to), filename, full_name, filename, date, error_send_to)
sendMail(error_send_to, send_to, mail_content.encode("utf-8"))
else:
mail_content = error_mail % (error_send_to, error_send_to, filename, taskID, full_name, user, date, result, u"\n".join(send_to))
conn.sendmail(error_send_to, error_send_to, mail_content.encode("utf-8"))
conn.quit()
sendMail(error_send_to, error_send_to, mail_content.encode("utf-8"))
# Éventuellement, on met à jour le numéro de la dernière tâche traitée.
new_last = task_list[0].split(',', 1)[0]