Package gri.tasks.managers

Source Code of gri.tasks.managers.JobManagerTask$PauseController

package gri.tasks.managers;

import gri.tasks.Task;
import gri.tasks.TaskDef;
import gri.tasks.AsyncTaskController;

import java.util.Map;

/**
* Synchronous Task object which allows us to run an asynchronous job through a
* JobManager.
*
* @author rogersda
*/
public class JobManagerTask implements Task {

  JobManager jobManager;
  String taskId;
  boolean freeJob = true;
 
  // -------------------------------------------------------- Constructors
 
  public JobManagerTask(JobManager jobManager, String taskId) {
    this.jobManager = jobManager;
    this.taskId = taskId;
  }
 
  // ------------------------------------------------------ Implementation
 
  public TaskDef getTaskDef() {
    return jobManager.getTaskDefinition(taskId);
  }
 
  public Map execute(Map inputs) throws Exception {
    JobSubmission submission = new JobSubmission(taskId, inputs);
    String jobId = jobManager.submitJob(submission);
   
    AsyncTaskController controller = jobManager.getJobController(jobId);
    PauseController pauser = new ConstantPause(3000);
    while (!controller.isComplete()) {
      try {
        pauser.pause();
      }
      catch(InterruptedException e) {
        throw e;  //why not?
      }
    }
   
    Map outputs = controller.getOutputs();
   
    if (freeJob)
      controller.free();
   
    return outputs;
  }
 
  // -------------------------------------------------------- Inner Classes
 
  /**
   * Interface that allows us to pause execution for a given amount
   * of time.  The Controller itself will determine how long each
   * pause is so that successive pauses may be longer.
   */
  static interface PauseController {
    public void pause() throws InterruptedException;
  }
 
  static class ConstantPause implements PauseController {
   
    long ms;
   
    public ConstantPause(long ms) {
      this.ms = ms;
    }
   
    public void pause() throws InterruptedException {
      Thread.sleep(ms);
    }
   
  }
 
  static class DecayPause implements PauseController {
   
    long pause0;
    long pause1;
    int steps;
   
    int nextStep;
   
    public DecayPause(long pause0, long pause1, int steps) {
      this.pause0 = pause0;
      this.pause1 = pause1;
      this.steps = steps;
           
      this.nextStep = 0;
    }
    public DecayPause(long pause0, long pause1) {
      this(pause0, pause1, 20);
    }
   
    public void pause() throws InterruptedException {
      long ms = (pause1 - pause0) * nextStep / steps;
     
      Thread.sleep(ms);
     
      nextStep++;
    }
   
  }
 
}
TOP

Related Classes of gri.tasks.managers.JobManagerTask$PauseController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.