Skip to content

Commit

Permalink
Terminate native image build container when Quarkus build terminates
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Jul 28, 2021
1 parent 95cbb8c commit 82a9e82
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Stream;

import org.apache.commons.lang3.RandomStringUtils;
import org.jboss.logging.Logger;

import io.quarkus.deployment.pkg.NativeConfig;
Expand All @@ -24,6 +26,7 @@ public abstract class NativeImageBuildContainerRunner extends NativeImageBuildRu
protected final NativeConfig.ContainerRuntime containerRuntime;
private final String[] baseContainerRuntimeArgs;
protected final String outputPath;
private final String containerName;

public NativeImageBuildContainerRunner(NativeConfig nativeConfig, Path outputDir) {
this.nativeConfig = nativeConfig;
Expand All @@ -33,7 +36,7 @@ public NativeImageBuildContainerRunner(NativeConfig nativeConfig, Path outputDir
this.baseContainerRuntimeArgs = new String[] { "--env", "LANG=C", "--rm" };

outputPath = outputDir == null ? null : outputDir.toAbsolutePath().toString();

containerName = "build-native-" + RandomStringUtils.random(5, true, false);
}

@Override
Expand Down Expand Up @@ -67,7 +70,12 @@ protected String[] getGraalVMVersionCommand(List<String> args) {

@Override
protected String[] getBuildCommand(List<String> args) {
return buildCommand("run", getContainerRuntimeBuildArgs(), args);
List<String> containerRuntimeBuildArgs = getContainerRuntimeBuildArgs();
List<String> effectiveContainerRuntimeBuildArgs = new ArrayList<>(containerRuntimeBuildArgs.size() + 2);
effectiveContainerRuntimeBuildArgs.addAll(containerRuntimeBuildArgs);
effectiveContainerRuntimeBuildArgs.add("--name");
effectiveContainerRuntimeBuildArgs.add(containerName);
return buildCommand("run", effectiveContainerRuntimeBuildArgs, args);
}

@Override
Expand All @@ -81,6 +89,24 @@ protected void objcopy(String... args) {
runCommand(command, null, null);
}

@Override
public void addShutdownHook(Process process) {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
if (process.isAlive()) {
try {
Process removeProcess = new ProcessBuilder(
List.of(containerRuntime.getExecutableName(), "rm", "-f", containerName))
.redirectOutput(ProcessBuilder.Redirect.DISCARD)
.redirectError(ProcessBuilder.Redirect.DISCARD)
.start();
removeProcess.waitFor(2_000, TimeUnit.SECONDS);
} catch (IOException | InterruptedException e) {
log.debug("Unable to stop running container", e);
}
}
}));
}

protected List<String> getContainerRuntimeBuildArgs() {
List<String> containerRuntimeArgs = new ArrayList<>();
nativeConfig.containerRuntimeOptions.ifPresent(containerRuntimeArgs::addAll);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public GraalVM.Version getGraalVMVersion() {
public void setup(boolean processInheritIODisabled) {
}

public void addShutdownHook(Process buildNativeProcess) {
}

public int build(List<String> args, String nativeImageName, String resultingExecutableName, Path outputDir,
boolean debugEnabled, boolean processInheritIODisabled)
throws InterruptedException, IOException {
Expand All @@ -53,6 +56,7 @@ public int build(List<String> args, String nativeImageName, String resultingExec
.directory(outputDir.toFile());
log.info(String.join(" ", buildCommand).replace("$", "\\$"));
final Process process = ProcessUtil.launchProcessStreamStdOut(processBuilder, processInheritIODisabled);
addShutdownHook(process);
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(new ErrorReplacingProcessReader(process.getErrorStream(), outputDir.resolve("reports").toFile(),
errorReportLatch));
Expand Down

0 comments on commit 82a9e82

Please sign in to comment.