@Override
    public void handle(HttpExchange arg0) throws IOException {
      try {
        JSONObject json =
            (JSONObject) JSONSerializer.toJSON(IOUtils.toString(arg0.getRequestBody()));

        JSONObject result = new JSONObject();

        String email = json.getString("email");
        String key = json.getString("key");

        for (int i = 0; i < jobs.size(); i++) {

          TnrsJob job = jobs.get(i);

          if (job.getRequest().getEmail().equals(email) && job.getRequest().getId().equals(key)) {
            if (job.status().equals("failed") || job.status().equals("error")) {
              result.put("type", "failed");
            } else {
              result.put("type", "incomplete");
              double progress = job.progress();
              if (job.progress() == 100.0) {
                progress = 99.0;
              }
              result.put("progress", progress);
            }
            HandlerHelper.writeResponseRequest(arg0, 200, result.toString(), "application/json");

            return;
          }
        }

        String filename = baseFolder + email.replace("@", "-").replace(".", "-") + "/result" + key;

        File results = new File(filename);

        if (!results.exists()) {
          result.put("type", "non-existent");
        } else {
          result.put("type", "complete");
          TnrsJob job = JobHelper.readJobInfo(baseFolder, email, key);

          result.put("job_type", job.getTypeString());
        }

        HandlerHelper.writeResponseRequest(arg0, 200, result.toString(), "application/json");

        return;
      } catch (Exception ex) {
        log.error(ExceptionUtils.getFullStackTrace(ex));
        throw new IOException(ex);
      }
    }
  /**
   * This main loop iterates over the submitted jobs and creates the corresponding threads that will
   * carry the execution of each job segment.
   */
  @Override
  public void run() {
    try {
      int jobno = 0;

      while (true) {

        sleep(20);
        synchronized (jobs) {
          if (jobs.size() == 0) {

            continue;
          }
        }

        TnrsJob job = jobs.get(jobno % jobs.size());
        if (job.status().equalsIgnoreCase("idle")) {
          ExecutionThread thread = new ExecutionThread(job);
          threads.add(thread);
          thread.start();
        } else if (job.status().equalsIgnoreCase("stopped")) {
          jobs.remove(job);
          JobHelper.cleanJobData(baseFolder, job);
          jobno = 0;
          continue;
        } else if (job.status().equals("failed")) {
          sendFailedJobEmail(job);
          job.setStatus("error");
        }
        jobno++;

        if (jobs.size() == 200) {

          for (int i = 0; i < threads.size(); i++) {
            threads.elementAt(i).join();
          }
          jobno = 0;
          threads.clear();
        }

        sleep(10);
      }

    } catch (Exception e) {
      log.error(ExceptionUtils.getFullStackTrace(e));
      e.printStackTrace();
    }
  }
    @Override
    public void handle(HttpExchange arg0) throws IOException {
      try {
        String message =
            "<html><body><table border=\"1\"><tr><th>Email</th><th>Submitted at:</th><th>Progress:</th><th>Status:</th><th>Job id</th></tr>";
        for (int i = 0; i < jobs.size(); i++) {
          TnrsJob job = jobs.get(i);

          message +=
              "<tr> <td> "
                  + job.getRequest().getEmail()
                  + "</td><td>"
                  + job.getSubmissionDate()
                  + " </td><td> "
                  + job.progress()
                  + "% </td><td>  "
                  + job.status()
                  + "</td><td>"
                  + job.getRequest().getId()
                  + "</td></tr>\n";
        }
        message += "</table>";
        HandlerHelper.writeResponseRequest(arg0, 200, message, "text/html");

      } catch (Exception ex) {
        log.error(ExceptionUtils.getFullStackTrace(ex));
        throw new IOException(ex);
      }
    }