在领事馆使用锁的工具

consullock的Python项目详细描述


Build StatuscodecovPyPI version

领事锁

cli和python接口,用于轻松管理consul锁

关于

  • cli&python接口:
    • 获取并释放锁。
    • 执行锁壳(退出时释放)。
    • 设置获取锁的会话的ttl。
    • 阻塞和非阻塞锁定。
    • 锁定获取超时。
    • 添加额外的元数据以锁定信息(对于监视 是拿着锁的)。
    • 指定在获取锁之前要激发的锁事件侦听器 尝试,如果无法获取锁定的锁。
  • 客户端:
    • 使用与正则表达式匹配的键释放多个锁。
    • 有意义的退出代码(详见^ {TT1}$)。
    • stdout上的json输出。
    • 使用-vv在stderr上添加额外的详细信息。
  • python接口:
    • 释放多个锁。
    • 使用与常规锁匹配的钥匙查找并释放多个锁 表达式。
    • 键入提示。

安装

先决条件:-python>;=3.6

该工具可以从pypi安装:

pip install consullock

出血边缘版本可以直接从github安装:

pip install git+https://github.com/wtsi-hgi/consul-lock.git@master#egg=consullock

使用量

cli

设置

设置环境变量:

exportCONSUL_HTTP_ADDR=consul.example.com:8500
exportCONSUL_HTTP_TOKEN=token
概述
usage: consullock [-h] [-v] {unlock,lock,execute} ...

Tool to use locks in Consul (v4.2.0)

positional arguments:
  {unlock,lock,execute}
                        action
    unlock              release a lock
    lock                acquire a lock
    execute             call executable whilst holding lock

optional arguments:
  -h, --help            show this help message and exit
  -v                    increase the level of log verbosity (add multiple
                        increase further)
锁定
usage: consullock lock [-h] [--session-ttl SESSION_TTL] [--non-blocking]
                       [--timeout TIMEOUT] [--metadata METADATA]
                       [--on-before-lock ON_BEFORE_LOCK [ON_BEFORE_LOCK ...]]
                       [--on-already-locked ON_ALREADY_LOCKED [ON_ALREADY_LOCKED ...]]
                       [-i I]
                       key

positional arguments:
  key                   the lock identifier

optional arguments:
  -h, --help            show this help message and exit
  --session-ttl SESSION_TTL
                        time to live (ttl) in seconds of the session that will
                        be created to hold the lock. Must be between 10s and
                        86400s (inclusive). If set to 0, the session will not
                        expire
  --non-blocking        do not block if cannot lock straight away
  --timeout TIMEOUT     give up trying to acquire the key after this many
                        seconds (where 0 is never)
  --metadata METADATA   additional metadata to add to the lock information
                        (will be converted to JSON)
  --on-before-lock ON_BEFORE_LOCK [ON_BEFORE_LOCK ...]
                        path to executable that is to be called before an
                        attempt is made to acquire a lock, where the lock key
                        is passed as the first argument. Any failures of this
                        executable are ignored
  --on-already-locked ON_ALREADY_LOCKED [ON_ALREADY_LOCKED ...]
                        path to executable that is to be called after an
                        attempt has been made to acquire a lock but failed due
                        to the lock already been taken, where the lock key is
                        passed as the first argument. Any failures of this
                        executable are ignored
  -i I                  number of seconds between polls to acquire a locked
                        lock
解锁
usage: consullock unlock [-h] [-r] key

positional arguments:
  key         the lock identifier

optional arguments:
  -h, --help  show this help message and exit
  -r          whether the key should be treated as a regular expression and to
              release all matching locks
用锁执行
usage: consullock execute [-h] [--session-ttl SESSION_TTL] [--non-blocking]
                          [--timeout TIMEOUT] [--metadata METADATA]
                          [--on-before-lock ON_BEFORE_LOCK [ON_BEFORE_LOCK ...]]
                          [--on-already-locked ON_ALREADY_LOCKED [ON_ALREADY_LOCKED ...]]
                          [-i I]
                          key executable

positional arguments:
  key                   the lock identifier
  executable            to execute in shell

optional arguments:
  -h, --help            show this help message and exit
  --session-ttl SESSION_TTL
                        time to live (ttl) in seconds of the session that will
                        be created to hold the lock. Must be between 10s and
                        86400s (inclusive). If set to 0, the session will not
                        expire
  --non-blocking        do not block if cannot lock straight away
  --timeout TIMEOUT     give up trying to acquire the key after this many
                        seconds (where 0 is never)
  --metadata METADATA   additional metadata to add to the lock information
                        (will be converted to JSON)
  --on-before-lock ON_BEFORE_LOCK [ON_BEFORE_LOCK ...]
                        path to executable that is to be called before an
                        attempt is made to acquire a lock, where the lock key
                        is passed as the first argument. Any failures of this
                        executable are ignored
  --on-already-locked ON_ALREADY_LOCKED [ON_ALREADY_LOCKED ...]
                        path to executable that is to be called after an
                        attempt has been made to acquire a lock but failed due
                        to the lock already been taken, where the lock key is
                        passed as the first argument. Any failures of this
                        executable are ignored
  -i I                  number of seconds between polls to acquire a locked
                        lock
示例
获取锁

使用“my/lock”键获取锁,锁将在10之后过期 分钟:

$ consul-lock lock --session-ttl=600 my/lock
{"created": "2017-11-30T14:55:49.322108", "key": "my/lock", "secondsToLock": 2.6241003070026636e-05, "session": "9b92744f-f1a9-db12-7873-ad44af5ae224"}
$ echo $?
0

尝试获取已锁定的“我的/锁定”,但不要阻止:

$ consul-lock lock --non-blocking my/lock
null
Unable to acquire lock: my/lock
$ echo $?
100

(其中null是json输出,写入stdout)

将元数据添加到锁中
$ consul-lock lock --metadata={"testing": 123} my/lock
{"created": "2017-12-05T12:26:13.717995", "key": "my/lock", "metadata": {"testing": 123}, "secondsToLock": 4.327880731027108, "session": "6ad662de-6e0c-8e0f-d92c-5fface60c49b"}
使用事件侦听器获取锁
$ consul-lock lock --on-before-lock=./my-before-script.sh --on-already-locked=./my-locked-script.sh my/lock
{"created": "2017-12-07T13:02:06.773088", "key": "my/lock", "secondsToLock": 1.349498052150011e-05, "session": "459266be-2a85-464e-aa08-eda97613a29c"}

注意:如果以`–x y``的形式指定事件侦听器,则 需要用double显式声明命令选项的结尾 破折号,例如 `` consul lock lock–在锁定之前打开./my-before-script.sh–my/lock``。

开锁
$ consul-lock unlock my/lock
["my/lock"]
$ echo$?0
使用锁执行shell
$ consul-lock execute my/lock ./my-executable
I am printed in "my-executable"

注意:返回代码是可执行文件的结果

Python

fromtypingimportDictfromconsullock.configurationimportget_consul_configuration_from_environmentfromconsullock.managersimportConsulLockManagerfromconsullock.modelsimportConsulLockInformation# Instantiate lock managerlock_manager=ConsulLockManager(consul_configuration=get_consul_configuration_from_environment(),session_ttl_in_seconds=3600)# Note: if the Consul configuration is not specified explicitly, an attempt to gather it from environmental variables# will be made (see: https://www.consul.io/docs/commands/index.html#environment-variables).# Use lock in contextwithlock_manager.acquire("my/lock"):do_things_holding_lock()# Get locklock=lock_manager.acquire("my/lock",timeout=60,blocking=True,metadata="testing")# Find lock associated to a given keylock=lock_manager.find("my/lock")# Find locks that match a regexlocks=lock_manager.find_regex("my/.*")# type: Dict[str, Optional[ConsulLockInformation]]# Release single locklock_manager.release("my/lock")# Release many lockslock_manager.release_all("my/lock-1","my/lock-2","my/lock-3")# Release locks with key matching regexlock_manager.release_regex("my/.*")# Execute shell whilst holding lock (note: use context manager if executing Python!)lock_manager.execute("my/lock","echo Hello World")

发展

测试

先决条件:-Docker(用于启动领事实例)

安装测试要求:

pip install -r test_requirements.txt

使用以下命令运行测试:

PYTHONPATH=. python -m unittest discover -v -s consullock/tests

替代方案

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何在数据库中存储工作日数据以及开始时间和结束时间?   mysql Java JPA内部联接查询不起作用   java MQ消息在不同的客户端应用程序中显示不同   javascript如何在xPage上提高typeAhead性能?   java在尝试保存EditText时无法暂停   mongodb“未能使用带参数的构造函数NO_构造函数实例化java.util.List”,   java如何修改使IllegalArgumentException:列“\u id”不存在的游标   转换Android。网Uri到Java。伊奥。输入流   java NetBeans RCP居中弹出窗口无法按预期工作   java将sql开发人员与netbeans连接起来   带有xmldsig签名的java JAXB编组   java ORACLE JDBC批处理执行不会返回受影响行的实际计数   java无法理解如何在由swagger自动生成的jaxrs服务器中访问请求头   java如何处理漏洞CVE20181258,同时将Spring安全版本5与外部客户端(最新版本)一起使用?   JavaSpring数据JPA存储库多租户单模式技术