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))) {
for (int i = 0; i < endpoints.length; i++) {
currentendpoint = endpoints[i];
if (!currentendpoint.isURL()) {
Socket sn = new Socket();
sn.connect(currentendpoint.getAddress(), timeout);
sn.close();
} else {
/* despite its name this does NOT open a real TCP connection */
HttpURLConnection conn = (HttpURLConnection)currentendpoint.getURL().openConnection();
conn.setConnectTimeout(timeout);
conn.setRequestMethod("GET");
conn.connect();
int code = conn.getResponseCode();
conn.disconnect();
if (code != 200) {
throw new ConnectException("Could not successfully connect to endpoint " + currentendpoint.toString());
try {
if (!currentendpoint.isURL()) {
Socket sn = new Socket();
sn.connect(currentendpoint.getAddress(), timeout);
sn.close();
} else {
/* despite its name this does NOT open a real TCP connection */
HttpURLConnection conn = (HttpURLConnection)currentendpoint.getURL().openConnection();
conn.setConnectTimeout(timeout);
conn.setRequestMethod("GET");
conn.connect();
int code = conn.getResponseCode();
conn.disconnect();
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) {
ex.printStackTrace();
}
@@ -68,7 +68,7 @@ public class OSGiRestartingPortMonitor extends AbstractPortMonitor {
if (sysinfo_available) {
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 {
bundle.stop();
} catch (BundleException e) {
@@ -77,13 +77,17 @@ public class OSGiRestartingPortMonitor extends AbstractPortMonitor {
try {
bundle.start();
} catch (BundleException e) {
e.printStackTrace();
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";
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 "
+ endpoint.getBundleName() + " was restarted");
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
# mail configuration
mail.enabled = false
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.smtp.host = 127.0.0.1
mail.smtp.port = 25