/** * 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; }); }
/** * 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; } }