-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start Ryuk lazily when in the reusable mode (#4938)
* Lazy Ryuk * Add `ResourceReaper#init`
- Loading branch information
Showing
9 changed files
with
201 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
core/src/main/java/org/testcontainers/utility/JVMHookResourceReaper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.testcontainers.utility; | ||
|
||
import com.github.dockerjava.api.model.Container; | ||
import com.github.dockerjava.api.model.PruneType; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* A {@link ResourceReaper} implementation that uses {@link Runtime#addShutdownHook(Thread)} | ||
* to cleanup containers. | ||
*/ | ||
class JVMHookResourceReaper extends ResourceReaper { | ||
|
||
@Override | ||
public void init() { | ||
setHook(); | ||
} | ||
|
||
@Override | ||
public synchronized void performCleanup() { | ||
super.performCleanup(); | ||
synchronized (DEATH_NOTE) { | ||
DEATH_NOTE.forEach(filters -> prune(PruneType.CONTAINERS, filters)); | ||
DEATH_NOTE.forEach(filters -> prune(PruneType.NETWORKS, filters)); | ||
DEATH_NOTE.forEach(filters -> prune(PruneType.VOLUMES, filters)); | ||
DEATH_NOTE.forEach(filters -> prune(PruneType.IMAGES, filters)); | ||
} | ||
} | ||
|
||
private void prune(PruneType pruneType, List<Map.Entry<String, String>> filters) { | ||
String[] labels = filters.stream() | ||
.filter(it -> "label".equals(it.getKey())) | ||
.map(Map.Entry::getValue) | ||
.toArray(String[]::new); | ||
switch (pruneType) { | ||
// Docker only prunes stopped containers, so we have to do it manually | ||
case CONTAINERS: | ||
List<Container> containers = dockerClient.listContainersCmd() | ||
.withFilter("label", Arrays.asList(labels)) | ||
.withShowAll(true) | ||
.exec(); | ||
|
||
containers.parallelStream().forEach(container -> { | ||
dockerClient.removeContainerCmd(container.getId()) | ||
.withForce(true) | ||
.withRemoveVolumes(true) | ||
.exec(); | ||
}); | ||
break; | ||
default: | ||
dockerClient.pruneCmd(pruneType).withLabelFilter(labels).exec(); | ||
break; | ||
} | ||
} | ||
} |
Oops, something went wrong.