Skip to content

Commit

Permalink
Merge pull request #111 from iamdanfox/shutdown-strategy
Browse files Browse the repository at this point in the history
add new AggressiveShutdownStrategy
  • Loading branch information
iamdanfox authored Oct 1, 2016
2 parents 5ce6b1a + 313ffa0 commit 54882a9
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 24 deletions.
38 changes: 19 additions & 19 deletions src/main/java/com/palantir/docker/compose/DockerComposeRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.palantir.docker.compose.configuration.DockerComposeFiles;
import com.palantir.docker.compose.configuration.ProjectName;
import com.palantir.docker.compose.configuration.ShutdownStrategy;
import com.palantir.docker.compose.connection.Cluster;
import com.palantir.docker.compose.connection.Container;
import com.palantir.docker.compose.connection.ContainerCache;
Expand Down Expand Up @@ -87,6 +88,11 @@ public Docker docker() {
return new Docker(dockerExecutable());
}

@Value.Default
public ShutdownStrategy shutdownStrategy() {
return ShutdownStrategy.GRACEFUL;
}

@Value.Default
public DockerCompose dockerCompose() {
DockerCompose dockerCompose = new DefaultDockerCompose(dockerComposeExecutable(), machine());
Expand All @@ -106,11 +112,6 @@ protected int retryAttempts() {
return DEFAULT_RETRY_ATTEMPTS;
}

@Value.Default
protected boolean skipShutdown() {
return false;
}

@Value.Default
protected boolean removeConflictingContainersOnStartup() {
return true;
Expand Down Expand Up @@ -143,20 +144,7 @@ public void before() throws IOException, InterruptedException {
@Override
public void after() {
try {
if (skipShutdown()) {
log.error("******************************************************************************************\n"
+ "* docker-compose-rule has been configured to skip docker-compose shutdown: *\n"
+ "* this means the containers will be left running after tests finish executing. *\n"
+ "* If you see this message when running on CI it means you are potentially abandoning *\n"
+ "* long running processes and leaking resources. *\n"
+ "*******************************************************************************************");
} else {
log.debug("Killing docker-compose cluster");
dockerCompose().down();
dockerCompose().kill();
dockerCompose().rm();
}

shutdownStrategy().shutdown(dockerCompose());
logCollector().stopCollecting();
} catch (IOException | InterruptedException e) {
throw new RuntimeException("Error cleaning up docker compose cluster", e);
Expand Down Expand Up @@ -187,6 +175,18 @@ public Builder saveLogsTo(String path) {
return logCollector(FileLogCollector.fromPath(path));
}

/**
* @deprecated Please use {@link DockerComposeRule#shutdownStrategy()} with {@link ShutdownStrategy#SKIP} instead.
*/
@Deprecated
public Builder skipShutdown(boolean skipShutdown) {
if (skipShutdown) {
return shutdownStrategy(ShutdownStrategy.SKIP);
}

return this;
}

public Builder waitingForService(String serviceName, HealthCheck<Container> healthCheck) {
return waitingForService(serviceName, healthCheck, DEFAULT_TIMEOUT);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2016 Palantir Technologies, Inc. All rights reserved.
*/

package com.palantir.docker.compose.configuration;

import com.palantir.docker.compose.execution.AggressiveShutdownStrategy;
import com.palantir.docker.compose.execution.DockerCompose;
import com.palantir.docker.compose.execution.GracefulShutdownStrategy;
import com.palantir.docker.compose.execution.SkipShutdownStrategy;
import java.io.IOException;

/**
* How should a cluster of containers be shut down by the `after` method of
* DockerComposeRule.
*/
public interface ShutdownStrategy {

ShutdownStrategy AGGRESSIVE = new AggressiveShutdownStrategy();
ShutdownStrategy GRACEFUL = new GracefulShutdownStrategy();
ShutdownStrategy SKIP = new SkipShutdownStrategy();

void shutdown(DockerCompose dockerCompose) throws IOException, InterruptedException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2016 Palantir Technologies, Inc. All rights reserved.
*/

package com.palantir.docker.compose.execution;

import com.palantir.docker.compose.configuration.ShutdownStrategy;
import java.io.IOException;

/**
* Shuts down containers as fast as possible, without giving them time to finish
* IO or clean up any resources.
*/
public class AggressiveShutdownStrategy implements ShutdownStrategy {

@Override
public void shutdown(DockerCompose dockerCompose) throws IOException, InterruptedException {
dockerCompose.rm();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2016 Palantir Technologies, Inc. All rights reserved.
*/

package com.palantir.docker.compose.execution;

import com.palantir.docker.compose.configuration.ShutdownStrategy;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Send SIGTERM to containers first, allowing them up to 10 seconds to
* terminate before killing and rm-ing them.
*/
public class GracefulShutdownStrategy implements ShutdownStrategy {

private static final Logger log = LoggerFactory.getLogger(GracefulShutdownStrategy.class);

@Override
public void shutdown(DockerCompose dockerCompose) throws IOException, InterruptedException {
log.debug("Killing docker-compose cluster");
dockerCompose.down();
dockerCompose.kill();
dockerCompose.rm();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2016 Palantir Technologies, Inc. All rights reserved.
*/

package com.palantir.docker.compose.execution;

import com.palantir.docker.compose.configuration.ShutdownStrategy;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SkipShutdownStrategy implements ShutdownStrategy {

private static final Logger log = LoggerFactory.getLogger(SkipShutdownStrategy.class);

@Override
public void shutdown(DockerCompose dockerCompose) throws IOException, InterruptedException {
log.warn("******************************************************************************************\n"
+ "* docker-compose-rule has been configured to skip docker-compose shutdown: *\n"
+ "* this means the containers will be left running after tests finish executing. *\n"
+ "* If you see this message when running on CI it means you are potentially abandoning *\n"
+ "* long running processes and leaking resources. *\n"
+ "******************************************************************************************");
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public Container withComposeExecutableReturningContainerFor(String containerName
return container;
}

private ImmutableDockerComposeRule.Builder defaultBuilder() {
private DockerComposeRule.Builder defaultBuilder() {
return DockerComposeRule.builder().dockerCompose(dockerCompose)
.files(mockFiles)
.machine(machine)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.palantir.docker.compose;

import static com.palantir.docker.compose.configuration.ShutdownStrategy.AGGRESSIVE;
import static com.palantir.docker.compose.connection.waiting.ClusterHealthCheck.serviceHealthCheck;
import static com.palantir.docker.compose.connection.waiting.HealthChecks.toHaveAllPortsOpen;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -37,7 +38,11 @@ public class DockerComposeRuleUpContainerIntegrationTest {

@Before
public void setUp() throws Exception {
dockerComposeRule = DockerComposeRule.builder().file(DOCKER_COMPOSE_YAML_PATH).build();
dockerComposeRule = DockerComposeRule
.builder()
.shutdownStrategy(AGGRESSIVE)
.file(DOCKER_COMPOSE_YAML_PATH)
.build();
}

@After
Expand Down
6 changes: 3 additions & 3 deletions src/test/resources/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ db:
- "POSTGRES_USER=palantir"
- "POSTGRES_PASSWORD=palantir"
ports:
- "5442:5432"
db2:
- "5432"

db2:
image: kiasaki/alpine-postgres
environment:
- "POSTGRES_DB=source"
Expand Down

0 comments on commit 54882a9

Please sign in to comment.