package compbio.stat.servlet;\r
\r
+import java.util.concurrent.Executors;\r
+import java.util.concurrent.ScheduledExecutorService;\r
+import java.util.concurrent.ScheduledFuture;\r
+import java.util.concurrent.TimeUnit;\r
+\r
import javax.servlet.ServletContextEvent;\r
import javax.servlet.ServletContextListener;\r
\r
+import org.apache.log4j.Logger;\r
+\r
+import compbio.engine.conf.PropertyHelperManager;\r
+import compbio.stat.collector.ExecutionStatCollector;\r
+import compbio.stat.collector.StatDB;\r
+import compbio.util.PropertyHelper;\r
+\r
public class StatisticCollector implements ServletContextListener {\r
\r
+ static PropertyHelper ph = PropertyHelperManager.getPropertyHelper();\r
+\r
+ private final Logger log = Logger.getLogger(StatisticCollector.class);\r
+\r
+ private ScheduledFuture<?> localcf;\r
+ private ScheduledFuture<?> clustercf;\r
+ private ScheduledExecutorService executor;\r
+\r
@Override\r
public void contextDestroyed(ServletContextEvent arg0) {\r
- // TODO Auto-generated method stub\r
-\r
+ try {\r
+ if (localcf != null) {\r
+ localcf.cancel(true);\r
+ }\r
+ if (clustercf != null) {\r
+ clustercf.cancel(true);\r
+ }\r
+ executor.shutdown();\r
+ executor.awaitTermination(3, TimeUnit.SECONDS);\r
+ } catch (InterruptedException e) {\r
+ log.warn(e.getMessage(), e);\r
+ } finally {\r
+ StatDB.shutdownDBServer();\r
+ executor.shutdownNow();\r
+ }\r
}\r
\r
@Override\r
public void contextInitialized(ServletContextEvent arg0) {\r
- // TODO Auto-generated method stub\r
+ String clusterWorkDir = getClusterJobDir();\r
+ int clusterMaxRuntime = getClusterJobTimeOut();\r
+\r
+ int localMaxRuntime = getLocalJobTimeOut();\r
+ String localWorkDir = getLocalJobDir();\r
+\r
+ log.info("Initializing statistics collector");\r
+ executor = Executors.newScheduledThreadPool(1);\r
+\r
+ if (collectClusterStats()) {\r
+ ExecutionStatCollector clusterCollector = new ExecutionStatCollector(\r
+ clusterWorkDir, clusterMaxRuntime);\r
+ clustercf = executor.scheduleAtFixedRate(clusterCollector, 60,\r
+ 24 * 60, TimeUnit.MINUTES);\r
+ log.info("Collecting cluster statistics ");\r
+ }\r
+ if (collectLocalStats()) {\r
+ ExecutionStatCollector localCollector = new ExecutionStatCollector(\r
+ localWorkDir, localMaxRuntime);\r
+ localcf = executor.scheduleAtFixedRate(localCollector, 10, 24 * 60,\r
+ TimeUnit.MINUTES);\r
+ log.info("Collecting local statistics ");\r
+ }\r
+\r
+ }\r
+\r
+ static String getClusterJobDir() {\r
+ return getStringProperty(ph.getProperty("cluster.tmp.directory"));\r
+ }\r
+\r
+ static int getClusterJobTimeOut() {\r
+ int maxRunTime = 24 * 7;\r
+ String clusterMaxRuntime = ph.getProperty("cluster.stat.maxruntime");\r
+ if (clusterMaxRuntime != null) {\r
+ clusterMaxRuntime = clusterMaxRuntime.trim();\r
+ maxRunTime = Integer.parseInt(clusterMaxRuntime);\r
+ }\r
+ return maxRunTime;\r
+ }\r
+\r
+ static int getLocalJobTimeOut() {\r
+ int maxRunTime = 24;\r
+ String localMaxRuntime = ph.getProperty("local.stat.maxruntime");\r
+ if (localMaxRuntime != null) {\r
+ localMaxRuntime = localMaxRuntime.trim();\r
+ maxRunTime = Integer.parseInt(localMaxRuntime);\r
+ }\r
+\r
+ return maxRunTime;\r
+ }\r
+\r
+ static String getLocalJobDir() {\r
+ return getStringProperty(ph.getProperty("local.tmp.directory"));\r
+ }\r
+\r
+ private static String getStringProperty(String propName) {\r
+ String locdir = ph.getProperty(propName);\r
+ if (locdir != null) {\r
+ locdir = locdir.trim();\r
+ }\r
+ return locdir;\r
+ }\r
+\r
+ static boolean collectClusterStats() {\r
+ return getBooleanProperty(ph\r
+ .getProperty("cluster.stat.collector.enable"));\r
+\r
+ }\r
+\r
+ static boolean collectLocalStats() {\r
+ return getBooleanProperty(ph.getProperty("local.stat.collector.enable"));\r
+ }\r
\r
+ private static boolean getBooleanProperty(String propName) {\r
+ boolean enabled = false;\r
+ if (propName != null) {\r
+ propName = propName.trim();\r
+ enabled = Boolean.parseBoolean(propName);\r
+ }\r
+ return enabled;\r
}\r
\r
}\r