Permalink
Browse files

Fix build

  • Loading branch information...
1 parent 3116c61 commit b95e9997d38405178e325c9b3e294b4cf40d6118 @paltman paltman committed Feb 24, 2014
Showing with 59 additions and 53 deletions.
  1. +5 −8 .travis.yml
  2. +0 −1 lint.sh
  3. +1 −1 notification/backends/__init__.py
  4. +2 −2 notification/compat.py
  5. +5 −1 notification/engine.py
  6. +46 −40 notification/lockfile.py
View
@@ -5,13 +5,10 @@ env:
- DJANGO=1.4.8
- DJANGO=1.5.4
install:
- - pip install -q Django==$DJANGO --use-mirrors
- - pip install -q django-nose --use-mirrors
- - pip install -q pylint --use-mirrors
- - pip install -q pep8 --use-mirrors
- - pip install -q pinax.checkers --use-mirrors
- - pip install -e . --use-mirrors
-before_script:
- - "./lint.sh"
+ - pip install -q Django==$DJANGO
+ - pip install -q django-nose
+ - pip install -q flake8
+ - pip install -e .
script:
+ - flake8 --max-line-length=100 --max-complexity=10 --statistics --benchmark notification
- python runtests.py
View
@@ -1 +0,0 @@
-pylint --rcfile=.pylintrc notification -r y && pep8 --ignore=E501 notification
@@ -3,7 +3,7 @@
from django.conf import settings
from django.core import exceptions
-from .base import BaseBackend
+from .base import BaseBackend # noqa
# pylint: disable-msg=C0103
View
@@ -18,9 +18,9 @@
try:
from urllib import quote
except ImportError:
- from urllib.parse import quote
+ from urllib.parse import quote # noqa
if six.PY3:
from threading import get_ident
else:
- from thread import get_ident
+ from thread import get_ident # noqa
View
@@ -20,7 +20,7 @@
LOCK_WAIT_TIMEOUT = getattr(settings, "NOTIFICATION_LOCK_WAIT_TIMEOUT", -1)
-def send_all(*args):
+def acquire_lock(*args):
if len(args) == 1:
lock = FileLock(args[0])
else:
@@ -36,7 +36,11 @@ def send_all(*args):
logging.debug("waiting for the lock timed out. quitting.")
return
logging.debug("acquired.")
+ return lock
+
+def send_all(*args):
+ lock = acquire_lock(*args)
batches, sent, sent_actual = 0, 0, 0
start_time = time.time()
View
@@ -316,6 +316,30 @@ def __init__(self, path, threaded=True):
"{}.{}{}".format(self.hostname, tname, self.pid)
)
+ def attempt_acquire(self, timeout, end_time, wait):
+ try:
+ os.mkdir(self.lock_file)
+ except OSError:
+ err = sys.exc_info()[1]
+ if err.errno == errno.EEXIST:
+ # Already locked.
+ if os.path.exists(self.unique_name):
+ # Already locked by me.
+ return
+ if timeout is not None and time.time() > end_time:
+ if timeout > 0:
+ raise LockTimeout
+ else:
+ # Someone else has the lock.
+ raise AlreadyLocked
+ time.sleep(wait)
+ else:
+ # Couldn"t create the lock for some other reason
+ raise LockFailed("failed to create %s" % self.lock_file)
+ else:
+ open(self.unique_name, "wb").close()
+ return
+
def acquire(self, timeout=None):
end_time = time.time()
if timeout is not None and timeout > 0:
@@ -327,28 +351,7 @@ def acquire(self, timeout=None):
wait = max(0, timeout / 10)
while True:
- try:
- os.mkdir(self.lock_file)
- except OSError:
- err = sys.exc_info()[1]
- if err.errno == errno.EEXIST:
- # Already locked.
- if os.path.exists(self.unique_name):
- # Already locked by me.
- return
- if timeout is not None and time.time() > end_time:
- if timeout > 0:
- raise LockTimeout
- else:
- # Someone else has the lock.
- raise AlreadyLocked
- time.sleep(wait)
- else:
- # Couldn"t create the lock for some other reason
- raise LockFailed("failed to create %s" % self.lock_file)
- else:
- open(self.unique_name, "wb").close()
- return
+ self.attempt_acquire(self, timeout, end_time, wait)
def release(self):
if not self.is_locked():
@@ -433,6 +436,26 @@ def i_am_the_only_lock(self, cursor):
# We"re the locker, so go home.
return
+ def attempt_acquire(self, timeout, cursor, end_time, wait):
+ if self.is_locked():
+ if self.i_am_the_only_lock(cursor):
+ return
+ else:
+ if self.create_lock(cursor):
+ return
+
+ # Maybe we should wait a bit longer.
+ if timeout is not None and time.time() > end_time:
+ if timeout > 0:
+ # No more waiting.
+ raise LockTimeout
+ else:
+ # Someone else has the lock and we are impatient..
+ raise AlreadyLocked
+
+ # Well, okay. We"ll give it a bit longer.
+ time.sleep(wait)
+
def acquire(self, timeout=None):
end_time = time.time()
if timeout is not None and timeout > 0:
@@ -448,24 +471,7 @@ def acquire(self, timeout=None):
cursor = self.connection.cursor()
while True:
- if self.is_locked():
- if self.i_am_the_only_lock(cursor):
- return
- else:
- if self.create_lock(cursor):
- return
-
- # Maybe we should wait a bit longer.
- if timeout is not None and time.time() > end_time:
- if timeout > 0:
- # No more waiting.
- raise LockTimeout
- else:
- # Someone else has the lock and we are impatient..
- raise AlreadyLocked
-
- # Well, okay. We"ll give it a bit longer.
- time.sleep(wait)
+ self.attempt_acquire(self, timeout, cursor, end_time, wait)
def release(self):
if not self.is_locked():

0 comments on commit b95e999

Please sign in to comment.