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.

ComputeEngineImpl.java 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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;
  21. import org.sonar.ce.container.ComputeEngineStatus;
  22. import org.sonar.ce.container.ComputeEngineContainer;
  23. import org.sonar.process.Props;
  24. import static com.google.common.base.Preconditions.checkState;
  25. public class ComputeEngineImpl implements ComputeEngine, ComputeEngineStatus {
  26. private final Props props;
  27. private final ComputeEngineContainer computeEngineContainer;
  28. private Status status = Status.INIT;
  29. public ComputeEngineImpl(Props props, ComputeEngineContainer computeEngineContainer) {
  30. this.props = props;
  31. this.computeEngineContainer = computeEngineContainer;
  32. computeEngineContainer.setComputeEngineStatus(this);
  33. }
  34. @Override
  35. public void startup() {
  36. checkState(this.status == Status.INIT, "startup() can not be called multiple times");
  37. try {
  38. this.status = Status.STARTING;
  39. this.computeEngineContainer.start(props);
  40. } finally {
  41. this.status = Status.STARTED;
  42. }
  43. }
  44. @Override
  45. public void stopProcessing() {
  46. checkState(this.status.ordinal() >= Status.STARTED.ordinal(), "stopProcessing() must not be called before startup()");
  47. checkState(this.status.ordinal() <= Status.STOPPING.ordinal(), "stopProcessing() can not be called after shutdown()");
  48. checkState(this.status.ordinal() <= Status.STOPPING_WORKERS.ordinal(), "stopProcessing() can not be called multiple times");
  49. try {
  50. this.status = Status.STOPPING_WORKERS;
  51. this.computeEngineContainer.stopWorkers();
  52. } finally {
  53. this.status = Status.WORKERS_STOPPED;
  54. }
  55. }
  56. @Override
  57. public void shutdown() {
  58. checkState(this.status.ordinal() >= Status.STARTED.ordinal(), "shutdown() must not be called before startup()");
  59. checkState(this.status.ordinal() <= Status.STOPPING.ordinal(), "shutdown() can not be called multiple times");
  60. try {
  61. this.status = Status.STOPPING;
  62. this.computeEngineContainer.stop();
  63. } finally {
  64. this.status = Status.STOPPED;
  65. }
  66. }
  67. @Override
  68. public Status getStatus() {
  69. return status;
  70. }
  71. }