@Override public boolean check(final String containerId) throws InterruptedException, DockerException { final Integer port = taskConfig.ports().get(healthCheck.getPort()).getExternalPort(); InetSocketAddress address = new InetSocketAddress(dockerHost.address(), port); if (address.getAddress().isLoopbackAddress()) { // tcp connections to a container-mapped port on loopback always succeed, // regardless of if the container is listening or not. use the bridge address instead. address = new InetSocketAddress(getBridgeAddress(containerId), port); } log.info("about to tcp healthcheck containerId={} with address={} for task={}", containerId, address, taskConfig); try (final Socket s = new Socket()) { s.connect(address, CONNECT_TIMEOUT_MILLIS); } catch (Exception e) { log.warn("tcp healthcheck failed for containerId={} due to exception={}", containerId, e.toString()); return false; } return true; } }
private static String formatHealthCheck(final HealthCheck healthCheck) { if (healthCheck == null) { return ""; } String str = String.format("type: %s", String.valueOf(healthCheck.getType())); if (healthCheck instanceof HttpHealthCheck) { final HttpHealthCheck httpHealthCheck = (HttpHealthCheck) healthCheck; str += String.format(", port: %s, path: %s", httpHealthCheck.getPort(), httpHealthCheck.getPath()); } else if (healthCheck instanceof TcpHealthCheck) { final TcpHealthCheck tcpHealthCheck = (TcpHealthCheck) healthCheck; str += String.format(", port: %s", tcpHealthCheck.getPort()); } else if (healthCheck instanceof ExecHealthCheck) { final ExecHealthCheck execHealthCheck = (ExecHealthCheck) healthCheck; str += String.format(", command: %s", Joiner.on(" ").join(execHealthCheck.getCommand())); } return str; }
port = ((HttpHealthCheck) healthCheck).getPort(); } else { port = ((TcpHealthCheck) healthCheck).getPort();
/** * Get endpoint health check for a given port. * * @param portName The port name * * @return An EndpointHealthCheck or null if no check exists */ private ServiceRegistration.EndpointHealthCheck endpointHealthCheck(String portName) { if (healthCheck() instanceof HttpHealthCheck) { final HttpHealthCheck httpHealthCheck = (HttpHealthCheck) healthCheck(); if (portName.equals(httpHealthCheck.getPort())) { return ServiceRegistration.EndpointHealthCheck.newHttpCheck(httpHealthCheck.getPath()); } } else if (healthCheck() instanceof TcpHealthCheck) { if (portName.equals(((TcpHealthCheck) healthCheck()).getPort())) { return ServiceRegistration.EndpointHealthCheck.newTcpCheck(); } } return null; }
private static String formatHealthCheck(final HealthCheck healthCheck) { if (healthCheck == null) { return ""; } String s = String.format("type: %s", String.valueOf(healthCheck.getType())); if (healthCheck instanceof HttpHealthCheck) { final HttpHealthCheck httpHealthCheck = (HttpHealthCheck) healthCheck; s += String.format(", port: %s, path: %s", httpHealthCheck.getPort(), httpHealthCheck.getPath()); } else if (healthCheck instanceof TcpHealthCheck) { final TcpHealthCheck tcpHealthCheck = (TcpHealthCheck) healthCheck; s += String.format(", port: %s", tcpHealthCheck.getPort()); } else if (healthCheck instanceof ExecHealthCheck) { final ExecHealthCheck execHealthCheck = (ExecHealthCheck) healthCheck; s += String.format(", command: %s", Joiner.on(" ").join(execHealthCheck.getCommand())); } return s; }
@Override public boolean check(final String containerId) throws InterruptedException, DockerException { final Integer port = taskConfig.ports().get(healthCheck.getPort()).getExternalPort(); InetSocketAddress address = new InetSocketAddress(dockerHost.address(), port); if (address.getAddress().isLoopbackAddress()) { // tcp connections to a container-mapped port on loopback always succeed, // regardless of if the container is listening or not. use the bridge address instead. final String bridge = docker.inspectContainer(containerId).networkSettings().gateway(); address = new InetSocketAddress(bridge, port); } try (final Socket s = new Socket()) { s.connect(address, CONNECT_TIMEOUT_MILLIS); } catch (Exception e) { return false; } return true; } }
/** * Get endpoint health check for a given port * @param portName The port name * @return An EndpointHealthCheck or null if no check exists */ private ServiceRegistration.EndpointHealthCheck endpointHealthCheck(String portName) { if (healthCheck() instanceof HttpHealthCheck) { HttpHealthCheck httpHealthCheck = (HttpHealthCheck) healthCheck(); if (portName.equals(httpHealthCheck.getPort())) { return ServiceRegistration.EndpointHealthCheck.newHttpCheck(httpHealthCheck.getPath()); } } else if (healthCheck() instanceof TcpHealthCheck) { if (portName.equals(((TcpHealthCheck) healthCheck()).getPort())) { return ServiceRegistration.EndpointHealthCheck.newTcpCheck(); } } return null; }