Fixed monitoring not to wait for next time window before checking all endpoints

This commit is contained in:
Simon Pamies
2013-02-18 19:15:32 +01:00
parent 315ffea003
commit 1561ba03d4
3 changed files with 50 additions and 41 deletions
@@ -90,47 +90,51 @@ public abstract class AbstractPortMonitor extends Thread {
if (currentmillis >= (lastmillis + interval + (100*endpoints.length))) { if (currentmillis >= (lastmillis + interval + (100*endpoints.length))) {
for (int i = 0; i < endpoints.length; i++) { for (int i = 0; i < endpoints.length; i++) {
currentendpoint = endpoints[i]; currentendpoint = endpoints[i];
if (!currentendpoint.isURL()) { try {
Socket sn = new Socket(); if (!currentendpoint.isURL()) {
sn.connect(currentendpoint.getAddress(), timeout); Socket sn = new Socket();
sn.close(); sn.connect(currentendpoint.getAddress(), timeout);
} else { sn.close();
/* despite its name this does NOT open a real TCP connection */ } else {
HttpURLConnection conn = (HttpURLConnection)currentendpoint.getURL().openConnection(); /* despite its name this does NOT open a real TCP connection */
conn.setConnectTimeout(timeout); HttpURLConnection conn = (HttpURLConnection)currentendpoint.getURL().openConnection();
conn.setRequestMethod("GET"); conn.setConnectTimeout(timeout);
conn.connect(); conn.setRequestMethod("GET");
int code = conn.getResponseCode(); conn.connect();
conn.disconnect(); int code = conn.getResponseCode();
if (code != 200) { conn.disconnect();
throw new ConnectException("Could not successfully connect to endpoint " + currentendpoint.toString()); if (code != 200) {
throw new ConnectException("Could not successfully connect to endpoint " + currentendpoint.toString());
}
}
log.finest("Connection succeeded to " + currentendpoint.toString());
handleConnection(currentendpoint);
lastmillis = System.currentTimeMillis();
currentendpoint.setSuccess(System.currentTimeMillis());
} catch (SocketTimeoutException|ConnectException ex) {
/* if service has failed before then wait at least some time before checking it again */
boolean handle = true;
if (currentendpoint.hasFailed() /* failed before */) {
if ( (currentendpoint.lastFailed()+Integer.parseInt(properties.getProperty("monitor.wait_after_failure", "60000"))) > System.currentTimeMillis()) {
handle = false;
} else {
log.info("Service has failed (" + currentendpoint.toString() + ") before and gracetime is over. Calling failure handler again.");
}
} else {
log.info("Connection FAILED to " + currentendpoint.toString());
}
if (handle) {
handleFailure(currentendpoint);
lastmillis = System.currentTimeMillis();
currentendpoint.setFailure(System.currentTimeMillis());
} }
} }
log.finest("Connection succeeded to " + currentendpoint.toString());
handleConnection(currentendpoint);
lastmillis = System.currentTimeMillis();
currentendpoint.setSuccess(System.currentTimeMillis());
} }
} }
} catch (SocketTimeoutException|ConnectException ex) {
/* if service has failed before then wait at least some time before checking it again */
boolean handle = true;
if (currentendpoint.hasFailed() /* failed before */) {
if ( (currentendpoint.lastFailed()+Integer.parseInt(properties.getProperty("monitor.wait_after_failure", "60000"))) > System.currentTimeMillis()) {
handle = false;
} else {
log.info("Service has failed (" + currentendpoint.toString() + ") before and gracetime is over. Calling failure handler again.");
}
} else {
log.info("Connection FAILED to " + currentendpoint.toString());
}
if (handle) {
handleFailure(currentendpoint);
lastmillis = System.currentTimeMillis();
currentendpoint.setFailure(System.currentTimeMillis());
}
} catch (IOException ex) { } catch (IOException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
@@ -68,7 +68,7 @@ public class OSGiRestartingPortMonitor extends AbstractPortMonitor {
if (sysinfo_available) { if (sysinfo_available) {
info_before_restart = info.toString(); info_before_restart = info.toString();
} }
if (bundle.getState() == BundleEvent.STARTED || bundle.getState() == BundleEvent.STOPPED) { if (bundle.getState() == BundleEvent.STARTED || bundle.getState() == BundleEvent.STOPPED || bundle.getState() == 32l) {
try { try {
bundle.stop(); bundle.stop();
} catch (BundleException e) { } catch (BundleException e) {
@@ -77,13 +77,17 @@ public class OSGiRestartingPortMonitor extends AbstractPortMonitor {
try { try {
bundle.start(); bundle.start();
} catch (BundleException e) { } catch (BundleException e) {
e.printStackTrace();
log.severe("Could not start " + endpoint.getBundleName() + "! Handler will try again next time"); log.severe("Could not start " + endpoint.getBundleName() + "! Handler will try again next time");
} }
} else {
log.severe("Bundle " + endpoint.getBundleName() + " not in state STARTED or ACTIVE (State: +"+bundle.getState()+")! Restart not performed.");
} }
final String subject = "Bundle " + endpoint.getBundleName() + " restarted"; final String subject = "Bundle " + endpoint.getBundleName() + " restarted";
log.info(subject); log.info(subject);
/* only send mail if service has not failed before */
if (!endpoint.hasFailed() /* before */) { /* only send mail if service has not failed before and mailing is enabled */
if (!endpoint.hasFailed() /* before */ && this.properties.getProperty("mail.enabled", "true").equalsIgnoreCase("true")) {
log.info("Sending mail to " + this.properties.getProperty("mail.to") + " saying that bundle " log.info("Sending mail to " + this.properties.getProperty("mail.to") + " saying that bundle "
+ endpoint.getBundleName() + " was restarted"); + endpoint.getBundleName() + " was restarted");
try { try {
@@ -32,8 +32,9 @@ monitor.endpoints = http://127.0.0.1:8886/gwt/Leaderboard.html, http://127.0.0.1
monitor.bundles = com.sap.sailing.gwt.ui, com.sap.sailing.server.gateway, com.sap.sailing.www.events, com.sap.sailing.www monitor.bundles = com.sap.sailing.gwt.ui, com.sap.sailing.server.gateway, com.sap.sailing.www.events, com.sap.sailing.www
# mail configuration # mail configuration
mail.enabled = false
mail.from = info@sapsailing.com mail.from = info@sapsailing.com
mail.to = axel.uhl@sap.com, axel.uhl@gmx.de, fmittag@gmx.net mail.to = axel.uhl@sap.com, axel.uhl@gmx.de, fmittag@gmx.net, spamsch@gmail.com
mail.transport.protocol = smtp mail.transport.protocol = smtp
mail.smtp.host = 127.0.0.1 mail.smtp.host = 127.0.0.1
mail.smtp.port = 25 mail.smtp.port = 25