@Override public void start() { JobExecutionHolder.set(jobExecution); JOB_EXECUTION_LOG.info("Execution started."); jobExecution.setStartDate(Instant.now()); jobExecution.setStatus(RUNNING); update(); }
@Override public void failed(String message, @Nullable Throwable throwable) { JOB_EXECUTION_LOG.error("Failed. " + message, throwable); jobExecution.setEndDate(Instant.now()); jobExecution.setStatus(FAILED); jobExecution.setProgressMessage(message); sendEmail( jobExecution.getFailureEmail(), jobExecution.getType() + " job failed.", jobExecution.getLog()); update(); JobExecutionHolder.unset(); }
@Override public void canceled() { JOB_EXECUTION_LOG.warn("Canceled"); jobExecution.setEndDate(Instant.now()); jobExecution.setStatus(CANCELED); sendEmail( jobExecution.getFailureEmail(), jobExecution.getType() + " job failed.", jobExecution.getLog()); update(); JobExecutionHolder.unset(); }
@SuppressWarnings("unchecked") private Job saveExecutionAndCreateJob(JobExecution jobExecution) { String entityTypeId = jobExecution.getEntityType().getId(); dataService.add(entityTypeId, jobExecution); try { JobFactory jobFactory = jobFactoryRegistry.getJobFactory(jobExecution); return jobFactory.createJob(jobExecution); } catch (RuntimeException ex) { LOG.error("Error creating job for JobExecution.", ex); jobExecution.setStatus(JobExecution.Status.FAILED); dataService.update(entityTypeId, jobExecution); throw ex; } }
/** * Executes a {@link ScheduledJob} in the current thread. * * @param scheduledJobId ID of the {@link ScheduledJob} to run */ @RunAsSystem public void executeScheduledJob(String scheduledJobId) { ScheduledJob scheduledJob = dataService.findOneById(SCHEDULED_JOB, scheduledJobId, ScheduledJob.class); if (scheduledJob == null) { throw new UnknownEntityException(SCHEDULED_JOB, scheduledJobId); } JobExecution jobExecution = createJobExecution(scheduledJob); Job molgenisJob = saveExecutionAndCreateJob(jobExecution); try { runJob(jobExecution, molgenisJob); } catch (Exception ex) { LOG.error("Error creating job for JobExecution.", ex); jobExecution.setStatus(JobExecution.Status.FAILED); jobExecution.setProgressMessage(ex.getMessage()); dataService.update(jobExecution.getEntityType().getId(), jobExecution); throw ex; } }
@Override public void success() { jobExecution.setEndDate(Instant.now()); jobExecution.setStatus(SUCCESS); jobExecution.setProgressInt(jobExecution.getProgressMax()); Duration yourDuration = Duration.millis(timeRunning()); Period period = yourDuration.toPeriod(); PeriodFormatter periodFormatter = new PeriodFormatterBuilder() .appendDays() .appendSuffix("d ") .appendHours() .appendSuffix("h ") .appendMinutes() .appendSuffix("m ") .appendSeconds() .appendSuffix("s ") .appendMillis() .appendSuffix("ms ") .toFormatter(); String timeSpent = periodFormatter.print(period); JOB_EXECUTION_LOG.info("Execution successful. Time spent: {}", timeSpent); sendEmail( jobExecution.getSuccessEmail(), jobExecution.getType() + " job succeeded.", jobExecution.getLog()); update(); JobExecutionHolder.unset(); }