AbstractNodeMonitorDescriptor.java 4.8 KB
Newer Older
1 2 3 4 5 6
package hudson.node_monitors;

import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.model.Hudson;
import hudson.triggers.Trigger;
7
import hudson.triggers.SafeTimerTask;
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Convenient base class for common {@link NodeMonitor} implementation
 * where the "monitoring" consists of executing something periodically on every node
 * and taking some action based on its result.
 *
 * <p>
 * "T" represents the the result of the monitoring. 
 *
 * @author Kohsuke Kawaguchi
 */
public abstract class AbstractNodeMonitorDescriptor<T> extends Descriptor<NodeMonitor> {
    protected AbstractNodeMonitorDescriptor(Class<? extends NodeMonitor> clazz) {
        this(clazz,HOUR);
    }

    protected AbstractNodeMonitorDescriptor(Class<? extends NodeMonitor> clazz, long interval) {
        super(clazz);

        // check every hour
35 36
        Trigger.timer.scheduleAtFixedRate(new SafeTimerTask() {
            public void doRun() {
37
                triggerUpdate();
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
            }
        }, interval, interval);
    }

    /**
     * Represents the last record of the update
     */
    private volatile Record record = null;

    /**
     * Represents the update activity in progress.
     */
    private volatile Record inProgress = null;

    /**
     * Performs monitoring of the given computer object.
     * This method is invoked periodically to perform the monitoring of the computer.
K
kohsuke 已提交
55 56 57 58 59 60 61 62
     *
     * @return
     *      Application-specific value that represents the observed monitoring value
     *      on the given node. This value will be returned from the {@link #get(Computer)} method.
     *      If null is returned, it will be interpreted as "no observed value." This is
     *      convenient way of abandoning the observation on a particular computer,
     *      whereas {@link IOException} is useful for indicating a hard error that needs to be
     *      corrected.
63 64 65 66
     */
    protected abstract T monitor(Computer c) throws IOException,InterruptedException;

    /**
K
kohsuke 已提交
67 68 69 70
     * Obtains the monitoring result currently available, or null if no data is available.
     *
     * <p>
     * If no data is available, a background task to collect data will be started.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
     */
    public T get(Computer c) {
        if(record==null) {
            // if this is the first time, schedule the check now
            if(inProgress==null) {
                synchronized(this) {
                    if(inProgress==null)
                        new Record().start();
                }
            }
            return null;
        }
        return record.data.get(c);
    }

86
    /**
K
kohsuke 已提交
87
     * @see NodeMonitor#triggerUpdate()
88
     */
K
kohsuke 已提交
89
    /*package*/ Thread triggerUpdate() {
90 91 92 93 94
        Record t = new Record();
        t.start();
        return t;
    }

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    /**
     * Thread that monitors nodes, as well as the data structure to record
     * the result.
     */
    private final class Record extends Thread {
        /**
         * Last computed monitoring result.
         */
        private final Map<Computer,T> data = new HashMap<Computer,T>();

        public Record() {
            super("Monitoring thread for "+getDisplayName()+" started on "+new Date());
            synchronized(AbstractNodeMonitorDescriptor.this) {
                if(inProgress!=null) {
                    // maybe it got stuck?
                    LOGGER.warning("Previous "+getDisplayName()+" monitoring activity still in progress. Interrupting");
                    inProgress.interrupt();
                }
                inProgress = this;
            }
        }

        public void run() {
            try {
                long startTime = System.currentTimeMillis();

                for( Computer c : Hudson.getInstance().getComputers() ) {
                    try {
123
                        if(c.getChannel()==null)
K
kohsuke 已提交
124 125 126
                            data.put(c,null);
                        else
                            data.put(c,monitor(c));
127
                    } catch (IOException e) {
K
kohsuke 已提交
128
                        LOGGER.log(Level.WARNING, "Failed to monitor "+c.getDisplayName()+" for "+getDisplayName(), e);
129 130 131 132 133 134 135 136 137
                    }
                }

                synchronized(AbstractNodeMonitorDescriptor.this) {
                    assert inProgress==this;
                    inProgress = null;
                    record = this;
                }

K
kohsuke 已提交
138
                LOGGER.fine("Node monitoring "+getDisplayName()+" completed in "+(System.currentTimeMillis()-startTime)+"ms");
139 140 141 142 143 144 145 146 147 148
            } catch (InterruptedException e) {
                LOGGER.log(Level.WARNING,"Node monitoring "+getDisplayName()+" aborted.",e);
            }
        }
    }

    private final Logger LOGGER = Logger.getLogger(getClass().getName());

    private static final long HOUR = 1000*60*60L;
}