From 6324225a4f01e6f711a3efcb20128416d2e027af Mon Sep 17 00:00:00 2001 From: Gian Merlino Date: Tue, 19 Mar 2013 16:32:01 -0700 Subject: [PATCH] ExecutorMain: Shut down when our parent disappears --- .../merger/worker/executor/ExecutorMain.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/merger/src/main/java/com/metamx/druid/merger/worker/executor/ExecutorMain.java b/merger/src/main/java/com/metamx/druid/merger/worker/executor/ExecutorMain.java index d169701fc6f..1208358b5e3 100644 --- a/merger/src/main/java/com/metamx/druid/merger/worker/executor/ExecutorMain.java +++ b/merger/src/main/java/com/metamx/druid/merger/worker/executor/ExecutorMain.java @@ -19,6 +19,7 @@ package com.metamx.druid.merger.worker.executor; +import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.metamx.common.lifecycle.Lifecycle; import com.metamx.common.logger.Logger; import com.metamx.druid.log.LogLevelAdjuster; @@ -26,6 +27,9 @@ import com.metamx.druid.merger.common.TaskStatus; import com.metamx.druid.merger.common.task.Task; import java.io.File; +import java.io.IOException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; /** */ @@ -55,6 +59,34 @@ public class ExecutorMain System.exit(2); } + // Spawn monitor thread to keep a watch on our parent process + // If stdin reaches eof, the parent is gone, and we should shut down + final ExecutorService parentMonitorExec = Executors.newSingleThreadExecutor( + new ThreadFactoryBuilder() + .setNameFormat("parent-monitor-%d") + .setDaemon(true) + .build() + ); + + parentMonitorExec.submit( + new Runnable() + { + @Override + public void run() + { + int b = -1; + try { + b = System.in.read(); + } catch (Exception e) { + log.error(e, "Failed to read from stdin"); + } + + // TODO ugh this is gross + System.exit(2); + } + } + ); + try { final Task task = node.getJsonMapper().readValue(new File(args[0]), Task.class);