Home / Class/ ServiceReadinessChecks Class — spring-boot Architecture

ServiceReadinessChecks Class — spring-boot Architecture

Architecture documentation for the ServiceReadinessChecks class in ServiceReadinessChecks.java from the spring-boot codebase.

Entity Profile

Relationship Graph

Source Code

core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/lifecycle/ServiceReadinessChecks.java lines 40–122

class ServiceReadinessChecks {

	private static final Log logger = LogFactory.getLog(ServiceReadinessChecks.class);

	private static final String DISABLE_LABEL = "org.springframework.boot.readiness-check.disable";

	private static final Duration SLEEP_BETWEEN_READINESS_TRIES = Duration.ofSeconds(1);

	private final Clock clock;

	private final Consumer<Duration> sleep;

	private final DockerComposeProperties.Readiness properties;

	private final TcpConnectServiceReadinessCheck check;

	ServiceReadinessChecks(DockerComposeProperties.Readiness properties) {
		this(properties, Clock.systemUTC(), ServiceReadinessChecks::sleep,
				new TcpConnectServiceReadinessCheck(properties.getTcp()));
	}

	ServiceReadinessChecks(DockerComposeProperties.Readiness properties, Clock clock, Consumer<Duration> sleep,
			TcpConnectServiceReadinessCheck check) {
		this.clock = clock;
		this.sleep = sleep;
		this.properties = properties;
		this.check = check;
	}

	/**
	 * Wait for the given services to be ready.
	 * @param runningServices the services to wait for
	 */
	void waitUntilReady(List<RunningService> runningServices) {
		Duration timeout = this.properties.getTimeout();
		Instant start = this.clock.instant();
		while (true) {
			List<ServiceNotReadyException> exceptions = check(runningServices);
			if (exceptions.isEmpty()) {
				return;
			}
			Duration elapsed = Duration.between(start, this.clock.instant());
			if (elapsed.compareTo(timeout) > 0) {
				throw new ReadinessTimeoutException(timeout, exceptions);
			}
			this.sleep.accept(SLEEP_BETWEEN_READINESS_TRIES);
		}
	}

	private List<ServiceNotReadyException> check(List<RunningService> runningServices) {
		List<ServiceNotReadyException> exceptions = null;
		for (RunningService service : runningServices) {
			if (isDisabled(service)) {
				continue;
			}
			logger.trace(LogMessage.format("Checking readiness of service '%s'", service));
			try {
				this.check.check(service);
				logger.trace(LogMessage.format("Service '%s' is ready", service));
			}
			catch (ServiceNotReadyException ex) {
				logger.trace(LogMessage.format("Service '%s' is not ready", service), ex);
				exceptions = (exceptions != null) ? exceptions : new ArrayList<>();
				exceptions.add(ex);
			}
		}
		return (exceptions != null) ? exceptions : Collections.emptyList();
	}

	private boolean isDisabled(RunningService service) {
		return service.labels().containsKey(DISABLE_LABEL);
	}

	private static void sleep(Duration duration) {
		try {
			Thread.sleep(duration.toMillis());
		}
		catch (InterruptedException ex) {
			Thread.currentThread().interrupt();
		}
	}

}

Domain

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free