/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. */
  24. package hudson.model;
  25. import hudson.triggers.SafeTimerTask;
  26. import hudson.triggers.Trigger;
  27. import hudson.ExtensionPoint;
  28. import hudson.Extension;
  29. import hudson.ExtensionList;
  30. import java.util.logging.Logger;
  31. import java.util.Random;
  32. import java.util.Timer;
  33. /**
  34. * Extension point to perform a periodic task in Hudson (through {@link Timer}.)
  35. *
  36. * <p>
  37. * This extension point is useful if your plugin needs to perform some work in the background periodically
  38. * (for example, monitoring, batch processing, garbage collection, etc.)
  39. *
  40. * <p>
  41. * Put {@link Extension} on your class to have it picked up and registered automatically, or
  42. * manually insert this to {@link Trigger#timer}.
  43. *
  44. * <p>
  45. * This class is designed to run a short task. Implementations whose periodic work takes a long time
  46. * to run should extend from {@link AsyncPeriodicWork} instead.
  47. *
  48. * @author Kohsuke Kawaguchi
  49. * @see AsyncPeriodicWork
  50. */
  51. public abstract class PeriodicWork extends SafeTimerTask implements ExtensionPoint {
  52. protected final Logger logger = Logger.getLogger(getClass().getName());
  53. /**
  54. * Gets the number of milliseconds between successive executions.
  55. *
  56. * <p>
  57. * Hudson calls this method once to set up a recurring timer, instead of
  58. * calling this each time after the previous execution completed. So this class cannot be
  59. * used to implement a non-regular recurring timer.
  60. *
  61. * <p>
  62. * IOW, the method should always return the same value.
  63. */
  64. public abstract long getRecurrencePeriod();
  65. /**
  66. * Gets the number of milliseconds til the first execution.
  67. *
  68. * <p>
  69. * By default it chooses the value randomly between 0 and {@link #getRecurrencePeriod()}
  70. */
  71. public long getInitialDelay() {
  72. return Math.abs(new Random().nextLong())%getRecurrencePeriod();
  73. }
  74. /**
  75. * Returns all the registered {@link PeriodicWork}s.
  76. */
  77. public static ExtensionList<PeriodicWork> all() {
  78. return Hudson.getInstance().getExtensionList(PeriodicWork.class);
  79. }
  80. // time constants
  81. protected static final long MIN = 1000*60;
  82. protected static final long HOUR =60*MIN;
  83. protected static final long DAY = 24*HOUR;
  84. }