Skip to content

Commit

Permalink
[JENKINS-45471] - Generalize handling of scheduling errors and preven…
Browse files Browse the repository at this point in the history
…t NPEs in perform3() (#115)

* [JENKINS-45471] - Generalize handling of scheduling errors and prevent NPEs in perform3()

* [JENKINS-45471] - Use Job/Build according to the comment from @jglick
  • Loading branch information
oleg-nenashev committed Jul 12, 2017
1 parent 59e30a2 commit 2c7e3c1
Showing 1 changed file with 21 additions and 4 deletions.
Expand Up @@ -401,7 +401,7 @@ public List<Future<AbstractBuild>> perform(AbstractBuild<?, ?> build, Launcher l
if (future != null) {
futures.add(future);
} else {
LOGGER.log(Level.FINE, "The schedule for project {0} and build {1} failed due either security reasons or the trigger is not compatible with un-parameterized jobs", new Object[]{project.getFullName(), build.number});
reportSchedulingError(build, project, listener);
}
}
}
Expand All @@ -412,9 +412,20 @@ public List<Future<AbstractBuild>> perform(AbstractBuild<?, ?> build, Launcher l
// don't trigger on this configuration
}
return Collections.emptyList();
}

}

private void reportSchedulingError(@Nonnull Run<?, ?> run, @Nonnull Job<?, ?> jobToTrigger, @Nonnull BuildListener listener) {
// Do not print details to Build Listener, they have been reported previously in #canTriggerProject()
listener.error("Skipping " + jobToTrigger.getFullName() + "...");
if (LOGGER.isLoggable(Level.CONFIG)) {
String message = String.format("Cannot schedule project %s. Job type is not parameterized, "
+ "or there is no Job/Build permission for the current authentication %s. "
+ "Skipping...", jobToTrigger, Jenkins.getAuthentication().getName());
LOGGER.log(Level.CONFIG, String.format("%s: %s", run, message),
new UnsupportedOperationException("Cannot schedule job " + jobToTrigger.getFullName()));
}
}

/**
* @deprecated
* Use {@link #perform3(AbstractBuild, Launcher, BuildListener)}
Expand Down Expand Up @@ -449,7 +460,12 @@ public ListMultimap<Job, Future<Run>> perform3(AbstractBuild<?, ?> build, Launch
for (Job project : getJobs(build.getRootBuild().getProject().getParent(), env)) {
List<Action> list = getBuildActions(actions, project);

futures.put(project, schedule(build, project, list, listener));
final Future scheduled = schedule(build, project, list, listener);
if (scheduled != null) {
futures.put(project, scheduled);
} else {
reportSchedulingError(build, project, listener);
}
}
}
return futures;
Expand Down Expand Up @@ -603,6 +619,7 @@ protected Future schedule(AbstractBuild<?, ?> build, Job project, List<Action> l
return schedule(build, project, list, TaskListener.NULL);
}

@CheckForNull
protected Future schedule(@Nonnull AbstractBuild<?, ?> build, @Nonnull Job project, @Nonnull List<Action> list, @Nonnull TaskListener listener) throws InterruptedException, IOException {
if (project instanceof ParameterizedJobMixIn.ParameterizedJob) {
return schedule(build, project, ((ParameterizedJobMixIn.ParameterizedJob) project).getQuietPeriod(), list, listener);
Expand Down

0 comments on commit 2c7e3c1

Please sign in to comment.