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

http://github.com/hudson/hudson · Java · 57 lines · 27 code · 7 blank · 23 comment · 2 complexity · 17befc4627263342389ea8c1e1bd464e MD5 · raw file

  1. package hudson.model;
  2. import hudson.Extension;
  3. import hudson.ExtensionList;
  4. import hudson.ExtensionPoint;
  5. import java.io.IOException;
  6. /**
  7. * Extension point that allows plugins to veto the restart.
  8. *
  9. * @author Kohsuke Kawaguchi
  10. * @since 1.376
  11. */
  12. public abstract class RestartListener implements ExtensionPoint {
  13. /**
  14. * Called periodically during the safe restart.
  15. *
  16. * @return false to block the restart
  17. */
  18. public abstract boolean isReadyToRestart() throws IOException, InterruptedException;
  19. /**
  20. * Called immediately before the restart is actually triggered.
  21. */
  22. public void onRestart() {}
  23. /**
  24. * Returns all the registered {@link LabelFinder}s.
  25. */
  26. public static ExtensionList<RestartListener> all() {
  27. return Hudson.getInstance().getExtensionList(RestartListener.class);
  28. }
  29. /**
  30. * Returns true iff all the listeners OKed the restart.
  31. */
  32. public static boolean isAllReady() throws IOException, InterruptedException {
  33. for (RestartListener listener : all()) {
  34. if (!listener.isReadyToRestart())
  35. return false;
  36. }
  37. return true;
  38. }
  39. /**
  40. * Default logic. Wait for all the executors to become idle.
  41. */
  42. @Extension
  43. public static class Default extends RestartListener {
  44. @Override
  45. public boolean isReadyToRestart() throws IOException, InterruptedException {
  46. Hudson h = Hudson.getInstance();
  47. return h.overallLoad.computeTotalExecutors() <= h.overallLoad.computeIdleExecutors();
  48. }
  49. }
  50. }