When using JobLauncherApplicationRunner, if we pass an arbitrary JobName, the spring application terminates with a success exit code without failure.
If the job name is entered incorrectly by mistake, it could be mistaken for success even though no job was actually executed.
It would be good to add validation whether the job name is defined in application.
sol. 1
public class JobLauncherApplicationRunner implements ApplicationRunner, Ordered, ApplicationEventPublisherAware {
...
@Value("${spring.batch.job.fail-on-unknown-name:false}")
private boolean failOnUnknownName;
@PostConstruct
public void validate() {
if (this.jobs.size() > 1 && !StringUtils.hasText(this.jobName)) {
throw new IllegalArgumentException("Job name must be specified in case of multiple jobs");
}
if (failOnUnknownName == true && this.jobs.stream().noneMatch(job -> StringUtils.equals(this.jobName, job.getName()))) {
throw IllegalArgumentException("No such job name : [$jobName]")
}
}
sol. 2
public class JobExecutionExitCodeGenerator implements ApplicationListener<JobExecutionEvent>, ExitCodeGenerator {
@Override
public int getExitCode() {
if (this.executions.isEmpty()) {
return 127;
}
for (JobExecution execution : this.executions) {
if (execution.getStatus().ordinal() > 0) {
return execution.getStatus().ordinal();
}
}
return 0;
}
Comment From: philwebb
Another option might be to change this line to not always swallow NoSuchJobException.
Comment From: philwebb
@fmbenhassine Do you have any thoughts on this one?
Comment From: fmbenhassine
Hi @philwebb , yes, I would also follow your suggestion of not swallowing the exception.
Comment From: itsAkshayDubey
Hello @philwebb @fmbenhassine,
Raised PR #36039 to fix this, kindly have a look.
Thanks, Akshay
Comment From: scottfrederick
Closing in favor of #36039
Comment From: itsAkshayDubey
Hello @scottfrederick , @philwebb , @fmbenhassine , @mhalbritter
Raised new PR #36060 for this bug, kindly have a look.
Thanks, Akshay
Comment From: umbum
Hi. I'm a little late =)
I think 2.x is getting job.names and 3.x is getting job.name, so the direction of modification should be slightly different.
-
job names In 2.x, which can accept multiple job names, wouldn't it be better to fail the batch application from the beginning if it contains at least one job that doesn't exist? Rather than having some jobs run and fail in the middle.
-
new property And I'm still wondering if this is really a bug. What do you think about making it possible to control failure by adding
spring.batch.job.fail-on-unknown-name:falseproperty? Currently, the default action is success, but if it suddenly changes to failure, I think it could cause confusion when upgrading spring boot version.
@philwebb (or spring team) If you give me your opinion, I'll work on this and submit a PR.
Comment From: wilkinsona
Thanks, @umbum, but @itsAkshayDubey is already working on this and has already submitted a PR. We'd prefer to keep working with them on this.
Comment From: snicoll
Closing in favor of PR #36060