public JobInstance createJobInstance(String jobName, final JobParameters jobParameters) {
    Assert.notNull(jobName, "Job name must not be null.");
    Assert.notNull(jobParameters, "JobParameters must not be null.");

    Assert.state(
        getJobInstance(jobName, jobParameters) == null, "JobInstance must not already exist");

    Long jobId = getNextId(JobInstance.class.getSimpleName(), mongoTemplate);

    JobInstance jobInstance = new JobInstance(jobId, jobName);

    jobInstance.incrementVersion();

    Map<String, JobParameter> jobParams = jobParameters.getParameters();
    Map<String, Object> paramMap = new HashMap<String, Object>(jobParams.size());
    for (Map.Entry<String, JobParameter> entry : jobParams.entrySet()) {
      paramMap.put(
          entry.getKey().replaceAll(DOT_STRING, DOT_ESCAPE_STRING), entry.getValue().getValue());
    }
    getCollection()
        .save(
            start()
                .add(JOB_INSTANCE_ID_KEY, jobId)
                .add(JOB_NAME_KEY, jobName)
                .add(JOB_KEY_KEY, createJobKey(jobParameters))
                .add(VERSION_KEY, jobInstance.getVersion())
                .add(JOB_PARAMETERS_KEY, new BasicDBObject(paramMap))
                .get());
    return jobInstance;
  }
示例#2
0
 public Date getJobsPreviousRunTime(String jobName) {
   JobExplorer explorer = getBatchJobExplorer();
   List<JobInstance> jobInstances = explorer.getJobInstances(jobName, 0, 1);
   if (jobInstances.size() == 0) {
     return new Date(0);
   }
   JobInstance jobInstance = jobInstances.get(0);
   long executionTimeInMillis =
       jobInstance.getJobParameters().getLong(MifosBatchJob.JOB_EXECUTION_TIME_KEY);
   return new Date(executionTimeInMillis);
 }
  public JobParameters getLastJobParameters(String jobName) throws NoSuchJobException {

    Collection<JobInstance> lastInstances = listJobInstances(jobName, 0, 1);

    JobInstance lastInstance = null;
    if (!lastInstances.isEmpty()) {
      lastInstance = lastInstances.iterator().next();
    }

    JobParameters oldParameters = new JobParameters();
    if (lastInstance != null) {
      oldParameters = lastInstance.getJobParameters();
    }

    return oldParameters;
  }
  public JobExecution restart(Long jobExecutionId)
      throws NoSuchJobExecutionException, JobExecutionAlreadyRunningException, JobRestartException,
          JobInstanceAlreadyCompleteException, NoSuchJobException, JobParametersInvalidException {

    JobExecution target = getJobExecution(jobExecutionId);
    JobInstance lastInstance = target.getJobInstance();

    Job job = jobLocator.getJob(lastInstance.getJobName());

    JobExecution jobExecution = jobLauncher.run(job, lastInstance.getJobParameters());

    if (jobExecution.isRunning()) {
      activeExecutions.add(jobExecution);
    }
    return jobExecution;
  }
示例#5
0
  public Date getJobsLastSuccessfulRunTime(String jobName) {
    JobExplorer explorer = getBatchJobExplorer();
    List<JobInstance> jobInstances = explorer.getJobInstances(jobName, 0, 100);

    for (JobInstance job : jobInstances) {
      for (JobExecution execution : explorer.getJobExecutions(job)) {
        if (BatchStatus.COMPLETED.equals(execution.getStatus())) {
          long executionTimeInMillis =
              job.getJobParameters().getLong(MifosBatchJob.JOB_EXECUTION_TIME_KEY);
          return new Date(executionTimeInMillis);
        }
      }
    }

    return new Date(0);
  }
  @Override
  public List<JobInstance> findJobInstancesByName(String jobName, int start, int count) {
    List<JobInstance> result = new ArrayList<JobInstance>();
    List<JobInstance> jobInstances =
        mapJobInstances(
            getCollection()
                .find(new BasicDBObject(JOB_NAME_KEY, jobName))
                .sort(jobInstanceIdObj(-1L)));
    for (JobInstance instanceEntry : jobInstances) {
      String key = instanceEntry.getJobName();
      String curJobName = key.substring(0, key.lastIndexOf("|"));

      if (curJobName.equals(jobName)) {
        result.add(instanceEntry);
      }
    }
    return result;
  }
  private JobInstance mapJobInstance(DBObject dbObject, JobParameters jobParameters) {
    JobInstance jobInstance = null;
    if (dbObject != null) {
      Long id = (Long) dbObject.get(JOB_INSTANCE_ID_KEY);
      if (jobParameters == null) {
        jobParameters = getJobParameters(id, mongoTemplate);
      }

      jobInstance =
          new JobInstance(
              id,
              (String)
                  dbObject.get(
                      JOB_NAME_KEY)); // should always be at version=0 because they never get
                                      // updated
      jobInstance.incrementVersion();
    }
    return jobInstance;
  }
  @RequestMapping(value = "/jobs/{jobName}", method = RequestMethod.GET)
  public String details(
      ModelMap model,
      @ModelAttribute("jobName") String jobName,
      Errors errors,
      @RequestParam(defaultValue = "0") int startJobInstance,
      @RequestParam(defaultValue = "20") int pageSize) {

    boolean launchable = jobService.isLaunchable(jobName);

    try {

      Collection<JobInstance> result =
          jobService.listJobInstances(jobName, startJobInstance, pageSize);
      Collection<JobInstanceInfo> jobInstances = new ArrayList<JobInstanceInfo>();
      model.addAttribute(
          "jobParameters",
          jobParametersExtractor.fromJobParameters(jobService.getLastJobParameters(jobName)));

      for (JobInstance jobInstance : result) {
        Collection<JobExecution> jobExecutions =
            jobService.getJobExecutionsForJobInstance(jobName, jobInstance.getId());
        jobInstances.add(new JobInstanceInfo(jobInstance, jobExecutions, timeZone));
      }

      model.addAttribute("jobInstances", jobInstances);
      int total = jobService.countJobInstances(jobName);
      TableUtils.addPagination(model, total, startJobInstance, pageSize, "JobInstance");
      int count = jobService.countJobExecutionsForJob(jobName);
      model.addAttribute(
          "jobInfo", new JobInfo(jobName, count, launchable, jobService.isIncrementable(jobName)));

    } catch (NoSuchJobException e) {
      errors.reject(
          "no.such.job",
          new Object[] {jobName},
          "There is no such job (" + HtmlUtils.htmlEscape(jobName) + ")");
    }

    return "jobs/job";
  }
  public JobExecutionInfo(JobExecution jobExecution, TimeZone timeZone) {

    // this.jobExecution = jobExecution;
    this.exitStatus = jobExecution.getExitStatus();
    this.timeZone = timeZone;
    this.id = jobExecution.getId();
    this.jobId = jobExecution.getJobId();
    this.stepExecutionCount = jobExecution.getStepExecutions().size();
    this.jobParameters = converter.getProperties(jobExecution.getJobParameters());
    this.jobParametersString =
        new JobParametersExtractor().fromJobParameters(jobExecution.getJobParameters());

    JobInstance jobInstance = jobExecution.getJobInstance();
    if (jobInstance != null) {
      this.jobName = jobInstance.getJobName();
      BatchStatus status = jobExecution.getStatus();
      this.restartable =
          status.isGreaterThan(BatchStatus.STOPPING) && status.isLessThan(BatchStatus.ABANDONED);
      this.abandonable =
          status.isGreaterThan(BatchStatus.STARTED) && status != BatchStatus.ABANDONED;
      this.stoppable = status.isLessThan(BatchStatus.STOPPING);
    } else {
      this.jobName = "?";
    }

    // Duration is always in GMT
    durationFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    // The others can be localized
    timeFormat.setTimeZone(timeZone);
    dateFormat.setTimeZone(timeZone);
    if (jobExecution.getStartTime() != null) {
      this.startDate = dateFormat.format(jobExecution.getStartTime());
      this.startTime = timeFormat.format(jobExecution.getStartTime());
      Date endTime = jobExecution.getEndTime() != null ? jobExecution.getEndTime() : new Date();
      this.duration =
          durationFormat.format(
              new Date(endTime.getTime() - jobExecution.getStartTime().getTime()));
    }
  }
  @Override
  public int getJobInstanceCount(String jobName) throws NoSuchJobException {

    int count = 0;
    List<JobInstance> jobInstances =
        mapJobInstances(
            getCollection()
                .find(new BasicDBObject(JOB_NAME_KEY, jobName))
                .sort(jobInstanceIdObj(-1L)));
    for (JobInstance instanceEntry : jobInstances) {
      String key = instanceEntry.getJobName();
      String curJobName = key.substring(0, key.lastIndexOf("|"));

      if (curJobName.equals(jobName)) {
        count++;
      }
    }

    if (count == 0) {
      throw new NoSuchJobException("No job instances for job name " + jobName + " were found");
    } else {
      return count;
    }
  }
示例#11
0
  @Test
  public void testGetInstanceId() {
    when(instance.getId()).thenReturn(5L);

    assertEquals(5L, context.getInstanceId());
  }
示例#12
0
  @Test
  public void testGetJobName() {
    when(instance.getJobName()).thenReturn("jobName");

    assertEquals("jobName", context.getJobName());
  }