Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How do you deal with service restarts? #4934

Unanswered
OrangeDog asked this question in Q&A
Discussion options

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:

  1. It doesn't work if there are multiple service instances, as a "running" job may be running in a different instance.
  2. It cannot be made atomic because restart runs its own transaction, which will not see the updates made before it.
  3. 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?

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /