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 6bbddc4
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 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 @@ -5695,8 +5702,12 @@ private Set<String> getGeneratedFeatures() {
*/
public void writeDevcMetadata(boolean alive) {
File metaFile = new File(buildDirectory, serverDirectory.getName() + "-liberty-devc-metadata.xml");

FileWriter metaFileWriter = null;
XMLStreamWriter metadataWriter = null;
try {
XMLStreamWriter metadataWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(new FileWriter(metaFile)) ;
metaFileWriter = new FileWriter(metaFile);
metadataWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(metaFileWriter) ;
metadataWriter.writeStartDocument();
metadataWriter.writeStartElement("devcModeMetaData");
writeElement(metadataWriter, "containerEngine", isDocker ? DEVC_CONTAINER_DOCKER : DEVC_CONTAINER_PODMAN);
Expand All @@ -5713,10 +5724,21 @@ public void writeDevcMetadata(boolean alive) {
writeElement(metadataWriter, "containerRunOpts", containerRunOpts);
metadataWriter.writeEndElement();
metadataWriter.writeEndDocument();
metadataWriter.flush();
metadataWriter.close();
} catch (Exception e) {
warn("Failed to write metadata.\n" + e.getMessage());
} finally {
try {
if (metadataWriter != null) {
metadataWriter.flush();
metadataWriter.close();
}
if (metaFileWriter != null) {
metaFileWriter.flush();
metaFileWriter.close();
}
} catch (Exception e) {
warn("Failed to close metadata writer due to an error.\n" + e.getMessage());
}
}
}

Expand Down

0 comments on commit 6bbddc4

Please sign in to comment.