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.

CEQueueStatusImpl.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.monitoring;
  21. import java.util.Optional;
  22. import java.util.concurrent.atomic.AtomicLong;
  23. import org.sonar.api.utils.System2;
  24. import org.sonar.db.DbClient;
  25. import org.sonar.db.DbSession;
  26. import org.sonar.db.ce.CeQueueDto;
  27. import org.sonar.server.property.InternalProperties;
  28. import static com.google.common.base.Preconditions.checkArgument;
  29. public class CEQueueStatusImpl implements CEQueueStatus {
  30. private final DbClient dbClient;
  31. private final System2 system;
  32. private final AtomicLong inProgress = new AtomicLong(0);
  33. private final AtomicLong error = new AtomicLong(0);
  34. private final AtomicLong success = new AtomicLong(0);
  35. private final AtomicLong processingTime = new AtomicLong(0);
  36. public CEQueueStatusImpl(DbClient dbClient, System2 system) {
  37. this.dbClient = dbClient;
  38. this.system = system;
  39. }
  40. @Override
  41. public long addInProgress() {
  42. return inProgress.incrementAndGet();
  43. }
  44. @Override
  45. public long addError(long processingTimeInMs) {
  46. addProcessingTime(processingTimeInMs);
  47. inProgress.decrementAndGet();
  48. return error.incrementAndGet();
  49. }
  50. @Override
  51. public long addSuccess(long processingTimeInMs) {
  52. addProcessingTime(processingTimeInMs);
  53. inProgress.decrementAndGet();
  54. return success.incrementAndGet();
  55. }
  56. private void addProcessingTime(long ms) {
  57. checkArgument(ms >= 0, "Processing time can not be < 0");
  58. processingTime.addAndGet(ms);
  59. }
  60. @Override
  61. public long getPendingCount() {
  62. try (DbSession dbSession = dbClient.openSession(false)) {
  63. return dbClient.ceQueueDao().countByStatus(dbSession, CeQueueDto.Status.PENDING);
  64. }
  65. }
  66. @Override
  67. public Optional<Long> getLongestTimePending() {
  68. try (DbSession dbSession = dbClient.openSession(false)) {
  69. return dbClient.ceQueueDao().selectCreationDateOfOldestPendingByMainComponentUuid(dbSession, null)
  70. .map(creationDate -> system.now() - creationDate);
  71. }
  72. }
  73. @Override
  74. public boolean areWorkersPaused() {
  75. try (DbSession dbSession = dbClient.openSession(false)) {
  76. Optional<String> val = dbClient.internalPropertiesDao().selectByKey(dbSession, InternalProperties.COMPUTE_ENGINE_PAUSE);
  77. return "true".equals(val.orElse(null));
  78. }
  79. }
  80. @Override
  81. public long getInProgressCount() {
  82. return inProgress.get();
  83. }
  84. @Override
  85. public long getErrorCount() {
  86. return error.get();
  87. }
  88. @Override
  89. public long getSuccessCount() {
  90. return success.get();
  91. }
  92. @Override
  93. public long getProcessingTime() {
  94. return processingTime.get();
  95. }
  96. }