package gaffer.process;

import gaffer.procfile.Procfile;
import gaffer.procfile.ProcfileEntry;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;

public class ProcessManager {
  private static final class ShutdownHook extends Thread {
    private final ActorSystem system;
    private final ActorRef process;
    private final CountDownLatch latch;

    private ShutdownHook(ActorSystem system, ActorRef processManager, CountDownLatch latch) {
      this.system = system;
      this.process = processManager;
      this.latch = latch;
    }

    @Override
    public void run() {
      if (system.isTerminated()) {
        return;
      }

      process.tell(Signal.TERM, process);
      try {
        latch.await(5, TimeUnit.SECONDS);
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }

  public static final String GAFFER_LOGGER = "gaffer";
  private static final Logger LOGGER = LoggerFactory.getLogger(GAFFER_LOGGER);

  private Procfile procfile;

  public ProcessManager(Procfile procfile) {
    this.procfile = procfile;
  }

  public void start(String dir) {
    ProcfileEntry[] entries = procfile.getEntries();
    List<Process> processes = new ArrayList<Process>(entries.length);
    for (int i = 0; i < entries.length; i++) {
      ProcfileEntry entry = entries[i];
      processes.add(new Process(dir, entry.getName(), entry.getCommand()));
    }

    CountDownLatch latch = new CountDownLatch(1);
    ActorSystem system = ActorSystem.create("start");
    ActorRef processManager =
        system.actorOf(Props.create(ProcessManagerActor.class, processes, LOGGER),
            ProcessManagerActor.class.getName());
    system.actorOf(Props.create(ProcessTerminatorActor.class, processManager, latch),
        ProcessTerminatorActor.class.getName());
    Runtime.getRuntime().addShutdownHook(new ShutdownHook(system, processManager, latch));
  }
}

@@ -528,18 +528,25 @@
  process
+Manager
 ;%0A
-
     priv
@@ -718,16 +718,23 @@
 .process
+Manager
  = proce
@@ -888,16 +888,23 @@
  process
+Manager
 .tell(Si
@@ -921,16 +921,23 @@
  process
+Manager
 );%0A     
@@ -1038,69 +1038,36 @@
     
-// TODO Auto-generated catch block%0A        e.printStackTrace(
+throw new RuntimeException(e
 );%0A 

Throw runtime exception in shutdown hook