/** * Saves execution in the current thread, then creates a Job and submits that for asynchronous * execution to a specific ExecutorService. * * @param jobExecution the {@link JobExecution} to save and submit. * @param executorService the ExecutorService to run the submitted job on */ public CompletableFuture<Void> submit( JobExecution jobExecution, ExecutorService executorService) { overwriteJobExecutionUser(jobExecution); Job molgenisJob = saveExecutionAndCreateJob(jobExecution); return CompletableFuture.runAsync(() -> runJob(jobExecution, molgenisJob), executorService) .handle( (voidResult, throwable) -> { if (throwable != null) { LOG.error( format( "Job of type '%s' with id '%s' completed with exception", jobExecution.getType(), jobExecution.getIdentifier()), throwable); } return voidResult; }); }
@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(); }
@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(); }