-
Notifications
You must be signed in to change notification settings - Fork 2.4k
-
When launching jobs from a web service, the jobs are queued and run in the instance that started them.
However, web services can be restarted at any time, which will stop the processing of those jobs.
How do you ensure that running jobs are restarted when the service is restarted?
I tried this, based on this SO question, which does "work":
@Component @RequiredArgsConstructor public static class JobStartup { private final JobExplorer jobExplorer; private final JobOperator jobOperator; private final JobRepository jobRepository; @EventListener(ApplicationReadyEvent.class) public void restartIncompleteJobs() throws JobExecutionException { for (String jobName : jobExplorer.getJobNames()) { for (JobExecution execution : jobExplorer.findRunningJobExecutions(jobName)) { execution.getStepExecutions().stream() .filter(step -> step.getStatus().isRunning()) .forEach(step -> { step.setStatus(BatchStatus.STOPPED); jobRepository.update(step); }); execution.setStatus(BatchStatus.STOPPED); jobRepository.update(execution); jobOperator.restart(execution.getId()); } } } }
There are a number of problems with this hack:
- It doesn't work if there are multiple service instances, as a "running" job may be running in a different instance.
- It cannot be made atomic because
restart
runs its own transaction, which will not see the updates made before it. - It just feels wrong having to use three separate APIs to achieve "one" thing.
Is there a better way to ensure jobs continue across service restarts, or anything Spring Batch can add to do this properly?
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment