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

24 lines
646 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
2015-11-09 19:07:06 +00:00
def send_message(message, connection=None):
message.save()
2015-11-09 19:07:06 +00:00
engine.send_message(message, connection=connection)
2015-05-05 19:42:55 +00:00
@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)
return Message.objects.filter(state=Message.SENT, created_at__lt=epoch).only('id').delete()