|
44 | 44 | import org.mandas.docker.client.LogStream; |
45 | 45 | import org.mandas.docker.client.exceptions.DockerException; |
46 | 46 | import org.mandas.docker.client.exceptions.ServiceNotFoundException; |
| 47 | +import org.mandas.docker.client.exceptions.TaskNotFoundException; |
47 | 48 | import org.mandas.docker.client.messages.RegistryAuth; |
48 | 49 | import org.mandas.docker.client.messages.mount.Mount; |
49 | 50 | import org.mandas.docker.client.messages.swarm.DnsConfig; |
|
53 | 54 | import org.mandas.docker.client.messages.swarm.Reservations; |
54 | 55 | import org.mandas.docker.client.messages.swarm.ResourceRequirements; |
55 | 56 | import org.mandas.docker.client.messages.swarm.Resources; |
| 57 | +import org.mandas.docker.client.messages.swarm.RestartPolicy; |
56 | 58 | import org.mandas.docker.client.messages.swarm.SecretBind; |
57 | 59 | import org.mandas.docker.client.messages.swarm.SecretFile; |
58 | 60 | import org.mandas.docker.client.messages.swarm.Service; |
@@ -314,7 +316,7 @@ public List<ExistingContainerInfo> scanExistingContainers() throws Exception { |
314 | 316 | ArrayList<ExistingContainerInfo> containers = new ArrayList<>(); |
315 | 317 |
|
316 | 318 | for (Service service : dockerClient.listServices()) { |
317 | | - org.mandas.docker.client.messages.swarm.ContainerSpec containerSpec = service.spec().taskTemplate().containerSpec(); |
| 319 | + org.mandas.docker.client.messages.swarm.ContainerSpec containerSpec = service.spec().taskTemplate().containerSpec(); |
318 | 320 |
|
319 | 321 | if (containerSpec == null) { |
320 | 322 | continue; |
@@ -359,7 +361,34 @@ public List<ExistingContainerInfo> scanExistingContainers() throws Exception { |
359 | 361 |
|
360 | 362 | @Override |
361 | 363 | public boolean isProxyHealthy(Proxy proxy) { |
362 | | - return true; // TODO |
| 364 | + for (Container container : proxy.getContainers()) { |
| 365 | + try { |
| 366 | + BackendContainerName serviceId = container.getRuntimeObjectOrNull(BackendContainerNameKey.inst); |
| 367 | + if (serviceId == null) { |
| 368 | + slog.warn(proxy, "Docker Swarm container failed: no service id found"); |
| 369 | + return false; |
| 370 | + } |
| 371 | + dockerClient.inspectService(serviceId.getName()); // check service exists |
| 372 | + List<Task> serviceTask = dockerClient.listTasks(Task.Criteria.builder().serviceName(serviceId.getName()).build()); |
| 373 | + if (serviceTask.isEmpty()) { |
| 374 | + slog.warn(proxy, "Docker Swarm container failed: service does not exist"); |
| 375 | + return false; |
| 376 | + } |
| 377 | + Task task = serviceTask.get(0); |
| 378 | + if (!task.status().state().equals("running")) { |
| 379 | + slog.warn(proxy, "Docker Swarm container failed: container not running, state reported by docker: " + toJson(task.status())); |
| 380 | + return false; |
| 381 | + } |
| 382 | + return true; |
| 383 | + } catch (ServiceNotFoundException e) { |
| 384 | + slog.warn(proxy, "Docker Swarm container failed: service does not exist"); |
| 385 | + return false; |
| 386 | + } catch (DockerException | InterruptedException e) { |
| 387 | + slog.warn(proxy, e, "Failed to check Docker Swarm container health"); |
| 388 | + return false; |
| 389 | + } |
| 390 | + } |
| 391 | + return true; |
363 | 392 | } |
364 | 393 |
|
365 | 394 | @Override |
|
0 commit comments