Source code for inspirehep.utils.lock

# -*- coding: utf-8 -*-
#
# This file is part of INSPIRE.
# Copyright (C) 2018 CERN.
#
# INSPIRE is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# INSPIRE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with INSPIRE. If not, see <http://www.gnu.org/licenses/>.
#
# In applying this license, CERN does not waive the privileges and immunities
# granted to it by virtue of its status as an Intergovernmental Organization
# or submit itself to any jurisdiction.

"""Locking."""

from __future__ import absolute_import, division, print_function

from contextlib import contextmanager

from flask import current_app as app
from redis import StrictRedis
from redis_lock import Lock


@contextmanager
[docs]def distributed_lock(lock_name, expire=10, auto_renewal=True, blocking=False): """Context manager to acquire a lock visible by all processes. This lock is implemented through Redis in order to be globally visible. Args: lock_name (str): name of the lock to be acquired. expire (int): duration in seconds after which the lock is released if not renewed in the meantime. auto_renewal (bool): if ``True``, the lock is automatically renewed as long as the context manager is still active. blocking (bool): if ``True``, wait for the lock to be released. If ``False``, return immediately, raising :class:`DistributedLockError`. It is recommended to set ``expire`` to a small value and ``auto_renewal=True``, which ensures the lock gets released quickly in case the process is killed without limiting the time that can be spent holding the lock. Raises: DistributedLockError: when ``blocking`` is set to ``False`` and the lock is already acquired. """ if not lock_name: raise ValueError('Lock name not specified.') redis_url = app.config.get('CACHE_REDIS_URL') redis = StrictRedis.from_url(redis_url) lock = Lock(redis, lock_name, expire=expire, auto_renewal=auto_renewal) if lock.acquire(blocking=blocking): try: yield finally: lock.release() else: raise DistributedLockError('Cannot acquire lock for %s', lock_name)
[docs]class DistributedLockError(Exception): pass