Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Adrián Ribao
Cartero
Commits
e5348218
Commit
e5348218
authored
Oct 02, 2014
by
Adrián Ribao
Browse files
not using subtasks for sending email. Disabling rate limit.
parent
94714495
Changes
2
Hide whitespace changes
Inline
Side-by-side
cartero/tasks.py
View file @
e5348218
# -*- 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
cartero/utils/sending.py
View file @
e5348218
...
...
@@ -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
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment