/hudson-core/src/main/java/hudson/model/queue/QueueSorter.java

http://github.com/hudson/hudson · Java · 57 lines · 27 code · 9 blank · 21 comment · 3 complexity · 2680e1f550b14ce95abaa57c6e9158f3 MD5 · raw file

  1. package hudson.model.queue;
  2. import hudson.ExtensionList;
  3. import hudson.ExtensionPoint;
  4. import hudson.init.Initializer;
  5. import hudson.model.Hudson;
  6. import hudson.model.Queue;
  7. import hudson.model.Queue.BuildableItem;
  8. import java.util.List;
  9. import java.util.logging.Logger;
  10. import static hudson.init.InitMilestone.JOB_LOADED;
  11. /**
  12. * Singleton extension point for sorting buildable items
  13. *
  14. * @since 1.343
  15. */
  16. public abstract class QueueSorter implements ExtensionPoint {
  17. /**
  18. * Sorts the buildable items list. The items at the beginning will be executed
  19. * before the items at the end of the list.
  20. *
  21. * @param buildables
  22. * List of buildable items in the queue. Never null.
  23. */
  24. public abstract void sortBuildableItems(List<BuildableItem> buildables);
  25. /**
  26. * All registered {@link QueueSorter}s. Only the first one will be picked up,
  27. * unless explicitly overridden by {@link Queue#setSorter(QueueSorter)}.
  28. */
  29. public static ExtensionList<QueueSorter> all() {
  30. return Hudson.getInstance().getExtensionList(QueueSorter.class);
  31. }
  32. /**
  33. * Installs the default queue sorter.
  34. *
  35. * {@link Queue#Queue(hudson.model.LoadBalancer)} is too early to do this
  36. */
  37. @Initializer(after=JOB_LOADED)
  38. public static void installDefaultQueueSorter() {
  39. ExtensionList<QueueSorter> all = all();
  40. if (all.isEmpty()) return;
  41. Queue q = Hudson.getInstance().getQueue();
  42. if (q.getSorter()!=null) return; // someone has already installed something. leave that alone.
  43. q.setSorter(all.get(0));
  44. if (all.size()>1)
  45. LOGGER.warning("Multiple QueueSorters are registered. Only the first one is used and the rest are ignored: "+all);
  46. }
  47. private static final Logger LOGGER = Logger.getLogger(QueueSorter.class.getName());
  48. }