Created
February 27, 2017 16:26
-
-
Save yoanisgil/c56043c15cbfff885a974f3966c5de4f to your computer and use it in GitHub Desktop.
Wait for port
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import sys | |
import time | |
import telnetlib | |
check_timeout = int(os.environ.get('CHECK_TIMEOUT', 1)) | |
sleep_interval = int(os.environ.get('SLEEP_INTERVAL', 1)) | |
port_to_check = int(os.environ.get('PORT_TO_CHECK')) | |
max_retries = int(os.environ.get('MAX_RETRIES', 20)) | |
host = os.environ.get('HOST', 'localhost') | |
failed_attempts = 0 | |
successful_attempts = 0 | |
for _ in range(max_retries): | |
try: | |
conn = telnetlib.Telnet(host, port_to_check, check_timeout) | |
successful_attempts += 1 | |
sys.stdout.write('Port %s:%s is active. Wil recheck in %s seconds\n' % ( | |
host, port_to_check, sleep_interval)) | |
time.sleep(sleep_interval) | |
except Exception, e: | |
sys.stderr.write('%s\n' % repr(e)) | |
sys.stdout.write('Sleeping %s sec before checking service status\n' % sleep_interval) | |
time.sleep(sleep_interval) | |
if successful_attempts > failed_attempts: | |
sys.stdout.write('Port %s is now active :)\n' % port_to_check) | |
sys.exit(0) | |
else: | |
sys.stderr.write('Service dit not started after %s retries\n' % max_retries) | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment