def testConfigure(): ''' Test the library configure function. ''' sherlock.configure(namespace='namespace') assert sherlock._configuration.namespace == 'namespace'
def test_valid_key_names_are_generated_when_namespace_is_set(self): name = 'lock' lock = sherlock.lock.MCLock(name, namespace='local_namespace') self.assertEquals(lock._key_name, 'local_namespace_%s' % name) sherlock.configure(namespace='global_namespace') lock = sherlock.lock.MCLock(name) self.assertEquals(lock._key_name, 'global_namespace_%s' % name)
def test_valid_key_names_are_generated_when_namespace_is_set(self): name = 'lock' lock = sherlock.lock.MCLock(name, namespace='local_namespace') self.assertEqual(lock._key_name, 'local_namespace_%s' % name) sherlock.configure(namespace='global_namespace') lock = sherlock.lock.MCLock(name) self.assertEqual(lock._key_name, 'global_namespace_%s' % name)
def test_init_does_not_use_global_default_for_client_obj(self): client_obj = etcd.Client() sherlock.configure(client=client_obj) lock = sherlock.lock.BaseLock('lockname') self.assertNotEqual(lock.client, client_obj)
def test_init_uses_global_defaults(self): sherlock.configure(namespace='new_namespace') lock = sherlock.lock.BaseLock('lockname') self.assertEqual(lock.namespace, 'new_namespace')
def test_lock_sets_client_object_on_lock_proxy_when_globally_configured(self): client = etcd.Client(host='8.8.8.8') sherlock.configure(client=client) lock = sherlock.lock.Lock('lock') self.assertEquals(lock._lock_proxy.client, client)
from flask import Flask, request from psycopg2 import connect from rq import Connection, Queue, Worker from sherlock import Lock from tx.functional.either import Left, Right #most worker functions are found here in reload #lock, functions prepended by _ are not locked, they are called within a wrapper #of the same name (without the _) which do use the lock. this is important sherlock.configure( backend=sherlock.backends.REDIS, client=redis.StrictRedis( host=os.environ["REDIS_LOCK_HOST"], port=int(os.environ["REDIS_LOCK_PORT"]), db=int(os.environ["REDIS_LOCK_DB"]), ), expire=int(os.environ["REDIS_LOCK_EXPIRE"]), timeout=int(os.environ["REDIS_LOCK_TIMEOUT"]), ) G_LOCK = "g_lock" def redisQueue(): return redis.StrictRedis( host=os.environ["REDIS_QUEUE_HOST"], port=int(os.environ["REDIS_QUEUE_PORT"]), db=int(os.environ["REDIS_QUEUE_DB"]), )
import celery from celery.utils.log import get_task_logger from django.conf import settings from django.core.mail import send_mail from django.db import (connections, transaction) from geonode.celery_app import app try: import pylibmc import sherlock from sherlock import MCLock as Lock sherlock.configure(expire=settings.MEMCACHED_LOCK_EXPIRE, timeout=settings.MEMCACHED_LOCK_TIMEOUT) memcache_client = pylibmc.Client([settings.MEMCACHED_LOCATION], binary=True) lock_type = "MEMCACHED" except Exception: from django.core.cache import cache from contextlib import contextmanager lock_type = "MEMCACHED-LOCAL-CONTEXT" memcache_client = None """ ref. http://docs.celeryproject.org/en/latest/tutorials/task-cookbook.html#ensuring-a-task-is-only-executed-one-at-a-time """ class Lock: def __init__(self, lock_id, *args, **kwargs): self.lock_id = lock_id
def test_lock_sets_client_object_on_lock_proxy_when_globally_configured( self): client = etcd.Client(host='8.8.8.8') sherlock.configure(client=client) lock = sherlock.lock.Lock('lock') self.assertEqual(lock._lock_proxy.client, client)
REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAuthenticated', ), 'PAGINATE_BY': 10, } CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 'LOCATION': '127.0.0.1:11211', } } import sherlock # Global configuration of defaults sherlock.configure(expire=120, timeout=20) MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ) ROOT_URLCONF = 'btcxblockchainapi.urls' WSGI_APPLICATION = 'btcxblockchainapi.wsgi.application' # Database
# SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 'ga!23p96%^pqs)$6u=ft1k-z+3*2ypw948q)9z+e$5x^k*kpe$' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = ['localhost', 'calendar-app-zeeshan.herokuapp.com'] # Print BASE_DIR for reference in build. print(BASE_DIR) # Configure Sherlock's locks to use Redis as the backend, # never expire locks and retry acquiring an acquired lock after an # interval of 0.1 second. sherlock.configure(backend=sherlock.backends.MEMCACHED, expire=None, retry_interval=0.1) # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'reservation', 'rest_framework' ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware',
"Trying to use locks but no lock backend configured.") if LOCKS == "shylock": if not shylock: raise ValueError("Cannot use shylock locks, shylock is not installed") if not pymongo: raise ValueError( "Cannot use shylock locks, pymongo driver is not available") if not MONGODB_CONNECTION_STRING: raise ValueError( "Cannot use shylock locks, MongoDB connection string is not configured" ) client = pymongo.MongoClient(MONGODB_CONNECTION_STRING) shylock.configure( shylock.ShylockPymongoBackend.create(client, MONGODB_DATABASE)) Lock = shylock.Lock elif LOCKS == "sherlock": if not sherlock: raise ValueError( "Cannot use sherlock locks, sherlock is not installed") if not pylibmc: raise ValueError("Cannot use sherlock locks, pylibmc is not installed") sherlock.configure( backend=sherlock.backends.MEMCACHED, client=pylibmc.Client(MEMCACHED), expire=5 * 60, ) Lock = sherlock.MCLock