diff options
author | Magnus Hagander | 2016-01-08 13:04:17 +0000 |
---|---|---|
committer | Magnus Hagander | 2016-01-08 13:04:17 +0000 |
commit | ff1e116cb196713e8b51b92721c5a3ff510b3a5d (patch) | |
tree | 3cf7f651b156dbc46154b2668f2da3a28cffa021 /postgresqleu/mailqueue | |
parent | 7466589e3cf2af4071741ff016e8e7726e65e6a8 (diff) |
Move send_queued_mail script to be internal command
Diffstat (limited to 'postgresqleu/mailqueue')
-rw-r--r-- | postgresqleu/mailqueue/management/__init__.py | 0 | ||||
-rw-r--r-- | postgresqleu/mailqueue/management/commands/__init__.py | 0 | ||||
-rwxr-xr-x | postgresqleu/mailqueue/management/commands/send_queued_mail.py | 34 |
3 files changed, 34 insertions, 0 deletions
diff --git a/postgresqleu/mailqueue/management/__init__.py b/postgresqleu/mailqueue/management/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/postgresqleu/mailqueue/management/__init__.py diff --git a/postgresqleu/mailqueue/management/commands/__init__.py b/postgresqleu/mailqueue/management/commands/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/postgresqleu/mailqueue/management/commands/__init__.py diff --git a/postgresqleu/mailqueue/management/commands/send_queued_mail.py b/postgresqleu/mailqueue/management/commands/send_queued_mail.py new file mode 100755 index 00000000..81850679 --- /dev/null +++ b/postgresqleu/mailqueue/management/commands/send_queued_mail.py @@ -0,0 +1,34 @@ +# Script to send off all queued email. +# +# This script is intended to be run frequently from cron. We queue things +# up in the db so that they get automatically rolled back as necessary, +# but once we reach this point we're just going to send all of them one +# by one. +# +from django.core.management.base import BaseCommand, CommandError +from django.db import connection + +import smtplib + +from postgresqleu.mailqueue.models import QueuedMail + +class Command(BaseCommand): + help = 'Send queued mail' + + def handle(self, *args, **options): + # Grab advisory lock, if available. Lock id is just a random number + # since we only need to interlock against ourselves. The lock is + # automatically released when we're done. + curs = connection.cursor() + curs.execute("SELECT pg_try_advisory_lock(72181378)") + if not curs.fetchall()[0][0]: + raise CommandException("Failed to get advisory lock, existing send_queued_mail process stuck?") + + for m in QueuedMail.objects.all(): + # Yes, we do a new connection for each run. Just because we can. + # If it fails we'll throw an exception and just come back on the + # next cron job. And local delivery should never fail... + smtp = smtplib.SMTP("localhost") + smtp.sendmail(m.sender, m.receiver, m.fullmsg.encode('utf-8')) + smtp.close() + m.delete() |