django-orchestra/orchestra/contrib/mailer/tasks.py

24 lines
595 B
Python
Raw Normal View History

2015-05-09 18:53:23 +00:00
from datetime import timedelta
2015-05-05 19:42:55 +00:00
from django.utils import timezone
from celery.task.schedules import crontab
2015-05-04 19:52:53 +00:00
2015-05-05 19:42:55 +00:00
from orchestra.contrib.tasks import task, periodic_task
2015-05-04 19:52:53 +00:00
2015-05-09 18:53:23 +00:00
from . import engine, settings
2015-05-05 19:42:55 +00:00
@task
def send_message(message):
message.save()
2015-05-05 19:42:55 +00:00
engine.send_message(message)
@periodic_task(run_every=crontab(hour=7, minute=30))
2015-05-04 19:52:53 +00:00
def cleanup_messages():
2015-05-05 19:42:55 +00:00
from .models import Message
delta = timedelta(days=settings.MAILER_MESSAGES_CLEANUP_DAYS)
now = timezone.now()
epoch = (now-delta)
2015-05-09 18:53:23 +00:00
return Message.objects.filter(state=Message.SENT, last_retry__lt=epoch).delete()