Commit e5348218 authored by Adrián Ribao's avatar Adrián Ribao
Browse files

not using subtasks for sending email. Disabling rate limit.

parent 94714495
# -*- coding: utf-8 -*-
from celery import task, group
from celery import task
from cartero.utils.sending import CampaignDeliver
from cartero.models import Campaign
from django.utils import timezone
# For future rate limit:
# http://blog.gregburek.com/2011/12/05/Rate-limiting-with-decorators/
# def RateLimited(maxPerSecond):
# minInterval = 1.0 / float(maxPerSecond)
# def decorate(func):
# lastTimeCalled = [0.0]
# def rateLimitedFunction(*args,**kargs):
# elapsed = time.clock() - lastTimeCalled[0]
# leftToWait = minInterval - elapsed
# if leftToWait>0:
# time.sleep(leftToWait)
# ret = func(*args,**kargs)
# lastTimeCalled[0] = time.clock()
# return ret
# return rateLimitedFunction
# return decorate
@task.task
def check_campaigns():
now = timezone.now()
campaigns_to_be_sent = Campaign.objects.filter(
start__lte = now,
status = Campaign.READY_TO_BE_SENT
)
start__lte=now,
status=Campaign.READY_TO_BE_SENT
)
for campaign in campaigns_to_be_sent:
# Change status to sending
......@@ -18,20 +36,33 @@ def check_campaigns():
campaign.save()
cd = CampaignDeliver(campaign)
# Set the rate limit
task.control.rate_limit('cartero.tasks.send_message', campaign.rate_limit)
contacts = cd.get_contacts()
g = group(send_message.s(cd, contact) for contact in contacts)
res = g()
res.get()
for contact in contacts:
cd.send_to_contact(contact)
campaign.status = Campaign.SENT
campaign.save()
# Set the rate limit
# task.control.rate_limit('cartero.tasks.send_message',
#campaign.rate_limit)
@task.task
def send_message(campaign_deliver, contact):
campaign_deliver.send_to_contact(contact)
return True
#contacts = cd.get_contacts()
# Using chords to send emails and process campaign
# Send message subtask is immutable, we don't care about it's result.
#job = group(send_message.s(cd, contact) for contact in contacts)
#job.apply_async(link=finish_campaign.s(campaign))
# res = g()
# res.get()
# campaign.status = Campaign.SENT
# campaign.save()
#@task.task
#def send_message(campaign_deliver, contact):
#campaign_deliver.send_to_contact(contact)
#return True
......@@ -84,10 +84,11 @@ class CampaignDeliver(object):
return text
def send_to_contact(self, contact):
msg = self.compose_email(contact)
msg.send()
contact.sent = True
contact.save()
if contact.sent == False:
msg = self.compose_email(contact)
msg.send()
contact.sent = True
contact.save()
def compose_email(self, contact):
html = self.campaign.email_template.html
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment