用于阻塞直到服务监听的cli实用程序
urlwait的Python项目详细描述
我需要一种方法来阻止我的应用程序从启动到数据库服务运行 接受连接。这在使用docker和docker compose时尤其是一个问题。 可以将此实用程序作为run-dev.sh或run-tests.sh脚本的一部分运行,它将 阻塞15秒(可配置),直到它可以连接到指定的主机名和端口 在您的连接URL中(默认情况下从数据库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)函数。
更改日志
- 0.4-2017-03-02-如果协议为sqlite,则始终返回true