"""
Block until all dependent services come online.
"""
import requests
import time
import sys
from relation_engine_server.utils.config import get_config

_CONF = get_config()


def wait_for_service(service_list):
    '''wait for a service or list of services to start up'''
    timeout = int(time.time()) + 60

    service_conf_list = [get_service_conf(s) for s in service_list]

    while True:
        try:
            for service in service_conf_list:
                name = service['name']
                url = service['url']
                if service['auth'] is not None:
                    requests.get(service['url'], auth=service['auth']).raise_for_status()
                else:
                    # auth and workspace both return 500, so don't raise_for_status
                    requests.get(service['url'])
            break
        except Exception:
            print(f"Waiting for {name} to start...")
            if int(time.time()) > timeout:
                raise RuntimeError(f"Timed out waiting for {name}, {url}")
            time.sleep(3)
    print(f"{name} started!")


def get_service_conf(service_name):

    service_conf = {
        'arangodb': {
            'url': _CONF['db_url'] + '/_admin/cluster/health',
            # server auth credentials
            'auth': (_CONF['db_user'], _CONF['db_pass']),
        },
        'auth': {
            'url': _CONF['auth_url'],
        },
        'workspace': {
            'url': _CONF['workspace_url'],
        },
        'localhost': {
            'url': 'http://127.0.0.1:5000',
        }
    }

    if service_name not in service_conf:
        raise KeyError(f'Configuration for {service_name} not found')

    return {
        'name': service_name,
        # auth defaults to None if there is nothing set
        'auth': service_conf[service_name].get('auth'),
        'url': service_conf[service_name]['url'],
    }


def wait_for_arangodb():
    '''wait for arangodb to be ready'''

    wait_for_service(['arangodb'])


def wait_for_services():
    '''wait for the workspace, auth, and arango to start up'''

    wait_for_service(['auth', 'workspace', 'arangodb'])


def wait_for_api():
    '''wait for the workspace, auth, arango, AND localhost:5000 to start up'''

    wait_for_services()
    wait_for_service(['localhost'])


if __name__ == '__main__':
    if sys.argv[1] == 'services':
        wait_for_services()
    elif sys.argv[1] == 'api':
        wait_for_api()

"""
Block until all dependent services come online.
"""
import requests
import time
import sys
from relation_engine_server.utils.config import get_config

_CONF = get_config()


def wait_for_service(service_list):
    '''wait for a service or list of services to start up'''
    timeout = int(time.time()) + 60

    service_conf_list = [get_service_conf(s) for s in service_list]

    while True:
        try:
            for service in service_conf_list:
                name = service['name']
                url = service['url']
                if service['auth'] is not None:
                    requests.get(service['url'], auth=service['auth']).raise_for_status()
                else:
                    # auth and workspace both return 500, so don't raise_for_status
                    requests.get(service['url'])
            break
        except Exception:
            print(f"Waiting for {name} to start...")
            if int(time.time()) > timeout:
                raise RuntimeError(f"Timed out waiting for {name}, {url}")
            time.sleep(3)
    print(f"{name} started!")


def get_service_conf(service_name):

    service_conf = {
        'arangodb': {
            'url': _CONF['db_url'] + '/_admin/cluster/health',
            # server auth credentials
            'auth': (_CONF['db_user'], _CONF['db_pass']),
        },
        'auth': {
            'url': _CONF['auth_url'],
        },
        'workspace': {
            'url': _CONF['workspace_url'],
        },
        'localhost': {
            'url': 'http://127.0.0.1:5000',
        }
    }

    if service_name not in service_conf:
        raise KeyError(f'Configuration for {service_name} not found')

    return {
        'name': service_name,
        # auth defaults to None if there is nothing set
        'auth': service_conf[service_name].get('auth'),
        'url': service_conf[service_name]['url'],
    }


def wait_for_arangodb():
    '''wait for arangodb to be ready'''

    wait_for_service(['arangodb'])


def wait_for_services():
    '''wait for the workspace, auth, and arango to start up'''

    wait_for_service(['auth', 'workspace', 'arangodb'])


def wait_for_api():
    '''wait for the workspace, auth, arango, AND localhost:5000 to start up'''

    wait_for_services()
    wait_for_service(['localhost'])


if __name__ == '__main__':
    if sys.argv[1] == 'services':
        wait_for_services()
    elif sys.argv[1] == 'api':
        wait_for_api()

Block until all dependent services come online. wait for a service or list of services to start up # auth and workspace both return 500, so don't raise_for_status # server auth credentials # auth defaults to None if there is nothing set wait for arangodb to be ready wait for the workspace, auth, and arango to start up wait for the workspace, auth, arango, AND localhost:5000 to start up