PageRenderTime 30ms CodeModel.GetById 26ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/hudson-core/src/main/java/hudson/model/PeriodicWork.java

http://github.com/hudson/hudson
Java | 91 lines | 22 code | 7 blank | 62 comment | 0 complexity | a7130b67075945c7bf875e8d2d3f16fd MD5 | raw file
 1/*
 2 * The MIT License
 3 * 
 4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
 5 * 
 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
 7 * of this software and associated documentation files (the "Software"), to deal
 8 * in the Software without restriction, including without limitation the rights
 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 * 
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 * 
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24package hudson.model;
25
26import hudson.triggers.SafeTimerTask;
27import hudson.triggers.Trigger;
28import hudson.ExtensionPoint;
29import hudson.Extension;
30import hudson.ExtensionList;
31
32import java.util.logging.Logger;
33import java.util.Random;
34import java.util.Timer;
35
36/**
37 * Extension point to perform a periodic task in Hudson (through {@link Timer}.)
38 *
39 * <p>
40 * This extension point is useful if your plugin needs to perform some work in the background periodically
41 * (for example, monitoring, batch processing, garbage collection, etc.)
42 *
43 * <p>
44 * Put {@link Extension} on your class to have it picked up and registered automatically, or
45 * manually insert this to {@link Trigger#timer}.
46 *
47 * <p>
48 * This class is designed to run a short task. Implementations whose periodic work takes a long time
49 * to run should extend from {@link AsyncPeriodicWork} instead. 
50 *
51 * @author Kohsuke Kawaguchi
52 * @see AsyncPeriodicWork
53 */
54public abstract class PeriodicWork extends SafeTimerTask implements ExtensionPoint {
55    protected final Logger logger = Logger.getLogger(getClass().getName());
56
57    /**
58     * Gets the number of milliseconds between successive executions.
59     *
60     * <p>
61     * Hudson calls this method once to set up a recurring timer, instead of
62     * calling this each time after the previous execution completed. So this class cannot be
63     * used to implement a non-regular recurring timer.
64     *
65     * <p>
66     * IOW, the method should always return the same value.
67     */
68    public abstract long getRecurrencePeriod();
69
70    /**
71     * Gets the number of milliseconds til the first execution.
72     *
73     * <p>
74     * By default it chooses the value randomly between 0 and {@link #getRecurrencePeriod()}
75     */
76    public long getInitialDelay() {
77        return Math.abs(new Random().nextLong())%getRecurrencePeriod();
78    }
79
80    /**
81     * Returns all the registered {@link PeriodicWork}s.
82     */
83    public static ExtensionList<PeriodicWork> all() {
84        return Hudson.getInstance().getExtensionList(PeriodicWork.class);
85    }
86
87    // time constants
88    protected static final long MIN = 1000*60;
89    protected static final long HOUR =60*MIN;
90    protected static final long DAY = 24*HOUR;
91}