Search in sources :

Example 1 with Task

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();
    }
}
Also used : Task(org.jvnet.hudson.reactor.Task) DaemonThreadFactory(hudson.util.DaemonThreadFactory) ExecutorService(java.util.concurrent.ExecutorService) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Reactor(org.jvnet.hudson.reactor.Reactor) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) UDPBroadcastThread(hudson.UDPBroadcastThread)

Example 2 with Task

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);
}
Also used : Task(org.jvnet.hudson.reactor.Task) Milestone(org.jvnet.hudson.reactor.Milestone) InitMilestone(hudson.init.InitMilestone) InitReactorListener(hudson.init.InitReactorListener) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) CopyOnWriteList(hudson.util.CopyOnWriteList) ExtensionList(hudson.ExtensionList) NodeList(hudson.slaves.NodeList) ArrayList(java.util.ArrayList) DescriptorExtensionList(hudson.DescriptorExtensionList) DescribableList(hudson.util.DescribableList) List(java.util.List) Level(java.util.logging.Level) InitReactorListener(hudson.init.InitReactorListener) ReactorListener(org.jvnet.hudson.reactor.ReactorListener) InitMilestone(hudson.init.InitMilestone)

Example 3 with Task

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;
}
Also used : Task(org.jvnet.hudson.reactor.Task) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) IOException(java.io.IOException)

Aggregations

Task (org.jvnet.hudson.reactor.Task)3 ArrayList (java.util.ArrayList)2 DescriptorExtensionList (hudson.DescriptorExtensionList)1 ExtensionList (hudson.ExtensionList)1 UDPBroadcastThread (hudson.UDPBroadcastThread)1 InitMilestone (hudson.init.InitMilestone)1 InitReactorListener (hudson.init.InitReactorListener)1 NodeList (hudson.slaves.NodeList)1 CopyOnWriteList (hudson.util.CopyOnWriteList)1 DaemonThreadFactory (hudson.util.DaemonThreadFactory)1 DescribableList (hudson.util.DescribableList)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 ExecutorService (java.util.concurrent.ExecutorService)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 Level (java.util.logging.Level)1 Milestone (org.jvnet.hudson.reactor.Milestone)1