1 package compbio.stat.servlet;
\r
3 import java.util.concurrent.Executors;
\r
4 import java.util.concurrent.ScheduledExecutorService;
\r
5 import java.util.concurrent.ScheduledFuture;
\r
6 import java.util.concurrent.TimeUnit;
\r
8 import javax.servlet.ServletContextEvent;
\r
9 import javax.servlet.ServletContextListener;
\r
11 import org.apache.log4j.Logger;
\r
13 import compbio.engine.conf.PropertyHelperManager;
\r
14 import compbio.stat.collector.ExecutionStatCollector;
\r
15 import compbio.stat.collector.StatDB;
\r
16 import compbio.util.PropertyHelper;
\r
18 public class StatisticCollector implements ServletContextListener {
\r
20 static PropertyHelper ph = PropertyHelperManager.getPropertyHelper();
\r
22 private final Logger log = Logger.getLogger(StatisticCollector.class);
\r
24 private ScheduledFuture<?> localcf;
\r
25 private ScheduledFuture<?> clustercf;
\r
26 private ScheduledExecutorService executor;
\r
29 public void contextDestroyed(ServletContextEvent arg0) {
\r
31 if (localcf != null) {
\r
32 localcf.cancel(true);
\r
34 if (clustercf != null) {
\r
35 clustercf.cancel(true);
\r
37 executor.shutdown();
\r
38 executor.awaitTermination(3, TimeUnit.SECONDS);
\r
39 } catch (InterruptedException e) {
\r
40 log.warn(e.getMessage(), e);
\r
42 StatDB.shutdownDBServer();
\r
43 executor.shutdownNow();
\r
48 public void contextInitialized(ServletContextEvent arg0) {
\r
49 String clusterWorkDir = getClusterJobDir();
\r
50 int clusterMaxRuntime = getClusterJobTimeOut();
\r
52 int localMaxRuntime = getLocalJobTimeOut();
\r
53 String localWorkDir = getLocalJobDir();
\r
55 log.info("Initializing statistics collector");
\r
56 executor = Executors.newScheduledThreadPool(1);
\r
58 if (collectClusterStats()) {
\r
59 ExecutionStatCollector clusterCollector = new ExecutionStatCollector(
\r
60 clusterWorkDir, clusterMaxRuntime);
\r
61 clustercf = executor.scheduleAtFixedRate(clusterCollector, 60,
\r
62 24 * 60, TimeUnit.MINUTES);
\r
63 log.info("Collecting cluster statistics ");
\r
65 if (collectLocalStats()) {
\r
66 ExecutionStatCollector localCollector = new ExecutionStatCollector(
\r
67 localWorkDir, localMaxRuntime);
\r
68 localcf = executor.scheduleAtFixedRate(localCollector, 10, 24 * 60,
\r
70 log.info("Collecting local statistics ");
\r
75 static String getClusterJobDir() {
\r
76 return getStringProperty(ph.getProperty("cluster.tmp.directory"));
\r
79 static int getClusterJobTimeOut() {
\r
80 int maxRunTime = 24 * 7;
\r
81 String clusterMaxRuntime = ph.getProperty("cluster.stat.maxruntime");
\r
82 if (clusterMaxRuntime != null) {
\r
83 clusterMaxRuntime = clusterMaxRuntime.trim();
\r
84 maxRunTime = Integer.parseInt(clusterMaxRuntime);
\r
89 static int getLocalJobTimeOut() {
\r
90 int maxRunTime = 24;
\r
91 String localMaxRuntime = ph.getProperty("local.stat.maxruntime");
\r
92 if (localMaxRuntime != null) {
\r
93 localMaxRuntime = localMaxRuntime.trim();
\r
94 maxRunTime = Integer.parseInt(localMaxRuntime);
\r
100 static String getLocalJobDir() {
\r
101 return getStringProperty(ph.getProperty("local.tmp.directory"));
\r
104 private static String getStringProperty(String propName) {
\r
105 String locdir = ph.getProperty(propName);
\r
106 if (locdir != null) {
\r
107 locdir = locdir.trim();
\r
112 static boolean collectClusterStats() {
\r
113 return getBooleanProperty(ph
\r
114 .getProperty("cluster.stat.collector.enable"));
\r
118 static boolean collectLocalStats() {
\r
119 return getBooleanProperty(ph.getProperty("local.stat.collector.enable"));
\r
122 private static boolean getBooleanProperty(String propName) {
\r
123 boolean enabled = false;
\r
124 if (propName != null) {
\r
125 propName = propName.trim();
\r
126 enabled = Boolean.parseBoolean(propName);
\r