/**
   * Cancel a job
   *
   * @return
   * @throws IOException
   */
  @RequestMapping(
      value = "/{jobId}/cancel",
      method = {RequestMethod.PUT})
  @ResponseBody
  public JobInstance cancel(@PathVariable String jobId) {

    try {
      final JobInstance jobInstance = jobService.getJobInstance(jobId);
      return jobService.cancelJob(jobInstance);
    } catch (Exception e) {
      logger.error(e.getLocalizedMessage(), e);
      throw new InternalErrorException(e);
    }
  }
  /*
   * (non-Javadoc)
   *
   * @see
   * org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
   */
  @Override
  public void afterPropertiesSet() throws Exception {
    String timeZone = jobService.getKylinConfig().getTimeZone();
    TimeZone tzone = TimeZone.getTimeZone(timeZone);
    TimeZone.setDefault(tzone);

    final KylinConfig kylinConfig = KylinConfig.getInstanceFromEnv();
    String serverMode = kylinConfig.getServerMode();

    if (Constant.SERVER_MODE_JOB.equals(serverMode.toLowerCase())
        || Constant.SERVER_MODE_ALL.equals(serverMode.toLowerCase())) {
      logger.info("Initializing Job Engine ....");

      new Thread(
              new Runnable() {
                @Override
                public void run() {
                  try {
                    DefaultScheduler scheduler = DefaultScheduler.getInstance();
                    scheduler.init(new JobEngineConfig(kylinConfig), new ZookeeperJobLock());
                    if (!scheduler.hasStarted()) {
                      logger.error("scheduler has not been started");
                      System.exit(1);
                    }
                  } catch (Exception e) {
                    throw new RuntimeException(e);
                  }
                }
              })
          .start();
    }
  }
  /**
   * get all cube jobs
   *
   * @return
   * @throws IOException
   */
  @RequestMapping(
      value = "",
      method = {RequestMethod.GET})
  @ResponseBody
  public List<JobInstance> list(JobListRequest jobRequest) {

    List<JobInstance> jobInstanceList = Collections.emptyList();
    List<JobStatusEnum> statusList = new ArrayList<JobStatusEnum>();

    if (null != jobRequest.getStatus()) {
      for (int status : jobRequest.getStatus()) {
        statusList.add(JobStatusEnum.getByCode(status));
      }
    }

    try {
      jobInstanceList =
          jobService.listAllJobs(
              jobRequest.getCubeName(),
              jobRequest.getProjectName(),
              statusList,
              jobRequest.getLimit(),
              jobRequest.getOffset());
    } catch (Exception e) {
      logger.error(e.getLocalizedMessage(), e);
      throw new InternalErrorException(e);
    }
    return jobInstanceList;
  }
 /**
  * Get a job step output
  *
  * @return
  * @throws IOException
  */
 @RequestMapping(
     value = "/{jobId}/steps/{stepId}/output",
     method = {RequestMethod.GET})
 @ResponseBody
 public Map<String, String> getStepOutput(
     @PathVariable String jobId, @PathVariable String stepId) {
   Map<String, String> result = new HashMap<String, String>();
   result.put("jobId", jobId);
   result.put("stepId", String.valueOf(stepId));
   result.put("cmd_output", jobService.getExecutableManager().getOutput(stepId).getVerboseMsg());
   return result;
 }
  /**
   * Get a cube job
   *
   * @return
   * @throws IOException
   */
  @RequestMapping(
      value = "/{jobId}",
      method = {RequestMethod.GET})
  @ResponseBody
  public JobInstance get(@PathVariable String jobId) {
    JobInstance jobInstance = null;
    try {
      jobInstance = jobService.getJobInstance(jobId);
    } catch (Exception e) {
      logger.error(e.getLocalizedMessage(), e);
      throw new InternalErrorException(e);
    }

    return jobInstance;
  }