一个用于阻塞直到服务监听的命令行工具
项目描述
我需要一种方法来阻止我的应用程序启动,直到数据库服务运行并接受连接。这在使用Docker和docker-compose时尤其成问题。您可以将此实用程序作为 run-dev.sh 或 run-tests.sh 脚本的一部分运行,并将在15秒(可配置)后阻塞,直到它可以连接到连接URL中指定的主机名和端口号(默认情况下从DATABASE_URL环境变量中获取)。
安装
$ pip install urlwait
用法
$ urlwait --help Usage: $ urlwait [SERVICE URL] [TIMEOUT] SERVICE URL is a connection url, e.g. a typical value for $DATABASE_URL. TIMEOUT is a number of seconds to try to connect to the host and port specified in the SERVICE URL. These values may also be specified in environment variables, but command line args take precedence: URLWAIT_VARNAME: the env var name containing the URL to check. Default DATABASE_URL. URLWAIT_TIMEOUT: the number of seconds to wait. Default 15. Returns a 0 status if the connection completed successfully before the timeout, or 1 if not. It is designed for use with Docker or other dev or testing environments where the connected services and the app are started simultaneously, but one should wait for the other. Examples: The following are equivalent: $ urlwait redis://localhost:6379/0 20 $ urlwait $CACHE_URL 20 $ URLWAIT_VARNAME=CACHE_URL URLWAIT_TIMEOUT=20 urlwait
该模块也可用于您的Python代码中
import os from urlwait import wait_for_url if wait_for_url(os.getenv('REDIS_URL')): # do things with the service else: # service likely did not start
如果您没有服务URL但有主机和端口号,您可以使用 wait_for_service(host, port, timeout) 函数代替。
Python支持
自版本1.0起,urlwait仅支持Python 3。如果您需要Python 2.7支持,您可以使用版本低于1.0的版本。
变更日志
1.0 - 2020-02-28 * 关闭套接字以防止Python报错。感谢@callahad! * 更新测试以仅针对Python 3.6+进行测试 * 停止支持Python 2.x
0.4 - 2017-03-02 - 如果协议是sqlite,则始终返回true
项目详情
下载文件
下载您平台对应的文件。如果您不确定该选择哪个,请了解更多关于安装包的信息。
源代码发行版
urlwait-1.0.tar.gz (5.1 kB 查看哈希值)
构建发行版
urlwait-1.0-py2.py3-none-any.whl (5.2 kB 查看哈希值)
关闭
urlwait-1.0.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | a9bf2da792fa6983fa93f6360108e16615066ab0f9cfb7f53e5faee5f5dffaac |
|
MD5 | b79134ca0d55d3e832afbb1061e25457 |
|
BLAKE2b-256 | 8d9b12a282343d5a81469108039cb59a0fb6b1d520c9772a5ce89bc25d56f1d9 |
关闭
urlwait-1.0-py2.py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | eae2c20001efc915166cac79c04bac0088ad5787ec64b36f27afd2f359953b2b |
|
MD5 | 3589a8002dc2d75d8aac5049b2eed10a |
|
BLAKE2b-256 | f6da7390a9362062593da7e67a469ddc6b7b5b410728dbc5365b943d35cf60bf |