You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CeQueue.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.ce.queue;
  21. import java.util.Collection;
  22. import java.util.List;
  23. import java.util.Optional;
  24. import org.sonar.ce.task.CeTask;
  25. import org.sonar.db.DbSession;
  26. import org.sonar.db.ce.CeQueueDto;
  27. /**
  28. * Queue of pending Compute Engine tasks. Both producer and consumer actions
  29. * are implemented.
  30. * <p>
  31. * This class is decoupled from the regular task type {@link org.sonar.db.ce.CeTaskTypes#REPORT}.
  32. * </p>
  33. */
  34. public interface CeQueue {
  35. /**
  36. * Build an instance of {@link CeTaskSubmit} required for {@link #submit(CeTaskSubmit, SubmitOption...)}. It allows
  37. * to enforce that task ids are generated by the queue. It's used also for having access
  38. * to the id before submitting the task to the queue.
  39. */
  40. CeTaskSubmit.Builder prepareSubmit();
  41. /**
  42. * Submits a task to the queue. The task is processed asynchronously.
  43. * <p>
  44. * Convenience method for calling {@link #submit(CeTaskSubmit, SubmitOption...)} without any {@link SubmitOption}
  45. * and which does not returning an {@link Optional}.
  46. * <p>
  47. * This method is equivalent to calling {@link #massSubmit(Collection, SubmitOption...)} with a singleton list and no
  48. * option.
  49. */
  50. CeTask submit(CeTaskSubmit submission);
  51. /**
  52. * Submits a task to the queue. The task is processed asynchronously.
  53. * <p>
  54. * This method is equivalent to calling {@code massSubmit(Collections.singletonList(submission))}.
  55. *
  56. * @return empty if {@code options} contains {@link SubmitOption#UNIQUE_QUEUE_PER_MAIN_COMPONENT UNIQUE_QUEUE_PER_MAIN_COMPONENT}
  57. * and there's already a queued task, otherwise the created task.
  58. */
  59. Optional<CeTask> submit(CeTaskSubmit submission, SubmitOption... options);
  60. /**
  61. * Submits multiple tasks to the queue at once. All tasks are processed asynchronously.
  62. * <p>
  63. * This method will perform significantly better that calling {@link #submit(CeTaskSubmit, SubmitOption...)} in a loop.
  64. * </p>
  65. */
  66. List<CeTask> massSubmit(Collection<CeTaskSubmit> submissions, SubmitOption... options);
  67. /**
  68. * Cancels a task in status {@link org.sonar.db.ce.CeQueueDto.Status#PENDING}. An unchecked
  69. * exception is thrown if the status is not {@link org.sonar.db.ce.CeQueueDto.Status#PENDING}.
  70. */
  71. void cancel(DbSession dbSession, CeQueueDto ceQueueDto);
  72. /**
  73. * Removes all the tasks from the queue, except the tasks with status
  74. * {@link org.sonar.db.ce.CeQueueDto.Status#IN_PROGRESS} are ignored. They are marked
  75. * as {@link org.sonar.db.ce.CeActivityDto.Status#CANCELED} in past activity.
  76. * This method can be called at runtime, even if workers are being executed.
  77. *
  78. * @return the number of canceled tasks
  79. */
  80. int cancelAll();
  81. /**
  82. * Requests workers to stop peeking tasks from queue. Does nothing if workers are already paused or being paused.
  83. * The workers that are already processing tasks are not interrupted.
  84. * This method is not restricted to the local workers. All the Compute Engine nodes are paused.
  85. */
  86. void pauseWorkers();
  87. /**
  88. * Resumes workers so that they can peek tasks from queue.
  89. * This method is not restricted to the local workers. All the Compute Engine nodes are paused.
  90. */
  91. void resumeWorkers();
  92. WorkersPauseStatus getWorkersPauseStatus();
  93. enum SubmitOption {
  94. UNIQUE_QUEUE_PER_MAIN_COMPONENT
  95. }
  96. enum WorkersPauseStatus {
  97. /**
  98. * Pause triggered but at least one task is still in-progress
  99. */
  100. PAUSING,
  101. /**
  102. * Paused, no tasks are in-progress. Tasks are pending.
  103. */
  104. PAUSED,
  105. /**
  106. * Not paused nor pausing
  107. */
  108. RESUMED
  109. }
  110. }