I’m building a customer nurturing application which will send an email when some time based condition is met on an order such as:
50% OF Order time_to_ship elapsed10 DAYS AFTER Order ship_date5 DAYS AFTER NewsletterSubscriber signup_date
I’m trying to determine how to best fire and handle these events. I have no experience dealing with this problem so any input is appreciated.
Current idea: poll for matches for each handler and ensure each handler-match combo fires only once via an extra table. In this case, I wouldn’t be firing events that I could listen to elsewhere, each handler is doing its own query. 3 handlers on 5 days after ... would fire 3 queries.
# build table that stores which handlers have fired for a given object
class HandlerFired(models.Model):
ctype = models.ForeignKey(ContentType, related_name="handlerfired_ctype")
id = models.IntegerField()
handler = models.ForeignKey(Handler)
class Handler(models.Model):
ctype = models.ForeignKey(ContentType)
condition = ...
# cron job every day
for handler in Handler.objects.filter(is_active=True):
objects = handler.run_query().exclude(
handlerfired=handler, handlerfired_ctype=handler.ctype)
handler.handle(objects) # do whatever it's supposed to do with given objects.
# handle() would also make sure the `HandlerFired` table is populated with a record.
The idea has given me a bit more grief / options than I imagined when I started the project. It does seem like a pretty common problem to have time driven events.
Alternatively, I could do a daily cron job of events not handlers but I think I would have to keep track of every event fired to ensure I don’t fire 2, or that one isn’t skipped (say 20% event skipped because of downtime). This would store thousands more records than recording handlers that have fired but feels more like a true event emitter.
Django async is an async execution queue. You can schedule a future event which will then check to see if the condition is valid at that time. For example, when the order is shipped schedule an event that fires 10 days later and when that executes it can make sure about any other conditions before sending the email.
http://pypi.python.org/pypi/django-async/
Django async has built in the run once behaviour and if the queue stops for any reason it will catch up on past jobs when it’s restarted. Transient errors (like the SMTP server being down) will be automatically retried.