package org.datavault.worker;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecuteResultHandler;
import org.apache.commons.exec.DefaultExecutor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class WorkerManager {

    private String numberOfWorkers;

    public void setNumberOfWorkers(String numberOfWorkers) {
        this.numberOfWorkers = numberOfWorkers;
    }


    public static void main(String [] args) throws IOException, InterruptedException {
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"config.xml"});

        WorkerManager workerManager = context.getBean(WorkerManager.class);
        List<DefaultExecuteResultHandler> resultHandlers = workerManager.startWorkers();
        workerManager.checkResultHandlers(resultHandlers);
    }

    private List<DefaultExecuteResultHandler> startWorkers() throws IOException, InterruptedException {
        List<DefaultExecuteResultHandler> resultHandlers = new ArrayList<>();
        int i = 0;
        while (i < Integer.parseInt(numberOfWorkers)) {
            resultHandlers.add(i, startWorker());
            i++;
        }

        return resultHandlers;
    }

    private DefaultExecuteResultHandler startWorker() throws IOException, InterruptedException {
        CommandLine cmdLine = new CommandLine("java");
        cmdLine.addArgument("-cp");
        cmdLine.addArgument("datavault-worker-1.0-SNAPSHOT-jar-with-dependencies.jar");
        cmdLine.addArgument("org.datavault.worker.WorkerInstance");

        DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();

        DefaultExecutor executor = new DefaultExecutor();
        executor.setExitValue(1);

        executor.execute(cmdLine, resultHandler);

        return resultHandler;
    }

    private void checkResultHandlers(List<DefaultExecuteResultHandler> resultHandlers) throws IOException, InterruptedException {
        while (true) {
            for (DefaultExecuteResultHandler resultHandler : resultHandlers) {
                if (resultHandler.hasResult()) {

                    // todo : what should I usefully do in this event??

                    System.out.println("Exit value " + resultHandler.getExitValue());
                    break;
                }
            }

            Thread.sleep(50L);
        }

    }



}

@@ -2261,17 +2261,16 @@
 lt()) %7B%0A
-%0A
         
@@ -2335,10 +2335,8 @@
 ent?
-?%0A
 %0A   
@@ -2372,17 +2372,43 @@
 rintln(%22
-E
+A handler has failed with e
 xit valu
@@ -2468,13 +2468,14 @@
     
-b
 re
-ak
+turn
 ;%0A  
@@ -2497,32 +2497,99 @@
 %0A            %7D%0A%0A
+            // Sleep for a while before doing another health check%0A
             Thre

Fix infinite loop in WorkerManager