X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=webservices%2Fcompbio%2Fstat%2Fservlet%2FStatisticCollector.java;h=0ffe77d671d3f83f19ca7b09f924de2776cf6856;hb=243b5d0278c06196854f7312d4c2d933deb89061;hp=3ababa34dda1bfca3cb5d418b6801fcc5b28a4d3;hpb=430553b263c6c20b20978f418d2112a9002bd97a;p=jabaws.git diff --git a/webservices/compbio/stat/servlet/StatisticCollector.java b/webservices/compbio/stat/servlet/StatisticCollector.java index 3ababa3..0ffe77d 100644 --- a/webservices/compbio/stat/servlet/StatisticCollector.java +++ b/webservices/compbio/stat/servlet/StatisticCollector.java @@ -1,20 +1,131 @@ package compbio.stat.servlet; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; +import org.apache.log4j.Logger; + +import compbio.engine.conf.PropertyHelperManager; +import compbio.stat.collector.ExecutionStatCollector; +import compbio.stat.collector.StatDB; +import compbio.util.PropertyHelper; + public class StatisticCollector implements ServletContextListener { + static PropertyHelper ph = PropertyHelperManager.getPropertyHelper(); + + private final Logger log = Logger.getLogger(StatisticCollector.class); + + private ScheduledFuture localcf; + private ScheduledFuture clustercf; + private ScheduledExecutorService executor; + @Override public void contextDestroyed(ServletContextEvent arg0) { - // TODO Auto-generated method stub - + try { + if (localcf != null) { + localcf.cancel(true); + } + if (clustercf != null) { + clustercf.cancel(true); + } + executor.shutdown(); + executor.awaitTermination(3, TimeUnit.SECONDS); + } catch (InterruptedException e) { + log.warn(e.getMessage(), e); + } finally { + StatDB.shutdownDBServer(); + executor.shutdownNow(); + } } @Override public void contextInitialized(ServletContextEvent arg0) { - // TODO Auto-generated method stub + String clusterWorkDir = getClusterJobDir(); + int clusterMaxRuntime = getClusterJobTimeOut(); + + int localMaxRuntime = getLocalJobTimeOut(); + String localWorkDir = getLocalJobDir(); + + log.info("Initializing statistics collector"); + executor = Executors.newScheduledThreadPool(1); + + if (collectClusterStats()) { + ExecutionStatCollector clusterCollector = new ExecutionStatCollector( + clusterWorkDir, clusterMaxRuntime); + clustercf = executor.scheduleAtFixedRate(clusterCollector, 60, + 24 * 60, TimeUnit.MINUTES); + log.info("Collecting cluster statistics "); + } + if (collectLocalStats()) { + ExecutionStatCollector localCollector = new ExecutionStatCollector( + localWorkDir, localMaxRuntime); + localcf = executor.scheduleAtFixedRate(localCollector, 10, 24 * 60, + TimeUnit.MINUTES); + log.info("Collecting local statistics "); + } + + } + + static String getClusterJobDir() { + return getStringProperty(ph.getProperty("cluster.tmp.directory")); + } + + static int getClusterJobTimeOut() { + int maxRunTime = 24 * 7; + String clusterMaxRuntime = ph.getProperty("cluster.stat.maxruntime"); + if (clusterMaxRuntime != null) { + clusterMaxRuntime = clusterMaxRuntime.trim(); + maxRunTime = Integer.parseInt(clusterMaxRuntime); + } + return maxRunTime; + } + + static int getLocalJobTimeOut() { + int maxRunTime = 24; + String localMaxRuntime = ph.getProperty("local.stat.maxruntime"); + if (localMaxRuntime != null) { + localMaxRuntime = localMaxRuntime.trim(); + maxRunTime = Integer.parseInt(localMaxRuntime); + } + + return maxRunTime; + } + + static String getLocalJobDir() { + return getStringProperty(ph.getProperty("local.tmp.directory")); + } + + private static String getStringProperty(String propName) { + String locdir = ph.getProperty(propName); + if (locdir != null) { + locdir = locdir.trim(); + } + return locdir; + } + + static boolean collectClusterStats() { + return getBooleanProperty(ph + .getProperty("cluster.stat.collector.enable")); + + } + + static boolean collectLocalStats() { + return getBooleanProperty(ph.getProperty("local.stat.collector.enable")); + } + private static boolean getBooleanProperty(String propName) { + boolean enabled = false; + if (propName != null) { + propName = propName.trim(); + enabled = Boolean.parseBoolean(propName); + } + return enabled; } }