'Forward ports to the deployed service.
This is private to ensure that it is called with the lock.
The lock is needed to mitigate a race condition. See the
inline comment around the Popen call.'

def __forward_port_to_service(self, service_name):

    local_port = _unused_port()
    remote_port = self.__service_port_map[service_name]
    command = self.__deployer.make_port_forward_command(service_name, local_port, remote_port)
    logging.info('Establishing    connection    to    %s    with    port    %d', service_name, local_port)
    logging.debug('RUNNING    %s', '    '.join(command))
    class KeepAlive(threading.Thread, ):
        def run(self):
            while True:
                try:
                    logging.info('KeepAlive    %s    polling', service_name)
                    got = urllib2.urlopen('http://localhost:{port}/health'.format(port=local_port))
                    logging.info('KeepAlive    %s    ->    %s', service_name, got.getcode())
                except Exception as ex:
                    logging.info('KeepAlive    %s    ->    %s', service_name, ex)
                time.sleep(20)
    if (self.options.deploy_spinnaker_type == 'distributed'):
        hack = KeepAlive()
        hack.setDaemon(True)
        hack.start()
    child = subprocess.Popen(command, stderr=sys.stderr.fileno(), stdout=None)
    return ForwardedPort(child, local_port)
