1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

make lockfile destination settable and update doc (#42795)

* make sure lock file is available for others when unlocking

* add tmpdir option and updated documentation
This commit is contained in:
Andreas Calminder 2018-08-08 15:52:11 +02:00 committed by Toshio Kuratomi
parent 1366a694f9
commit 3419c75411

View file

@ -12,7 +12,6 @@ import pwd
import grp import grp
import time import time
import shutil import shutil
import tempfile
import traceback import traceback
import fcntl import fcntl
import sys import sys
@ -57,30 +56,32 @@ class FileLock:
self.lockfd = None self.lockfd = None
@contextmanager @contextmanager
def lock_file(self, path, lock_timeout=None): def lock_file(self, path, tmpdir, lock_timeout=None):
''' '''
Context for lock acquisition Context for lock acquisition
''' '''
try: try:
self.set_lock(path, lock_timeout) self.set_lock(path, tmpdir, lock_timeout)
yield yield
finally: finally:
self.unlock() self.unlock()
def set_lock(self, path, lock_timeout=None): def set_lock(self, path, tmpdir, lock_timeout=None):
''' '''
Create a lock file based on path with flock to prevent other processes Create a lock file based on path with flock to prevent other processes
using given path using given path.
Please note that currently file locking only works when it's executed by
the same user, I.E single user scenarios
:kw path: Path (file) to lock :kw path: Path (file) to lock
:kw tmpdir: Path where to place the temporary .lock file
:kw lock_timeout: :kw lock_timeout:
Wait n seconds for lock acquisition, fail if timeout is reached. Wait n seconds for lock acquisition, fail if timeout is reached.
0 = Do not wait, fail if lock cannot be acquired immediately, 0 = Do not wait, fail if lock cannot be acquired immediately,
Default is None, wait indefinitely until lock is released. Default is None, wait indefinitely until lock is released.
:returns: True :returns: True
''' '''
tmp_dir = tempfile.gettempdir() lock_path = os.path.join(tmpdir, 'ansible-{0}.lock'.format(os.path.basename(path)))
lock_path = os.path.join(tmp_dir, 'ansible-{0}.lock'.format(os.path.basename(path)))
l_wait = 0.1 l_wait = 0.1
r_exception = IOError r_exception = IOError
if sys.version_info[0] == 3: if sys.version_info[0] == 3:
@ -115,7 +116,8 @@ class FileLock:
def unlock(self): def unlock(self):
''' '''
Unlock the file descriptor locked by set_lock Make sure lock file is available for everyone and Unlock the file descriptor
locked by set_lock
:returns: True :returns: True
''' '''