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

basic rate limit for sending emails

parent 0d362ea5
Pipeline #228 passed with stage
in 44 seconds
# -*- coding: utf-8 -*-
import itertools
import time
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()
......@@ -38,31 +24,15 @@ def check_campaigns():
cd = CampaignDeliver(campaign)
contacts = cd.get_contacts()
for contact in contacts:
cd.send_to_contact(contact)
# Make groups for sending each second
args = [iter(contacts)] * campaign.rate_limit_amount
groups = itertools.izip_longest(fillvalue=None, *args)
for group in groups:
for contact in group:
if contact is not None:
cd.send_to_contact(contact)
time.sleep(1)
campaign.status = Campaign.SENT
campaign.save()
# Set the rate limit
# task.control.rate_limit('cartero.tasks.send_message',
#campaign.rate_limit)
#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
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