{"id":21881037,"url":"https://github.com/wtsi-hgi/consul-lock","last_synced_at":"2025-07-19T06:04:05.406Z","repository":{"id":47092484,"uuid":"110961479","full_name":"wtsi-hgi/consul-lock","owner":"wtsi-hgi","description":"CLI and Python interface for easily managing Consul locks","archived":false,"fork":false,"pushed_at":"2021-09-13T21:58:20.000Z","size":171,"stargazers_count":4,"open_issues_count":4,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-15T09:55:05.817Z","etag":null,"topics":["cli","consul","distributed-locks","library","python","tool"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wtsi-hgi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-16T11:08:01.000Z","updated_at":"2021-10-28T22:17:37.000Z","dependencies_parsed_at":"2022-08-29T14:31:14.189Z","dependency_job_id":null,"html_url":"https://github.com/wtsi-hgi/consul-lock","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/wtsi-hgi/consul-lock","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wtsi-hgi%2Fconsul-lock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wtsi-hgi%2Fconsul-lock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wtsi-hgi%2Fconsul-lock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wtsi-hgi%2Fconsul-lock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wtsi-hgi","download_url":"https://codeload.github.com/wtsi-hgi/consul-lock/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wtsi-hgi%2Fconsul-lock/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265895924,"owners_count":23845408,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","consul","distributed-locks","library","python","tool"],"created_at":"2024-11-28T09:17:31.750Z","updated_at":"2025-07-19T06:04:05.384Z","avatar_url":"https://github.com/wtsi-hgi.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/wtsi-hgi/consul-lock.svg?branch=master)](https://travis-ci.org/wtsi-hgi/consul-lock)\n[![codecov](https://codecov.io/gh/wtsi-hgi/consul-lock/branch/master/graph/badge.svg)](https://codecov.io/gh/wtsi-hgi/consul-lock)\n[![PyPI version](https://badge.fury.io/py/consullock.svg)](https://badge.fury.io/py/consullock)\n\n# Consul Lock\n_CLI and Python interface for easily managing Consul locks_\n\n\n## About\n- CLI \u0026 Python interface:\n    - Acquire and release a lock.\n    - Execute shell whilst holding a lock (releasing on exit).\n    - Set TTL of the session that acquires a lock.\n    - Blocking and non-blocking locking.\n    - Lock acquire timeouts.\n    - Add extra metadata to lock information (useful for monitoring who is holding locks).\n    - Specify lock event listeners to be fired before lock acquire is attempted and if unable to acquire locked lock.\n- CLI:\n    - Release multiple locks with keys that match regular expression.\n    - Meaningful exit codes (see `configuration.py` for details).\n    - JSON output on stdout.\n    - Add extra verbosity on stderr with `-vv`.\n- Python interface:\n    - Release multiple locks.\n    - Find and release multiple locks with keys that match regular expression.\n    - Type hinting.\n\n\n## Installation\nPrerequisites:\n- python \u003e= 3.6\n\nThe tool can be installed from PyPi:\n```bash\npip install consullock\n```\n\nBleeding edge versions can be installed directly from GitHub:\n```bash\npip install git+https://github.com/wtsi-hgi/consul-lock.git@master#egg=consullock\n```\n\n\n## Usage\n### CLI\n#### Setup\nSet environment variables:\n```bash\nexport CONSUL_HTTP_ADDR=consul.example.com:8500\nexport CONSUL_HTTP_TOKEN=token\n```\n\n#### General\n```\nusage: consul-lock [-h] [-v] {unlock,lock,execute} ...\n\nTool to use locks in Consul (v4.2.0)\n\npositional arguments:\n  {unlock,lock,execute}\n                        action\n    unlock              release a lock\n    lock                acquire a lock\n    execute             call executable whilst holding lock\n\noptional arguments:\n  -h, --help            show this help message and exit\n  -v                    increase the level of log verbosity (add multiple\n                        increase further)\n```\n\n#### Locking\n```\nusage: consul-lock lock [-h] [--session-ttl SESSION_TTL] [--non-blocking]\n                        [--timeout TIMEOUT] [--metadata METADATA]\n                        [--on-before-lock ON_BEFORE_LOCK [ON_BEFORE_LOCK ...]]\n                        [--on-already-locked ON_ALREADY_LOCKED [ON_ALREADY_LOCKED ...]]\n                        [-i I]\n                        key\n\npositional arguments:\n  key                   the lock identifier\n\noptional arguments:\n  -h, --help            show this help message and exit\n  --session-ttl SESSION_TTL\n                        time to live (ttl) in seconds of the session that will\n                        be created to hold the lock. Must be between 10s and\n                        86400s (inclusive). If set to 0, the session will not\n                        expire\n  --non-blocking        do not block if cannot lock straight away\n  --timeout TIMEOUT     give up trying to acquire the key after this many\n                        seconds (where 0 is never)\n  --metadata METADATA   additional metadata to add to the lock information\n                        (will be converted to JSON)\n  --on-before-lock ON_BEFORE_LOCK [ON_BEFORE_LOCK ...]\n                        path to executable that is to be called before an\n                        attempt is made to acquire a lock, where the lock key\n                        is passed as the first argument. Any failures of this\n                        executable are ignored\n  --on-already-locked ON_ALREADY_LOCKED [ON_ALREADY_LOCKED ...]\n                        path to executable that is to be called after an\n                        attempt has been made to acquire a lock but failed due\n                        to the lock already been taken, where the lock key is\n                        passed as the first argument. Any failures of this\n                        executable are ignored\n  -i I                  number of seconds between polls to acquire a locked\n                        lock\n```\n\n#### Unlocking              \n```\nusage: consul-lock unlock [-h] [-r] key\n\npositional arguments:\n  key         the lock identifier\n\noptional arguments:\n  -h, --help  show this help message and exit\n  -r          whether the key should be treated as a regular expression and to\n              release all matching locks\n```\n\n#### Executing with lock\n```\nusage: consul-lock execute [-h] [--session-ttl SESSION_TTL] [--non-blocking]\n                           [--timeout TIMEOUT] [--metadata METADATA]\n                           [--on-before-lock ON_BEFORE_LOCK [ON_BEFORE_LOCK ...]]\n                           [--on-already-locked ON_ALREADY_LOCKED [ON_ALREADY_LOCKED ...]]\n                           [-i I]\n                           key executable\n\npositional arguments:\n  key                   the lock identifier\n  executable            to execute in shell\n\noptional arguments:\n  -h, --help            show this help message and exit\n  --session-ttl SESSION_TTL\n                        time to live (ttl) in seconds of the session that will\n                        be created to hold the lock. Must be between 10s and\n                        86400s (inclusive). If set to 0, the session will not\n                        expire\n  --non-blocking        do not block if cannot lock straight away\n  --timeout TIMEOUT     give up trying to acquire the key after this many\n                        seconds (where 0 is never)\n  --metadata METADATA   additional metadata to add to the lock information\n                        (will be converted to JSON)\n  --on-before-lock ON_BEFORE_LOCK [ON_BEFORE_LOCK ...]\n                        path to executable that is to be called before an\n                        attempt is made to acquire a lock, where the lock key\n                        is passed as the first argument. Any failures of this\n                        executable are ignored\n  --on-already-locked ON_ALREADY_LOCKED [ON_ALREADY_LOCKED ...]\n                        path to executable that is to be called after an\n                        attempt has been made to acquire a lock but failed due\n                        to the lock already been taken, where the lock key is\n                        passed as the first argument. Any failures of this\n                        executable are ignored\n  -i I                  number of seconds between polls to acquire a locked\n                        lock\n```\n\n#### Examples\n##### Acquire lock\nBlock until acquires lock with key \"my/lock\", which will expire after 10 minutes:\n```\n$ consul-lock lock --session-ttl=600 my/lock\n{\"created\": \"2017-11-30T14:55:49.322108\", \"key\": \"my/lock\", \"secondsToLock\": 2.6241003070026636e-05, \"session\": \"9b92744f-f1a9-db12-7873-ad44af5ae224\"}\n$ echo $?\n0 \n```\n\nTry to acquire already locked \"my/lock\" but do not block:\n```\n$ consul-lock lock --non-blocking my/lock\nnull\nUnable to acquire lock: my/lock\n$ echo $?\n100\n```\n(where `null` is the JSON output, written to stdout)\n\n\n##### Add metadata to lock\n```\n$ consul-lock lock --metadata={\"testing\": 123} my/lock\n{\"created\": \"2017-12-05T12:26:13.717995\", \"key\": \"my/lock\", \"metadata\": {\"testing\": 123}, \"secondsToLock\": 4.327880731027108, \"session\": \"6ad662de-6e0c-8e0f-d92c-5fface60c49b\"}\n```\n\n\n##### Acquire lock with event listeners\n```\n$ consul-lock lock --on-before-lock=./my-before-script.sh --on-already-locked=./my-locked-script.sh my/lock\n{\"created\": \"2017-12-07T13:02:06.773088\", \"key\": \"my/lock\", \"secondsToLock\": 1.349498052150011e-05, \"session\": \"459266be-2a85-464e-aa08-eda97613a29c\"}\n```\n_Note: if specifying event listeners in the form `--x y`, you will need to explicitly declare the end of the command \noptions with a double dash, e.g. `consul-lock lock --on-before-lock ./my-before-script.sh -- my/lock`._\n\n\n##### Unlock lock\n```bash\n$ consul-lock unlock my/lock\n[\"my/lock\"]\n$ echo $?\n0\n```\n\n\n##### Execute shell with lock\n```bash\n$ consul-lock execute my/lock ./my-executable\nI am printed in \"my-executable\"\n```\n_Note: the return code is the result of the executable_\n\n\n### Python\n```python\nfrom typing import Dict\nfrom consullock.configuration import get_consul_configuration_from_environment\nfrom consullock.managers import ConsulLockManager\nfrom consullock.models import ConsulLockInformation\n\n# Instantiate lock manager\nlock_manager = ConsulLockManager(\n    consul_configuration=get_consul_configuration_from_environment(),\n    session_ttl_in_seconds=3600)\n# Note: if the Consul configuration is not specified explicitly, an attempt to gather it from environmental variables \n# will be made (see: https://www.consul.io/docs/commands/index.html#environment-variables).\n\n# Use lock in context\nwith lock_manager.acquire(\"my/lock\"):\n    do_things_holding_lock()\n\n# Get lock\nlock = lock_manager.acquire(\"my/lock\", timeout=60, blocking=True, metadata=\"testing\")\n\n# Find lock associated to a given key\nlock = lock_manager.find(\"my/lock\")\n\n# Find locks that match a regex\nlocks = lock_manager.find_regex(\"my/.*\")  # type: Dict[str, Optional[ConsulLockInformation]]\n\n# Release single lock\nlock_manager.release(\"my/lock\")\n\n# Release many locks\nlock_manager.release_all(\"my/lock-1\", \"my/lock-2\", \"my/lock-3\")\n\n# Release locks with key matching regex\nlock_manager.release_regex(\"my/.*\")\n\n# Execute shell whilst holding lock (note: use context manager if executing Python!)\nlock_manager.execute(\"my/lock\", \"echo Hello World\")\n```\n\n\n## Development\n### Tests\nPrerequisites:\n- Docker (used to start Consul instances)\n\nInstall the test requirements:\n```\npip install -r test_requirements.txt\n```\n\nRun tests with:\n```\nPYTHONPATH=. python -m unittest discover -v -s consullock/tests\n```\n\n\n## Alternatives\n- [alaa/consul-lock](https://github.com/alaa/consul-lock) - Go tool with CLI where the user has to deal with sessions.\n- [fidian/consul-locker](https://github.com/fidian/consul-locker) - bash script that locks, executes and then releases.\n- [KurToMe/python-consul-lock](https://github.com/KurToMe/python-consul-lock) - unmaintained Python lock interface with \nwith questionable configuration.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwtsi-hgi%2Fconsul-lock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwtsi-hgi%2Fconsul-lock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwtsi-hgi%2Fconsul-lock/lists"}