package ai.h2o.automl;

import jsr166y.CountedCompleter;
import water.H2O;
import water.Job;
import water.Key;
import water.util.Log;

public class TimedH2OJob extends H2OJob {

  public TimedH2OJob(TimedH2ORunnable runnable) {
    super(runnable);
  }
  public TimedH2OJob(TimedH2ORunnable runnable, Key key) {
    super(runnable, key);
  }

  public Job start() {
    Job j = super.start();
    startTimer();
    return j;
  }

  private void startTimer() { H2O.submitTask(new Timer(this)); }
  private class Timer extends H2O.H2OCountedCompleter<Timer> {
    final TimedH2OJob _j;
    Timer(TimedH2OJob j) {_j=j;}
    @Override public void compute2() {
      while( ((TimedH2ORunnable)_j._target).keepRunning()) {
        try {
          Thread.sleep(Math.min(10,((TimedH2ORunnable)_j._target).timeRemaining()));
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      tryComplete();
    }
    @Override public void onCompletion(CountedCompleter cc) {
      Log.info("Stopping Timed H2OJob");
      _j._target.stop();
      _j._jobKey.get().stop();
    }
  }
}

@@ -1066,31 +1066,322 @@
     
-_j._jobKey.get().stop()
+Job j = _j._jobKey.get();%0A      j.update(1);%0A//      j.stop();%0A    %7D%0A    @Override public boolean onExceptionalCompletion(Throwable t, CountedCompleter cc) %7B%0A      Log.info(%22Timed H2OJob finished exceptionally%22);%0A      _j._target.stop();%0A      _j._jobKey.get().stop();%0A      t.printStackTrace();%0A      return false
 ;%0A  

set the automl job to 100% when finished