我正在尝试执行事务性任务,如果无法发送电子邮件,该任务将回滚数据库更新。
下面是我的代码,任何人都可以建议这里做错了什么?
from celery.task import task
from django.core.mail import send_mail, send_mass_mail
from django.db import transaction
@task(name='communicator.process_emails')
@transaction.commit_manually
def process_emails():
from models import Comm_Queue
try:
message = []
for i in Comm_Queue.objects.filter(status='P').order_by('sender_email'):
message.append((i.subject, i.content, i.sender_email, [i.recipient_email]))
Comm_Queue.objects.filter(id=i.id).update(status='S')
if send_mass_mail(message):
transaction.commit()
except Exception, e:
print 'rolled back (exception): %s' % e.__str__()
transaction.rollback()