Skip to content

Commit

Permalink
Ensure devc metadata file writers close
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbsox committed Nov 13, 2023
1 parent 1172cbb commit 2d0fd94
Showing 1 changed file with 48 additions and 27 deletions.
75 changes: 48 additions & 27 deletions src/main/java/io/openliberty/tools/common/plugins/util/DevUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@

import com.sun.nio.file.SensitivityWatchEventModifier;

import io.openliberty.tools.ant.ServerTask;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
Expand All @@ -96,8 +98,6 @@
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import io.openliberty.tools.ant.ServerTask;

/**
* Utility class for dev mode.
*/
Expand Down Expand Up @@ -1280,7 +1280,9 @@ private void buildContainerImage(File tempContainerfile, File userContainerfile,
info("The RUN features.sh command is detected in the Containerfile and extra time may be necessary when installing features.");
}
long startTime = System.currentTimeMillis();
execContainerCmdAndLog(getRunProcess(buildCmd), containerBuildTimeout);

execContainerCmdAndLog(getRunProcess(buildCmd), containerBuildTimeout, false);

checkContainerBuildTime(startTime, buildContext);
info("Completed building container image.");
} catch (IllegalThreadStateException e) {
Expand Down Expand Up @@ -1338,7 +1340,7 @@ private void startContainer() throws PluginExecutionException {
String startContainerCommand = getContainerCommand();
info(startContainerCommand);
containerRunProcess = getRunProcess(startContainerCommand);
execContainerCmdAndLog(containerRunProcess, 0);
execContainerCmdAndLog(containerRunProcess, 0, true);
} catch (IOException e) {
error("Error starting container: " + e.getMessage());
throw new RuntimeException(e);
Expand Down Expand Up @@ -1374,7 +1376,7 @@ private Process getRunProcess(String command) throws IOException {
return processBuilder.start();
}

private void execContainerCmdAndLog(final Process startingProcess, int timeout) throws InterruptedException {
private void execContainerCmdAndLog(final Process startingProcess, int timeout, boolean isStartCommand) throws InterruptedException {
Thread logCopyInputThread = new Thread(new Runnable() {
@Override
public void run() {
Expand All @@ -1396,7 +1398,10 @@ public void run() {
});
logCopyErrorThread.start();

writeDevcMetadata(true);
if (isStartCommand) {
writeDevcMetadata(true);
}

if (timeout == 0) {
startingProcess.waitFor();
} else {
Expand All @@ -1411,7 +1416,9 @@ public void run() {
debug("Unexpected exit running container command, return value=" + startingProcess.exitValue());
// show first message from standard err
String errorMessage = new String(firstErrorLine).trim() + " RC=" + startingProcess.exitValue();
writeDevcMetadata(false);
if (isStartCommand) {
writeDevcMetadata(false);
}
throw new RuntimeException(errorMessage);
}
}
Expand Down Expand Up @@ -5696,27 +5703,41 @@ private Set<String> getGeneratedFeatures() {
public void writeDevcMetadata(boolean alive) {
File metaFile = new File(buildDirectory, serverDirectory.getName() + "-liberty-devc-metadata.xml");
try {
XMLStreamWriter metadataWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(new FileWriter(metaFile)) ;
metadataWriter.writeStartDocument();
metadataWriter.writeStartElement("devcModeMetaData");
writeElement(metadataWriter, "containerEngine", isDocker ? DEVC_CONTAINER_DOCKER : DEVC_CONTAINER_PODMAN);
writeElement(metadataWriter, "containerName", containerName != null ? containerName : DEVMODE_CONTAINER_BASE_NAME);
writeElement(metadataWriter, "imageName", imageName);
if (containerfile != null) {
writeElement(metadataWriter, "containerfile", containerfile.getCanonicalPath());
}
if (containerBuildContext != null) {
writeElement(metadataWriter, "containerBuildContext", containerBuildContext.getCanonicalPath());
}
writeElement(metadataWriter, "containerAlive", String.valueOf(alive));
writeElement(metadataWriter, "containerBuildTimeout", Integer.toString(containerBuildTimeout));
writeElement(metadataWriter, "containerRunOpts", containerRunOpts);
metadataWriter.writeEndElement();
metadataWriter.writeEndDocument();
metadataWriter.flush();
metadataWriter.close();
FileWriter metaFileWriter = null;
XMLStreamWriter metadataWriter = null;
try {
metaFileWriter = new FileWriter(metaFile);
metadataWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(metaFileWriter) ;
metadataWriter.writeStartDocument();
metadataWriter.writeStartElement("devcModeMetaData");
writeElement(metadataWriter, "containerEngine", isDocker ? DEVC_CONTAINER_DOCKER : DEVC_CONTAINER_PODMAN);
writeElement(metadataWriter, "containerName", containerName != null ? containerName : DEVMODE_CONTAINER_BASE_NAME);
writeElement(metadataWriter, "imageName", imageName);
if (containerfile != null) {
writeElement(metadataWriter, "containerfile", containerfile.getCanonicalPath());
}
if (containerBuildContext != null) {
writeElement(metadataWriter, "containerBuildContext", containerBuildContext.getCanonicalPath());
}
writeElement(metadataWriter, "containerAlive", String.valueOf(alive));
writeElement(metadataWriter, "containerBuildTimeout", Integer.toString(containerBuildTimeout));
writeElement(metadataWriter, "containerRunOpts", containerRunOpts);
metadataWriter.writeEndElement();
metadataWriter.writeEndDocument();
} catch (Exception e) {
warn("Failed to write metadata.\n" + e.getMessage());
} finally {
if (metadataWriter != null) {
metadataWriter.flush();
metadataWriter.close();
}
if (metaFileWriter != null) {
metaFileWriter.flush();
metaFileWriter.close();
}
}
} catch (Exception e) {
warn("Failed to write metadata.\n" + e.getMessage());
warn("Failed to write metadata due to error creating writer.\n" + e.getMessage());
}
}

Expand Down

0 comments on commit 2d0fd94

Please sign in to comment.