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.

StopRequestWatcherImpl.java 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.application.process;
  21. import org.sonar.application.FileSystem;
  22. import org.sonar.application.config.AppSettings;
  23. import org.sonar.process.ProcessId;
  24. import org.sonar.process.sharedmemoryfile.DefaultProcessCommands;
  25. import org.sonar.process.sharedmemoryfile.ProcessCommands;
  26. import static org.sonar.process.ProcessProperties.Property.ENABLE_STOP_COMMAND;
  27. public class StopRequestWatcherImpl extends Thread implements StopRequestWatcher {
  28. private static final long DEFAULT_WATCHER_DELAY_MS = 500L;
  29. private final ProcessCommands commands;
  30. private final Runnable listener;
  31. private final AppSettings settings;
  32. private long delayMs = DEFAULT_WATCHER_DELAY_MS;
  33. StopRequestWatcherImpl(AppSettings settings, Runnable listener, ProcessCommands commands) {
  34. super("StopRequestWatcherImpl");
  35. this.settings = settings;
  36. this.commands = commands;
  37. this.listener = listener;
  38. // safeguard, do not block the JVM if thread is not interrupted
  39. // (method stopWatching() never called).
  40. setDaemon(true);
  41. }
  42. public static StopRequestWatcherImpl create(AppSettings settings, Runnable listener, FileSystem fs) {
  43. DefaultProcessCommands commands = DefaultProcessCommands.secondary(fs.getTempDir(), ProcessId.APP.getIpcIndex());
  44. return new StopRequestWatcherImpl(settings, listener, commands);
  45. }
  46. long getDelayMs() {
  47. return delayMs;
  48. }
  49. void setDelayMs(long delayMs) {
  50. this.delayMs = delayMs;
  51. }
  52. @Override
  53. public void run() {
  54. try {
  55. while (true) {
  56. if (commands.askedForStop()) {
  57. listener.run();
  58. return;
  59. }
  60. Thread.sleep(delayMs);
  61. }
  62. } catch (InterruptedException e) {
  63. Thread.currentThread().interrupt();
  64. // stop watching the commands
  65. }
  66. }
  67. @Override
  68. public void startWatching() {
  69. if (settings.getProps().valueAsBoolean(ENABLE_STOP_COMMAND.getKey())) {
  70. start();
  71. }
  72. }
  73. @Override
  74. public void stopWatching() {
  75. // does nothing if not started
  76. interrupt();
  77. }
  78. }