# Path: pgctl/functions.py
# def exec_(argv, env=None):  # never returns
#     """Wrapper to os.execv which runs any atexit handlers (for coverage's sake).
#     Like os.execv, this function never returns.
#     """
#     if env is None:
#         env = os.environ
# 
#     # in python3, sys.exitfunc has gone away, and atexit._run_exitfuncs seems to be the only pubic-ish interface
#     #   https://hg.python.org/cpython/file/3.4/Modules/atexitmodule.c#l289
#     import atexit
#     atexit._run_exitfuncs()
#     os.execvpe(argv[0], argv, env)
# 
# Path: pgctl/functions.py
# def print_stderr(s):
#     # Sadness: https://bugs.python.org/issue13601
#     print(s, file=sys.stderr)
#     sys.stderr.flush()


import os.path
import select
import time
from sys import argv
from .functions import exec_
from .functions import print_stderr
    from .subprocess import call
    while True:  # waiting for the service to come up.
        if is_service_down:
            print_stderr('pgctl-poll-ready: service is stopping -- quitting the poll')
            return
        elif check_ready() == 0:
            print_stderr('pgctl-poll-ready: service\'s ready check succeeded')
            os.write(notification_fd, b'ready\n')
            break
        else:
            is_service_down = wait_for_down_signal(down_fifo, poll_ready)

    is_service_down = wait_for_down_signal(down_fifo, 0)
    start = time.time()
    while True:  # heartbeat, continue to check if the service is up. if it becomes down, terminate it.
        elapsed = time.time() - start
        if is_service_down:
            print_stderr('pgctl-poll-ready: service is stopping -- quitting the poll')
            return
        elif check_ready() == 0:
            start = time.time()
            is_service_down = wait_for_down_signal(down_fifo, poll_down)
        elif elapsed < timeout:
            print_stderr(f'pgctl-poll-ready: failed (restarting in {timeout - elapsed:.2f} seconds)')
            is_service_down = wait_for_down_signal(down_fifo, poll_down)
        else:
            service = os.path.basename(os.getcwd())
            # TODO: Add support for directories
            print_stderr(
                f'pgctl-poll-ready: failed for more than {timeout:.2f} seconds -- we are restarting this service for you'
            )

# Path: pgctl/functions.py
# def exec_(argv, env=None):  # never returns
#     """Wrapper to os.execv which runs any atexit handlers (for coverage's sake).
#     Like os.execv, this function never returns.
#     """
#     if env is None:
#         env = os.environ
# 
#     # in python3, sys.exitfunc has gone away, and atexit._run_exitfuncs seems to be the only pubic-ish interface
#     #   https://hg.python.org/cpython/file/3.4/Modules/atexitmodule.c#l289
#     import atexit
#     atexit._run_exitfuncs()
#     os.execvpe(argv[0], argv, env)
# 
# Path: pgctl/functions.py
# def print_stderr(s):
#     # Sadness: https://bugs.python.org/issue13601
#     print(s, file=sys.stderr)
#     sys.stderr.flush()


# Path: pgctl/poll_ready.py
import os.path
import select
import time
from sys import argv
from .functions import exec_
from .functions import print_stderr
    from .subprocess import call

    while True:  # waiting for the service to come up.
        if is_service_down:
            print_stderr('pgctl-poll-ready: service is stopping -- quitting the poll')
            return
        elif check_ready() == 0:
            print_stderr('pgctl-poll-ready: service\'s ready check succeeded')
            os.write(notification_fd, b'ready\n')
            break
        else:
            is_service_down = wait_for_down_signal(down_fifo, poll_ready)

    is_service_down = wait_for_down_signal(down_fifo, 0)
    start = time.time()
    while True:  # heartbeat, continue to check if the service is up. if it becomes down, terminate it.
        elapsed = time.time() - start
        if is_service_down:
            print_stderr('pgctl-poll-ready: service is stopping -- quitting the poll')
            return
        elif check_ready() == 0:
            start = time.time()
            is_service_down = wait_for_down_signal(down_fifo, poll_down)
        elif elapsed < timeout:
            print_stderr(f'pgctl-poll-ready: failed (restarting in {timeout - elapsed:.2f} seconds)')
            is_service_down = wait_for_down_signal(down_fifo, poll_down)
        else:
            service = os.path.basename(os.getcwd())
            # TODO: Add support for directories
            print_stderr(
                f'pgctl-poll-ready: failed for more than {timeout:.2f} seconds -- we are restarting this service for you'
            )

            exec_(('pgctl', 'restart', service))  # doesn't return