Skip to content
Snippets Groups Projects
Commit eb0f0ce0 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

Move watcher's LockFile function to utils

Reviewed-by: iustinp
parent 307149a8
No related branches found
No related tags found
No related merge requests found
......@@ -90,18 +90,6 @@ def DoCmd(cmd):
return res
def LockFile(fd):
"""Locks a file using POSIX locks.
"""
try:
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError, err:
if err.errno == errno.EAGAIN:
raise errors.LockError("File already locked")
raise
class WatcherState(object):
"""Interface to a state file recording restart attempts.
......@@ -118,7 +106,7 @@ class WatcherState(object):
fd = os.open(constants.WATCHER_STATEFILE, os.O_RDWR | os.O_CREAT)
self.statefile = os.fdopen(fd, 'w+')
LockFile(self.statefile.fileno())
utils.LockFile(self.statefile.fileno())
try:
self._data = serializer.Load(self.statefile.read())
......@@ -151,7 +139,7 @@ class WatcherState(object):
# starting ganeti-watcher again at the same time will create a conflict.
fd = utils.WriteFile(constants.WATCHER_STATEFILE,
data=serialized_form,
prewrite=LockFile, close=False)
prewrite=utils.LockFile, close=False)
self.statefile = os.fdopen(fd, 'w+')
def Close(self):
......
......@@ -1082,3 +1082,15 @@ def LockedMethod(fn):
lock.release()
return result
return wrapper
def LockFile(fd):
"""Locks a file using POSIX locks.
"""
try:
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError, err:
if err.errno == errno.EAGAIN:
raise errors.LockError("File already locked")
raise
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment