Skip to content

Instantly share code, notes, and snippets.

@slartibaartfast
Created June 15, 2017 16:05
Show Gist options
  • Save slartibaartfast/f6d25fb05b2d7860c807e6dada8d6549 to your computer and use it in GitHub Desktop.
Save slartibaartfast/f6d25fb05b2d7860c807e6dada8d6549 to your computer and use it in GitHub Desktop.
See if a service is ready to be included
# check_db.py
import socket
import time
import argparse
""" Check if port is open, avoid docker-compose race condition """
parser = argparse.ArgumentParser(description='Check if port is open, avoid\
docker-compose race condition')
parser.add_argument('--service-name', required=True)
parser.add_argument('--ip', required=True)
parser.add_argument('--port', required=True)
args = parser.parse_args()
# Get arguments
service_name = str(args.service_name)
port = int(args.port)
ip = str(args.ip)
# Infinite loop
while True:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((ip, port))
if result == 0:
print("{0} port is open! Bye!".format(service_name))
break
else:
print("{0} port is not open! I'll check it soon!".format(service_name))
time.sleep(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment