@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(); }
public Instant getSubmissionDate() { return getInstant(SUBMISSION_DATE); }
@Nullable public Integer getProgressMax() { return getInt(PROGRESS_MAX); }
@Override public void increment(int amount) { jobExecution.setProgressInt(jobExecution.getProgressInt() + amount); update(); }
@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(); }
/** * 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; } }
/** * Appends a log message to the execution log. The first time the log exceeds MAX_LOG_LENGTH, it * gets truncated and the TRUNCATION_BANNER gets added. Subsequent calls to appendLog will be * ignored. * * @param formattedMessage The formatted message to append to the log. */ void appendLog(String formattedMessage) { if (logTruncated) return; String combined = join(getLog(), formattedMessage); if (combined.length() > MAX_LOG_LENGTH) { String truncated = abbreviate(combined, MAX_LOG_LENGTH - TRUNCATION_BANNER.length() * 2 - 2); combined = join(new String[] {TRUNCATION_BANNER, truncated, TRUNCATION_BANNER}, "\n"); logTruncated = true; } setLog(combined); }
/** * 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; }); }
private void sendEmail(String[] to, String subject, String text) { if (to.length > 0) { try { SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setTo(to); mailMessage.setSubject(subject); mailMessage.setText(text); mailSender.send(mailMessage); } catch (RuntimeException e) { jobExecution.setProgressMessage( String.format( "%s (Mail not sent: %s)", jobExecution.getProgressMessage(), e.getMessage())); } } }
@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; } }
@Override protected void append(ILoggingEvent eventObject) { String formattedMessage = layout.doLayout(eventObject); JobExecution jobExecution = JobExecutionHolder.get(); jobExecution.appendLog(formattedMessage); } }
private void tryUpdate(JobExecution jobExecution) { Entity jobExecutionCopy = new DynamicEntity(jobExecution.getEntityType()); jobExecutionCopy.set(jobExecution); try { dataService.update(jobExecutionCopy.getEntityType().getId(), jobExecutionCopy); } catch (Exception ex) { LOG.warn("Error updating job execution", ex); } } }
@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(); }
@Nullable public Instant getStartDate() { return getInstant(START_DATE); }
@Nullable public Integer getProgressInt() { return getInt(PROGRESS_INT); }
@Nullable public Instant getEndDate() { return getInstant(END_DATE); }