1111import hudson .model .Item ;
1212import hudson .model .Job ;
1313import hudson .model .Project ;
14+ import hudson .triggers .SCMTrigger ;
1415import hudson .triggers .Trigger ;
1516import hudson .triggers .TriggerDescriptor ;
1617import hudson .util .FormValidation ;
18+ import hudson .util .NamingThreadFactory ;
1719import hudson .util .SequentialExecutionQueue ;
1820import hudson .util .StreamTaskListener ;
21+ import java .lang .reflect .Field ;
22+ import java .util .concurrent .Executors ;
23+ import java .util .concurrent .ThreadFactory ;
1924import jenkins .model .Jenkins ;
20- import jenkins .model .Jenkins .MasterComputer ;
2125import jenkins .model .ParameterizedJobMixIn ;
2226import jenkins .triggers .SCMTriggerItem .SCMTriggerItems ;
2327import org .apache .commons .jelly .XMLOutput ;
4549import java .util .Set ;
4650
4751import static org .apache .commons .lang3 .StringUtils .isEmpty ;
48- import static org .jenkinsci .plugins .github .Messages .github_trigger_check_method_warning_details ;
4952import static org .jenkinsci .plugins .github .util .JobInfoHelpers .asParameterizedJobMixIn ;
5053
5154/**
@@ -72,7 +75,9 @@ public void onPost() {
7275 */
7376 public void onPost (String triggeredByUser ) {
7477 final String pushBy = triggeredByUser ;
75- getDescriptor ().queue .execute (new Runnable () {
78+ DescriptorImpl d = getDescriptor ();
79+ d .checkThreadPoolSize ();
80+ d .queue .execute (new Runnable () {
7681 private boolean runPolling () {
7782 try {
7883 StreamTaskListener listener = new StreamTaskListener (getLogFile ());
@@ -226,7 +231,7 @@ public void writeLogTo(XMLOutput out) throws IOException {
226231 @ Extension
227232 public static class DescriptorImpl extends TriggerDescriptor {
228233 private final transient SequentialExecutionQueue queue =
229- new SequentialExecutionQueue (MasterComputer . threadPoolForRemoting );
234+ new SequentialExecutionQueue (Executors . newSingleThreadExecutor ( threadFactory ()) );
230235
231236 private transient String hookUrl ;
232237
@@ -235,6 +240,43 @@ public static class DescriptorImpl extends TriggerDescriptor {
235240 @ Inject
236241 private transient GitHubHookRegisterProblemMonitor monitor ;
237242
243+ @ Inject
244+ private transient SCMTrigger .DescriptorImpl scmTrigger ;
245+
246+ private transient int maximumThreads = Integer .MIN_VALUE ;
247+
248+ private static ThreadFactory threadFactory () {
249+ return new NamingThreadFactory (Executors .defaultThreadFactory (), "GitHubPushTrigger" );
250+ }
251+
252+ public DescriptorImpl () {
253+ checkThreadPoolSize ();
254+ }
255+
256+ /**
257+ * Update the {@link java.util.concurrent.ExecutorService} instance.
258+ */
259+ /*package*/
260+ synchronized void checkThreadPoolSize () {
261+ if (scmTrigger != null ) {
262+ int count = scmTrigger .getPollingThreadCount ();
263+ if (maximumThreads != count ) {
264+ maximumThreads = count ;
265+ try {
266+ Field getQueue = SCMTrigger .DescriptorImpl .class .getDeclaredField ("queue" );
267+ getQueue .setAccessible (true );
268+ SequentialExecutionQueue q = (SequentialExecutionQueue ) getQueue .get (scmTrigger );
269+ this .queue .setExecutors (q .getExecutors ());
270+ } catch (NoSuchFieldException | IllegalAccessException | ClassCastException e ) {
271+ queue .setExecutors (
272+ (count == 0
273+ ? Executors .newCachedThreadPool (threadFactory ())
274+ : Executors .newFixedThreadPool (maximumThreads , threadFactory ())));
275+ }
276+ }
277+ }
278+ }
279+
238280 @ Override
239281 public boolean isApplicable (Item item ) {
240282 return item instanceof Job && SCMTriggerItems .asSCMTriggerItem (item ) != null
@@ -351,7 +393,7 @@ public FormValidation doCheckHookRegistered(@AncestorInPath Job<?, ?> job) {
351393 for (GitHubRepositoryName repo : repos ) {
352394 if (monitor .isProblemWith (repo )) {
353395 return FormValidation .warning (
354- github_trigger_check_method_warning_details (
396+ org . jenkinsci . plugins . github . Messages . github_trigger_check_method_warning_details (
355397 repo .getUserName (), repo .getRepositoryName (), repo .getHost ()
356398 ));
357399 }
0 commit comments