Skip to main content
https://img.shields.io/travis/pmac/urlwait.svg https://img.shields.io/pypi/v/urlwait.svg

I needed a way to block my app from starting until the database service was running and accepting connections. This was particularly a problem when using Docker and docker-compose. You can run this utility as part of a run-dev.sh or run-tests.sh script and it will block for 15 seconds (configurable) until it can connect to the hostname and port as specified in your connection URL (gleaned from DATABASE_URL environment variable by default).

Installation

$ pip install urlwait

Usage

$ 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

The module is also usable in your python code:

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

If you don’t have the service URL but do have the host and port, you can use the wait_for_service(host, port, timeout) function instead.

Python Support

Since version 1.0 urlwait is Python 3 only. If you need Python 2.7 support you can use a pre 1.0 release.

Changelog

  • 1.0 - 2020-02-28 * Close the socket to keep Python from complaining. Thanks @callahad! * Update tests to only test on Python 3.6+ * Drop support for Python 2.x

  • 0.4 - 2017-03-02 - Always return true if protocol is sqlite

Release files for urlwait 1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for urlwait 1.0
File Size Uploaded
urlwait-1.0.tar.gz 5.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for urlwait 1.0
File Interpreter ABI Platform
urlwait-1.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 10.3 kB

Release files / urlwait-1.0.tar.gz

Download URL urlwait-1.0.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
a9bf2da792fa6983fa93f6360108e16615066ab0f9cfb7f53e5faee5f5dffaac
BLAKE2b-256 checksum
How to use checksums
8d9b12a282343d5a81469108039cb59a0fb6b1d520c9772a5ce89bc25d56f1d9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.10

Release files / urlwait-1.0-py2.py3-none-any.whl

Download URL urlwait-1.0-py2.py3-none-any.whl
Size 5.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
eae2c20001efc915166cac79c04bac0088ad5787ec64b36f27afd2f359953b2b
BLAKE2b-256 checksum
How to use checksums
f6da7390a9362062593da7e67a469ddc6b7b5b410728dbc5365b943d35cf60bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.10

Release history Release notifications | RSS feed

This release

1.0 This release

2 release files

0.4

2 release files

0.3

2 release files

0.2

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page