File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\jenkins\InitReactorRunner.java.html

1 package jenkins;
2
3 import hudson.init.InitMilestone;
4 import hudson.init.InitReactorListener;
5 import hudson.util.DaemonThreadFactory;
6 import hudson.util.Service;
7 import jenkins.model.Configuration;
8 import jenkins.model.Jenkins;
9 import org.jvnet.hudson.reactor.Milestone;
10 import org.jvnet.hudson.reactor.Reactor;
11 import org.jvnet.hudson.reactor.ReactorException;
12 import org.jvnet.hudson.reactor.ReactorListener;
13 import org.jvnet.hudson.reactor.Task;
14
15 import java.io.IOException;
16 import java.util.List;
17 import java.util.concurrent.ExecutorService;
18 import java.util.concurrent.Executors;
19 import java.util.concurrent.LinkedBlockingQueue;
20 import java.util.concurrent.ThreadPoolExecutor;
21 import java.util.concurrent.TimeUnit;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24
25 import static java.util.logging.Level.SEVERE;
26
27 /**
28 * Executes the {@link Reactor} for the purpose of bootup.
29 *
30 * @author Kohsuke Kawaguchi
31 */
32 public class InitReactorRunner {
33 public void run(Reactor reactor) throws InterruptedException, ReactorException, IOException {
34 reactor.addAll(InitMilestone.ordering().discoverTasks(reactor));
35
36 ExecutorService es;
37 if (Jenkins.PARALLEL_LOAD)
38 es = new ThreadPoolExecutor(
39 TWICE_CPU_NUM, TWICE_CPU_NUM, 5L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new DaemonThreadFactory());
40 else
41 es = Executors.newSingleThreadExecutor(new DaemonThreadFactory());
42 try {
43 reactor.execute(es,buildReactorListener());
44 } finally {
45 es.shutdownNow(); // upon a successful return the executor queue should be empty. Upon an exception, we want to cancel all pending tasks
46 }
47
48 }
49
50 /**
51 * Aggregates all the listeners into one and returns it.
52 *
53 * <p>
54 * At this point plugins are not loaded yet, so we fall back to the META-INF/services look up to discover implementations.
55 * As such there's no way for plugins to participate into this process.
56 */
57 private ReactorListener buildReactorListener() throws IOException {
58 List<ReactorListener> r = (List) Service.loadInstances(Thread.currentThread().getContextClassLoader(), InitReactorListener.class);
59 r.add(new ReactorListener() {
60 final Level level = Level.parse( Configuration.getStringConfigParameter("initLogLevel", "FINE") );
61 public void onTaskStarted(Task t) {
62 LOGGER.log(level,"Started "+t.getDisplayName());
63 }
64
65 public void onTaskCompleted(Task t) {
66 LOGGER.log(level,"Completed "+t.getDisplayName());
67 }
68
69 public void onTaskFailed(Task t, Throwable err, boolean fatal) {
70 LOGGER.log(SEVERE, "Failed "+t.getDisplayName(),err);
71 }
72
73 public void onAttained(Milestone milestone) {
74 Level lv = level;
75 String s = "Attained "+milestone.toString();
76 if (milestone instanceof InitMilestone) {
77 lv = Level.INFO; // noteworthy milestones --- at least while we debug problems further
78 onInitMilestoneAttained((InitMilestone) milestone);
79 s = milestone.toString();
80 }
81 LOGGER.log(lv,s);
82 }
83 });
84 return new ReactorListener.Aggregator(r);
85 }
86
87 /**
88 * Called when the init milestone is attained.
89 */
90 protected void onInitMilestoneAttained(InitMilestone milestone) {
91 }
92
93 private static final int TWICE_CPU_NUM = Integer.getInteger(
94 InitReactorRunner.class.getName()+".concurrency",
95 Runtime.getRuntime().availableProcessors() * 2);
96
97 private static final Logger LOGGER = Logger.getLogger(InitReactorRunner.class.getName());
98 }
99