Skip to content

Commit

Permalink
Merge pull request #3 from orlovegor/master
Browse files Browse the repository at this point in the history
get script name from module string; raise command exception
  • Loading branch information
gotlium committed Aug 8, 2014
2 parents 6a5a1c9 + d4ef54c commit 55ae9bf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
16 changes: 8 additions & 8 deletions mmc/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@


class AbstractLock(object):
def __init__(self):
def __init__(self, script):
self._script = script
self._tempdir = tempfile.gettempdir()
self._script = sys.argv[1]

def get_lock_file(self):
return os.path.join(self._tempdir, self._script + '.lock')
Expand Down Expand Up @@ -50,11 +50,11 @@ def is_run(self):


class RedisLock(AbstractLock):
def __init__(self):
def __init__(self, script):
from redis import StrictRedis

self._cli = StrictRedis(**defaults.REDIS_CONFIG)
super(RedisLock, self).__init__()
super(RedisLock, self).__init__(script)
self.random_wait()

def unlock(self):
Expand All @@ -69,16 +69,16 @@ def is_run(self):


class MemcacheLock(RedisLock):
def __init__(self):
def __init__(self, script):
try:
from memcache import Client
except ImportError:
from pylibmc import Client

super(MemcacheLock, self).__init__()
super(MemcacheLock, self).__init__(script)
self._cli = Client(**defaults.MEMCACHED_CONFIG)
self.random_wait()


def get_lock_instance():
return globals().get(defaults.LOCK_TYPE)()
def get_lock_instance(script):
return globals().get(defaults.LOCK_TYPE)(script)
7 changes: 4 additions & 3 deletions mmc/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def __init__(self):
self._error_message = None
self._traceback = None
self._show_traceback = False
self._script = sys.argv[1]
self._lock = get_lock_instance()
self._script = self.__module__.split('.')[-1]
self._lock = get_lock_instance(self._script)

def __mmc_one_copy(self):
try:
Expand All @@ -54,10 +54,11 @@ def execute(self, *args, **options):

try:
self._no_monkey.execute(self, *args, **options)
except Exception, ex:
except Exception as ex:
self._success = False
self._error_message = ex.__unicode__()
self._traceback = traceback.format_exc()
raise

def __mmc_store_log(self):
try:
Expand Down

0 comments on commit 55ae9bf

Please sign in to comment.