From 88942f341575a9e07f9951617ac4b59940e6558e Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 12 Sep 2014 16:07:48 +0200 Subject: [PATCH] SONAR-4898 fix merge of branch-4.5 --- .../org/sonar/process/MonitoredProcess.java | 169 ------------------ server/sonar-process-monitor/pom.xml | 2 +- server/sonar-process/pom.xml | 2 +- 3 files changed, 2 insertions(+), 171 deletions(-) delete mode 100644 server/process/sonar-process/src/main/java/org/sonar/process/MonitoredProcess.java diff --git a/server/process/sonar-process/src/main/java/org/sonar/process/MonitoredProcess.java b/server/process/sonar-process/src/main/java/org/sonar/process/MonitoredProcess.java deleted file mode 100644 index 39331b60a2b..00000000000 --- a/server/process/sonar-process/src/main/java/org/sonar/process/MonitoredProcess.java +++ /dev/null @@ -1,169 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.process; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; - -public abstract class MonitoredProcess implements ProcessMXBean { - - private static final Logger LOGGER = LoggerFactory.getLogger(MonitoredProcess.class); - - private static final long AUTOKILL_TIMEOUT_MS = 30000L; - private static final long AUTOKILL_CHECK_DELAY_MS = 2000L; - public static final String NAME_PROPERTY = "pName"; - - private Long lastPing; - private final String name; - private boolean terminated = false; - private long timeout = AUTOKILL_TIMEOUT_MS; - private long checkDelay = AUTOKILL_CHECK_DELAY_MS; - - protected final Props props; - private ScheduledFuture pingTask = null; - - private ScheduledExecutorService monitor; - private final boolean isMonitored; - - protected MonitoredProcess(Props props) { - this(props, !ProcessUtils.isJvmDebugEnabled()); - } - - protected MonitoredProcess(Props props, boolean monitor) { - this.isMonitored = monitor; - this.props = props; - this.name = props.nonNullValue(NAME_PROPERTY); - - JmxUtils.registerMBean(this, name); - ProcessUtils.addSelfShutdownHook(this); - } - - public MonitoredProcess setTimeout(long timeout) { - this.timeout = timeout; - return this; - } - - private long getTimeout() { - return timeout; - } - - public MonitoredProcess setCheckDelay(long checkDelay) { - this.checkDelay = checkDelay; - return this; - } - - private long getCheckDelay() { - return checkDelay; - } - - public final void start() { - if (monitor != null) { - throw new IllegalStateException("Already started"); - } - LOGGER.debug("Process[{}] starting", name); - scheduleAutokill(isMonitored); - try { - doStart(); - } catch (Exception e) { - LOGGER.error("Could not start process: {}", e); - this.terminate(); - } - LOGGER.debug("Process[{}] started", name); - } - - /** - * If the process does not receive pings during the max allowed period, then - * process auto-kills - */ - private void scheduleAutokill(final boolean isMonitored) { - final Runnable breakOnMissingPing = new Runnable() { - @Override - public void run() { - long time = System.currentTimeMillis(); - if (time - lastPing > getTimeout()) { - if (isMonitored) { - LoggerFactory.getLogger(getClass()).info(String.format( - "Did not receive any ping during %d seconds. Shutting down.", getTimeout() / 1000)); - terminate(); - } - } - } - }; - lastPing = System.currentTimeMillis(); - monitor = Executors.newScheduledThreadPool(1); - pingTask = monitor.scheduleAtFixedRate(breakOnMissingPing, getCheckDelay(), getCheckDelay(), TimeUnit.MILLISECONDS); - } - - @Override - public final long ping() { - this.lastPing = System.currentTimeMillis(); - return lastPing; - } - - @Override - public final synchronized void terminate() { - if (monitor != null) { - LOGGER.debug("Process[{}] terminating", name); - try { - doTerminate(); - } catch (Exception e) { - LOGGER.error("Fail to terminate " + name, e); - // do not propagate exception - } - monitor.shutdownNow(); - monitor = null; - if (pingTask != null) { - pingTask.cancel(true); - pingTask = null; - } - LOGGER.debug("Process[{}] terminated", name); - terminated = true; - } - } - - public boolean isTerminated() { - return terminated && monitor == null; - } - - public boolean isMonitored() { - return this.isMonitored; - } - - @Override - public final boolean isReady() { - try { - return doIsReady(); - } catch (Exception ignored) { - LOGGER.trace("Exception while checking if ready", ignored); - return false; - } - } - - protected abstract void doStart(); - - protected abstract void doTerminate(); - - protected abstract boolean doIsReady(); -} diff --git a/server/sonar-process-monitor/pom.xml b/server/sonar-process-monitor/pom.xml index fa0869a52b8..0e347c286ae 100644 --- a/server/sonar-process-monitor/pom.xml +++ b/server/sonar-process-monitor/pom.xml @@ -5,7 +5,7 @@ org.codehaus.sonar server - 4.5-SNAPSHOT + 5.0-SNAPSHOT ../ 4.0.0 diff --git a/server/sonar-process/pom.xml b/server/sonar-process/pom.xml index 25413fd7ce9..38fdf7c2c6e 100644 --- a/server/sonar-process/pom.xml +++ b/server/sonar-process/pom.xml @@ -5,7 +5,7 @@ org.codehaus.sonar server - 4.5-SNAPSHOT + 5.0-SNAPSHOT ../ 4.0.0 -- 2.39.5