/
decorators.py
72 lines (59 loc) · 2.27 KB
/
decorators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import time
import logging
import tempfile
from lockfile import FileLock, AlreadyLocked, LockTimeout
from django.conf import settings
"""
A decorator for management commands (or any class method) to ensure that there is
only ever one process running the method at any one time.
Requires lockfile - (pip install lockfile)
Author: Ross Lawley
Via: http://rosslawley.co.uk/archive/old/2010/10/18/locking-django-management-commands/
"""
# Lock timeout value - how long to wait for the lock to become available.
# Default behavior is to never wait for the lock to be available (fail fast)
LOCK_WAIT_TIMEOUT = getattr(settings, "DEFAULT_LOCK_WAIT_TIMEOUT", -1)
def handle_lock(handle):
"""
Decorate the handle method with a file lock to ensure there is only ever
one process running at any one time.
"""
def wrapper(self, *args, **options):
start_time = time.time()
verbosity = options.get('verbosity', 0)
if verbosity == 0:
level = logging.ERROR
elif verbosity == 1:
level = logging.WARN
elif verbosity == 2:
level = logging.INFO
else:
level = logging.DEBUG
logging.basicConfig(level=level, format="%(message)s")
logging.debug("-" * 72)
lock_name = self.__module__.split('.').pop()
lock = FileLock(tempfile.gettempdir() + '/pdk_lock_' + lock_name)
logging.debug("%s - acquiring lock..." % lock_name)
try:
lock.acquire(LOCK_WAIT_TIMEOUT)
except AlreadyLocked:
logging.debug("lock already in place. quitting.")
return
except LockTimeout:
logging.debug("waiting for the lock timed out. quitting.")
return
logging.debug("acquired.")
try:
handle(self, *args, **options)
except:
import traceback
logging.error("Command Failed")
logging.error('==' * 72)
logging.error(traceback.format_exc())
logging.error('==' * 72)
logging.debug("releasing lock...")
lock.release()
logging.debug("released.")
logging.info("done in %.2f seconds" % (time.time() - start_time))
return
return wrapper