Skip to content

Commit

Permalink
Move everything read/write lock to lock.py
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriy-serdyuk authored and maximecb committed Aug 28, 2017
1 parent fcb57f1 commit ba23c29
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 60 deletions.
62 changes: 2 additions & 60 deletions fuel/utils/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
guaranteed by default copy.
"""
import atexit
import logging
import os
import shutil
import stat
import time
import shutil

from fuel import config
from fuel.utils import write_lock
from fuel.utils.lock import get_writelock, release_writelock, get_readlock

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -276,63 +275,6 @@ def check_enough_space(dataset_local_dir, remote_fname, local_fname,
(storage_total * max_disk_usage))


def get_readlock(pid, path):
"""Obtain a readlock on a file
Parameters
----------
path : str
Name of the file on which to obtain a readlock
"""

timestamp = int(time.time() * 1e6)
lockdir_name = "%s.readlock.%i.%i" % (path, pid, timestamp)
os.mkdir(lockdir_name)

# Register function to release the readlock at the end of the script
atexit.register(release_readlock, lockdirName=lockdir_name)


def release_readlock(lockdir_name):
"""Release a previously obtained readlock
Parameters
----------
lockdir_name : str
Name of the previously obtained readlock
"""

# Make sure the lock still exists before deleting it
if os.path.exists(lockdir_name) and os.path.isdir(lockdir_name):
os.rmdir(lockdir_name)


def get_writelock(filename):
"""Obtain a writelock on a file.
Only one write lock may be held at any given time.
Parameters
----------
filename : str
Name of the file on which to obtain a writelock
"""

# write lock expect locks to be on folder. Since we want a lock on a
# file, we will have to ask write lock for a folder with a different
# name from the file we want a lock on or else write lock will
# try to create a folder with the same name as the file
write_lock.get_lock(filename + ".writelock")


def release_writelock():
"""Release the previously obtained writelock."""
write_lock.release_lock()


def disk_usage(path):
"""Return free usage about the given path, in bytes.
Expand Down
57 changes: 57 additions & 0 deletions fuel/utils/write_lock.py → fuel/utils/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,60 @@ def release_lock():
if get_lock.lock_is_enabled and get_lock.n_lock == 0:
get_lock.start_time = None
get_lock.unlocker.unlock()


def get_writelock(filename):
"""Obtain a writelock on a file.
Only one write lock may be held at any given time.
Parameters
----------
filename : str
Name of the file on which to obtain a writelock
"""

# write lock expect locks to be on folder. Since we want a lock on a
# file, we will have to ask write lock for a folder with a different
# name from the file we want a lock on or else write lock will
# try to create a folder with the same name as the file
lock.get_lock(filename + ".writelock")


def release_writelock():
"""Release the previously obtained writelock."""
lock.release_lock()


def release_readlock(lockdir_name):
"""Release a previously obtained readlock
Parameters
----------
lockdir_name : str
Name of the previously obtained readlock
"""

# Make sure the lock still exists before deleting it
if os.path.exists(lockdir_name) and os.path.isdir(lockdir_name):
os.rmdir(lockdir_name)


def get_readlock(pid, path):
"""Obtain a readlock on a file
Parameters
----------
path : str
Name of the file on which to obtain a readlock
"""

timestamp = int(time.time() * 1e6)
lockdir_name = "%s.readlock.%i.%i" % (path, pid, timestamp)
os.mkdir(lockdir_name)

# Register function to release the readlock at the end of the script
atexit.register(release_readlock, lockdirName=lockdir_name)

0 comments on commit ba23c29

Please sign in to comment.