mirror of https://github.com/apache/druid.git
ForkingTaskRunner: Use guava Closer to closer stuff
This commit is contained in:
parent
952b8ce06b
commit
5d44f0f15b
|
@ -32,6 +32,7 @@ import com.google.common.collect.Lists;
|
|||
import com.google.common.collect.Maps;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.google.common.io.Closeables;
|
||||
import com.google.common.io.Closer;
|
||||
import com.google.common.io.Files;
|
||||
import com.google.common.io.InputSupplier;
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
|
@ -115,6 +116,8 @@ public class ForkingTaskRunner implements TaskRunner, TaskLogProvider
|
|||
|
||||
final ProcessHolder processHolder;
|
||||
|
||||
try {
|
||||
final Closer closer = Closer.create();
|
||||
try {
|
||||
if (!attemptDir.mkdirs()) {
|
||||
throw new IOException(String.format("Could not create directories: %s", attemptDir));
|
||||
|
@ -204,33 +207,24 @@ public class ForkingTaskRunner implements TaskRunner, TaskLogProvider
|
|||
);
|
||||
|
||||
processHolder = taskInfo.processHolder;
|
||||
processHolder.registerWithCloser(closer);
|
||||
}
|
||||
|
||||
log.info("Logging task %s output to: %s", task.getId(), logFile);
|
||||
|
||||
final InputStream fromProc = processHolder.process.getInputStream();
|
||||
final OutputStream toLogfile = Files.newOutputStreamSupplier(logFile).getOutput();
|
||||
final OutputStream toLogfile = closer.register(
|
||||
Files.newOutputStreamSupplier(logFile).getOutput()
|
||||
);
|
||||
|
||||
boolean runFailed = false;
|
||||
boolean runFailed = true;
|
||||
|
||||
try {
|
||||
ByteStreams.copy(fromProc, toLogfile);
|
||||
final int statusCode = processHolder.process.waitFor();
|
||||
log.info("Process exited with status[%d] for task: %s", statusCode, task.getId());
|
||||
|
||||
if (statusCode != 0) {
|
||||
runFailed = true;
|
||||
}
|
||||
|
||||
toLogfile.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.warn(e, "Failed to log process output for task: %s", task.getId());
|
||||
runFailed = true;
|
||||
Closeables.close(toLogfile, true);
|
||||
}
|
||||
finally {
|
||||
Closeables.close(processHolder, true);
|
||||
if (statusCode == 0) {
|
||||
runFailed = false;
|
||||
}
|
||||
|
||||
// Upload task logs
|
||||
|
@ -248,6 +242,11 @@ public class ForkingTaskRunner implements TaskRunner, TaskLogProvider
|
|||
// Process exited unsuccessfully
|
||||
return TaskStatus.failure(task.getId());
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
throw closer.rethrow(t);
|
||||
} finally {
|
||||
closer.close();
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.info(e, "Exception caught during execution");
|
||||
|
@ -403,7 +402,7 @@ public class ForkingTaskRunner implements TaskRunner, TaskLogProvider
|
|||
}
|
||||
}
|
||||
|
||||
private static class ProcessHolder implements Closeable
|
||||
private static class ProcessHolder
|
||||
{
|
||||
private final Process process;
|
||||
private final File logFile;
|
||||
|
@ -416,11 +415,10 @@ public class ForkingTaskRunner implements TaskRunner, TaskLogProvider
|
|||
this.port = port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException
|
||||
private void registerWithCloser(Closer closer)
|
||||
{
|
||||
process.getInputStream().close();
|
||||
process.getOutputStream().close();
|
||||
closer.register(process.getInputStream());
|
||||
closer.register(process.getOutputStream());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue