use of org.jvnet.hudson.reactor.Task in project hudson-2.x by hudson.
the class Hudson method executeReactor.
/**
* Executes a reactor.
*
* @param is
* If non-null, this can be consulted for ignoring some tasks. Only used during the initialization of Hudson.
*/
private void executeReactor(final InitStrategy is, TaskBuilder... builders) throws IOException, InterruptedException, ReactorException {
Reactor reactor = new Reactor(builders) {
/**
* Sets the thread name to the task for better diagnostics.
*/
@Override
protected void runTask(Task task) throws Exception {
if (is != null && is.skipInitTask(task)) {
return;
}
// full access in the initialization thread
SecurityContextHolder.getContext().setAuthentication(ACL.SYSTEM);
String taskName = task.getDisplayName();
Thread t = Thread.currentThread();
String name = t.getName();
if (taskName != null) {
t.setName(taskName);
}
try {
long start = System.currentTimeMillis();
super.runTask(task);
if (LOG_STARTUP_PERFORMANCE) {
LOGGER.info(String.format("Took %dms for %s by %s", System.currentTimeMillis() - start, taskName, name));
}
} finally {
t.setName(name);
SecurityContextHolder.clearContext();
}
}
};
ExecutorService es;
if (PARALLEL_LOAD) {
es = new ThreadPoolExecutor(TWICE_CPU_NUM, TWICE_CPU_NUM, 5L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new DaemonThreadFactory());
} else {
es = Executors.newSingleThreadExecutor(new DaemonThreadFactory());
}
try {
reactor.execute(es, buildReactorListener());
} finally {
// upon a successful return the executor queue should be empty. Upon an exception, we want to cancel all pending tasks
es.shutdownNow();
}
}
use of org.jvnet.hudson.reactor.Task in project hudson-2.x by hudson.
the class Hudson method buildReactorListener.
/**
* Aggregates all the listeners into one and returns it.
*
* <p>
* At this point plugins are not loaded yet, so we fall back to the META-INF/services look up to discover implementations.
* As such there's no way for plugins to participate into this process.
*/
private ReactorListener buildReactorListener() throws IOException {
List<ReactorListener> r = (List) Service.loadInstances(Thread.currentThread().getContextClassLoader(), InitReactorListener.class);
r.add(new ReactorListener() {
final Level level = Level.parse(System.getProperty(Hudson.class.getName() + ".initLogLevel", "FINE"));
public void onTaskStarted(Task t) {
LOGGER.log(level, "Started " + t.getDisplayName());
}
public void onTaskCompleted(Task t) {
LOGGER.log(level, "Completed " + t.getDisplayName());
}
public void onTaskFailed(Task t, Throwable err, boolean fatal) {
LOGGER.log(Level.SEVERE, "Failed " + t.getDisplayName(), err);
}
public void onAttained(Milestone milestone) {
Level lv = level;
String s = "Attained " + milestone.toString();
if (milestone instanceof InitMilestone) {
// noteworthy milestones --- at least while we debug problems further
lv = Level.INFO;
initLevel = (InitMilestone) milestone;
s = initLevel.toString();
}
LOGGER.log(lv, s);
}
});
return new ReactorListener.Aggregator(r);
}
use of org.jvnet.hudson.reactor.Task in project hudson-2.x by hudson.
the class InitializerFinder method discoverTasks.
public Collection<Task> discoverTasks(Reactor session) throws IOException {
List<Task> result = new ArrayList<Task>();
for (Method e : Index.list(Initializer.class, cl, Method.class)) {
if (!discovered.add(e))
// already reported once
continue;
if (!Modifier.isStatic(e.getModifiers()))
throw new IOException(e + " is not a static method");
Initializer i = e.getAnnotation(Initializer.class);
// stale index
if (i == null)
continue;
result.add(new TaskImpl(i, e));
}
return result;
}
Aggregations