+/* Copyright (c) 2011 Peter Troshin\r
+ * \r
+ * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0 \r
+ * \r
+ * This library is free software; you can redistribute it and/or modify it under the terms of the\r
+ * Apache License version 2 as published by the Apache Software Foundation\r
+ * \r
+ * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
+ * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
+ * License for more details.\r
+ * \r
+ * A copy of the license is in apache_license.txt. It is also available here:\r
+ * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
+ * \r
+ * Any republication or derived work distributed in source code form\r
+ * must include this copyright and license notice.\r
+ */\r
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
+import compbio.util.Util;\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 = compbio.engine.client.Util\r
+ .convertToAbsolute(getLocalJobDir());\r
+\r
+ log.info("Initializing statistics collector");\r
+ executor = Executors.newScheduledThreadPool(2);\r
+\r
+ if (collectClusterStats()) {\r
+\r
+ ExecutionStatCollector clusterCollector = new ExecutionStatCollector(\r
+ clusterWorkDir, clusterMaxRuntime);\r
+ clustercf = executor.scheduleAtFixedRate(clusterCollector, 60,\r
+ 24 * 60, TimeUnit.MINUTES);\r
+ // clustercf = executor.scheduleAtFixedRate(clusterCollector, 15,\r
+ // 30,\r
+ // TimeUnit.SECONDS);\r
+ log.info("Collecting cluster statistics ");\r
+ } else {\r
+ log.info("Cluster statistics collector is disabled or not configured! ");\r
+ }\r
+ if (collectLocalStats()) {\r
+ ExecutionStatCollector localCollector = new ExecutionStatCollector(\r
+ localWorkDir, localMaxRuntime);\r
+ // localcf = executor.scheduleAtFixedRate(localCollector, 100, 60,\r
+ // TimeUnit.SECONDS);\r
+\r
+ localcf = executor.scheduleAtFixedRate(localCollector, 10, 24 * 60,\r
+ TimeUnit.MINUTES);\r
+ log.info("Collecting local statistics ");\r
+ } else {\r
+ log.info("Local statistics collector is disabled or not configured! ");\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
+ if (propName != null) {\r
+ propName = propName.trim();\r
+ }\r
+ return propName;\r
+ }\r
+\r
+ static boolean collectClusterStats() {\r
+ return getBooleanProperty(ph\r
+ .getProperty("cluster.stat.collector.enable"));\r
+ }\r
+\r
+ static boolean collectLocalStats() {\r
+ return getBooleanProperty(ph.getProperty("local.stat.collector.enable"));\r
+ }\r
\r
+ private static boolean getBooleanProperty(String propValue) {\r
+ boolean enabled = false;\r
+ if (!Util.isEmpty(propValue)) {\r
+ propValue = propValue.trim();\r
+ enabled = Boolean.parseBoolean(propValue);\r
+ }\r
+ return enabled;\r
}\r
\r
}\r