Skip to content

Commit

Permalink
Merge 71d87c2 into e67531c
Browse files Browse the repository at this point in the history
  • Loading branch information
psychok7 committed Aug 14, 2015
2 parents e67531c + 71d87c2 commit bfb6a35
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions mailer/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
import smtplib
import logging

import lockfile
from socket import error as socket_error

from django.conf import settings
from django.core.mail import get_connection
from lockfile import FileLock, AlreadyLocked, LockTimeout
from lockfile.mkdirlockfile import MkdirLockFile

from mailer.models import Message, MessageLog, RESULT_SUCCESS, RESULT_FAILURE

Expand All @@ -18,6 +19,8 @@
# default behavior is to never wait for the lock to be available.
LOCK_WAIT_TIMEOUT = getattr(settings, "MAILER_LOCK_WAIT_TIMEOUT", -1)

LOCK_TYPE = getattr(settings, "MAILER_LOCK_TYPE", "FileLock")


def prioritize():
"""
Expand Down Expand Up @@ -73,14 +76,20 @@ def _throttle_emails():

def acquire_lock():
logging.debug("acquiring lock...")
lock = lockfile.FileLock("send_mail")

if LOCK_TYPE == 'MkdirLockFile':
# mkdir(2)() system call as the basic lock mechanism
lock = MkdirLockFile("send_mail")
else:
# Default Lock Type
lock = FileLock("send_mail")

try:
lock.acquire(LOCK_WAIT_TIMEOUT)
except lockfile.AlreadyLocked:
except AlreadyLocked:
logging.debug("lock already in place. quitting.")
return False, lock
except lockfile.LockTimeout:
except LockTimeout:
logging.debug("waiting for the lock timed out. quitting.")
return False, lock
logging.debug("acquired.")
Expand Down

0 comments on commit bfb6a35

Please sign in to comment.