mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Polish docker compose code
This commit is contained in:
parent
8abebd75fb
commit
75207db6be
@ -130,7 +130,7 @@ class DockerCli {
|
|||||||
return (line) -> logLevel.log(this.logger, line);
|
return (line) -> logLevel.log(this.logger, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
private <R> List<String> createCommand(Type type) {
|
private List<String> createCommand(Type type) {
|
||||||
return switch (type) {
|
return switch (type) {
|
||||||
case DOCKER -> new ArrayList<>(this.dockerCommand);
|
case DOCKER -> new ArrayList<>(this.dockerCommand);
|
||||||
case DOCKER_COMPOSE -> {
|
case DOCKER_COMPOSE -> {
|
||||||
|
@ -45,8 +45,8 @@ public interface DockerCompose {
|
|||||||
void up(LogLevel logLevel);
|
void up(LogLevel logLevel);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run {@code docker compose down} to shutdown any running services.
|
* Run {@code docker compose down} to shut down any running services.
|
||||||
* @param timeout the amount of time to wait or {@link #FORCE_SHUTDOWN} to shutdown
|
* @param timeout the amount of time to wait or {@link #FORCE_SHUTDOWN} to shut down
|
||||||
* without waiting.
|
* without waiting.
|
||||||
*/
|
*/
|
||||||
void down(Duration timeout);
|
void down(Duration timeout);
|
||||||
@ -59,8 +59,8 @@ public interface DockerCompose {
|
|||||||
void start(LogLevel logLevel);
|
void start(LogLevel logLevel);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run {@code docker compose stop} to shutdown any running services.
|
* Run {@code docker compose stop} to shut down any running services.
|
||||||
* @param timeout the amount of time to wait or {@link #FORCE_SHUTDOWN} to shutdown
|
* @param timeout the amount of time to wait or {@link #FORCE_SHUTDOWN} to shut down
|
||||||
* without waiting.
|
* without waiting.
|
||||||
*/
|
*/
|
||||||
void stop(Duration timeout);
|
void stop(Duration timeout);
|
||||||
|
@ -94,7 +94,7 @@ class DockerComposeLifecycleManager {
|
|||||||
logger.trace("Docker compose support not enabled");
|
logger.trace("Docker compose support not enabled");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (this.skipCheck.shouldSkip(this.classLoader, logger, this.properties.getSkip())) {
|
if (this.skipCheck.shouldSkip(this.classLoader, this.properties.getSkip())) {
|
||||||
logger.trace("Docker compose support skipped");
|
logger.trace("Docker compose support skipped");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ import org.springframework.context.ApplicationListener;
|
|||||||
import org.springframework.context.ConfigurableApplicationContext;
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link ApplicationListener} used to setup a {@link DockerComposeLifecycleManager}.
|
* {@link ApplicationListener} used to set up a {@link DockerComposeLifecycleManager}.
|
||||||
*
|
*
|
||||||
* @author Moritz Halbritter
|
* @author Moritz Halbritter
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
|
@ -24,8 +24,8 @@ import org.springframework.context.ApplicationContext;
|
|||||||
import org.springframework.context.ApplicationEvent;
|
import org.springframework.context.ApplicationEvent;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link ApplicationEvent} published when docker compose {@link RunningService} instance
|
* {@link ApplicationEvent} published when docker compose {@link RunningService} instances
|
||||||
* are available. This even is published from the {@link ApplicationPreparedEvent} that
|
* are available. This event is published from the {@link ApplicationPreparedEvent} that
|
||||||
* performs the docker compose startup.
|
* performs the docker compose startup.
|
||||||
*
|
*
|
||||||
* @author Moritz Halbritter
|
* @author Moritz Halbritter
|
||||||
|
@ -20,8 +20,6 @@ import java.util.Collections;
|
|||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplicationAotProcessor;
|
import org.springframework.boot.SpringApplicationAotProcessor;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
|
|
||||||
@ -46,7 +44,7 @@ class DockerComposeSkipCheck {
|
|||||||
SKIPPED_STACK_ELEMENTS = Collections.unmodifiableSet(skipped);
|
SKIPPED_STACK_ELEMENTS = Collections.unmodifiableSet(skipped);
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean shouldSkip(ClassLoader classLoader, Log logger, DockerComposeProperties.Skip properties) {
|
boolean shouldSkip(ClassLoader classLoader, DockerComposeProperties.Skip properties) {
|
||||||
if (properties.isInTests() && hasAtLeastOneRequiredClass(classLoader)) {
|
if (properties.isInTests() && hasAtLeastOneRequiredClass(classLoader)) {
|
||||||
Thread thread = Thread.currentThread();
|
Thread thread = Thread.currentThread();
|
||||||
for (StackTraceElement element : thread.getStackTrace()) {
|
for (StackTraceElement element : thread.getStackTrace()) {
|
||||||
|
@ -52,7 +52,7 @@ public enum LifecycleManagement {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Return whether docker compose should be started.
|
* Return whether docker compose should be started.
|
||||||
* @return whether docker compose should be started.
|
* @return whether docker compose should be started
|
||||||
*/
|
*/
|
||||||
boolean shouldStartup() {
|
boolean shouldStartup() {
|
||||||
return this.startup;
|
return this.startup;
|
||||||
|
@ -22,7 +22,7 @@ import java.util.function.BiConsumer;
|
|||||||
import org.springframework.boot.docker.compose.core.DockerCompose;
|
import org.springframework.boot.docker.compose.core.DockerCompose;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Command used to shutdown docker compose.
|
* Command used to shut down docker compose.
|
||||||
*
|
*
|
||||||
* @author Moritz Halbritter
|
* @author Moritz Halbritter
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
|
@ -24,7 +24,8 @@ import org.springframework.boot.docker.compose.core.RunningService;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Exception thrown if readiness checking has timed out. Related
|
* Exception thrown if readiness checking has timed out. Related
|
||||||
* {@link ServiceNotReadyException} are available from {@link #getSuppressed()}.
|
* {@link ServiceNotReadyException ServiceNotReadyExceptions} are available from
|
||||||
|
* {@link #getSuppressed()}.
|
||||||
*
|
*
|
||||||
* @author Moritz Halbritter
|
* @author Moritz Halbritter
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
|
@ -24,8 +24,8 @@ import java.net.SocketTimeoutException;
|
|||||||
import org.springframework.boot.docker.compose.core.RunningService;
|
import org.springframework.boot.docker.compose.core.RunningService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default {@link ServiceReadinessCheck} that readiness by connecting to the exposed TCP
|
* Default {@link ServiceReadinessCheck} that checks readiness by connecting to the
|
||||||
* ports.
|
* exposed TCP ports.
|
||||||
*
|
*
|
||||||
* @author Moritz Halbritter
|
* @author Moritz Halbritter
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
@ -66,7 +66,7 @@ class TcpConnectServiceReadinessCheck implements ServiceReadinessCheck {
|
|||||||
|
|
||||||
private void check(RunningService service, int port, Socket socket) throws IOException {
|
private void check(RunningService service, int port, Socket socket) throws IOException {
|
||||||
try {
|
try {
|
||||||
// -1 is indicates the socket has been closed immediately
|
// -1 indicates the socket has been closed immediately
|
||||||
// Other responses or a timeout are considered as success
|
// Other responses or a timeout are considered as success
|
||||||
if (socket.getInputStream().read() == -1) {
|
if (socket.getInputStream().read() == -1) {
|
||||||
throw new ServiceNotReadyException(service,
|
throw new ServiceNotReadyException(service,
|
||||||
|
@ -28,7 +28,7 @@ import org.springframework.boot.docker.compose.core.RunningService;
|
|||||||
*/
|
*/
|
||||||
class ConnectionNamePredicate implements Predicate<DockerComposeConnectionSource> {
|
class ConnectionNamePredicate implements Predicate<DockerComposeConnectionSource> {
|
||||||
|
|
||||||
private String required;
|
private final String required;
|
||||||
|
|
||||||
ConnectionNamePredicate(String required) {
|
ConnectionNamePredicate(String required) {
|
||||||
this.required = asCanonicalName(required);
|
this.required = asCanonicalName(required);
|
||||||
|
@ -74,14 +74,13 @@ class DockerComposeServiceConnectionsApplicationListener
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private <T> void register(BeanDefinitionRegistry registry, RunningService runningService,
|
private <T> void register(BeanDefinitionRegistry registry, RunningService runningService,
|
||||||
Class<?> connectionDetailsType, ConnectionDetails connectionDetails) {
|
Class<?> connectionDetailsType, ConnectionDetails connectionDetails) {
|
||||||
String beanName = getBeanName(runningService, connectionDetailsType, connectionDetails);
|
String beanName = getBeanName(runningService, connectionDetailsType);
|
||||||
Class<T> beanType = (Class<T>) connectionDetails.getClass();
|
Class<T> beanType = (Class<T>) connectionDetails.getClass();
|
||||||
Supplier<T> beanSupplier = () -> (T) connectionDetails;
|
Supplier<T> beanSupplier = () -> (T) connectionDetails;
|
||||||
registry.registerBeanDefinition(beanName, new RootBeanDefinition(beanType, beanSupplier));
|
registry.registerBeanDefinition(beanName, new RootBeanDefinition(beanType, beanSupplier));
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getBeanName(RunningService runningService, Class<?> connectionDetailsType,
|
private String getBeanName(RunningService runningService, Class<?> connectionDetailsType) {
|
||||||
ConnectionDetails connectionDetails) {
|
|
||||||
List<String> parts = new ArrayList<>();
|
List<String> parts = new ArrayList<>();
|
||||||
parts.add(ClassUtils.getShortNameAsProperty(connectionDetailsType));
|
parts.add(ClassUtils.getShortNameAsProperty(connectionDetailsType));
|
||||||
parts.add("for");
|
parts.add("for");
|
||||||
|
@ -115,7 +115,7 @@ class DockerComposeLifecycleManagerTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void startupWhenInTestDoesNotStart() {
|
void startupWhenInTestDoesNotStart() {
|
||||||
given(this.skipCheck.shouldSkip(any(), any(), any())).willReturn(true);
|
given(this.skipCheck.shouldSkip(any(), any())).willReturn(true);
|
||||||
EventCapturingListener listener = new EventCapturingListener();
|
EventCapturingListener listener = new EventCapturingListener();
|
||||||
this.eventListeners.add(listener);
|
this.eventListeners.add(listener);
|
||||||
setupRunningServices();
|
setupRunningServices();
|
||||||
|
@ -56,7 +56,7 @@ class DockerComposeListenerTests {
|
|||||||
then(listener.getManager()).should().startup();
|
then(listener.getManager()).should().startup();
|
||||||
}
|
}
|
||||||
|
|
||||||
class TestDockerComposeListener extends DockerComposeListener {
|
static class TestDockerComposeListener extends DockerComposeListener {
|
||||||
|
|
||||||
private final ConfigurableApplicationContext context;
|
private final ConfigurableApplicationContext context;
|
||||||
|
|
||||||
|
@ -67,7 +67,7 @@ class ServiceReadinessChecksTests {
|
|||||||
|
|
||||||
private List<RunningService> runningServices;
|
private List<RunningService> runningServices;
|
||||||
|
|
||||||
private MockServiceReadinessCheck mockTcpCheck = new MockServiceReadinessCheck();
|
private final MockServiceReadinessCheck mockTcpCheck = new MockServiceReadinessCheck();
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setup() {
|
void setup() {
|
||||||
|
Loading…
Reference in New Issue
Block a user