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
17 import compbio.util.Util;
\r
19 public class StatisticCollector implements ServletContextListener {
\r
21 static PropertyHelper ph = PropertyHelperManager.getPropertyHelper();
\r
23 private final Logger log = Logger.getLogger(StatisticCollector.class);
\r
25 private ScheduledFuture<?> localcf;
\r
26 private ScheduledFuture<?> clustercf;
\r
27 private ScheduledExecutorService executor;
\r
30 public void contextDestroyed(ServletContextEvent arg0) {
\r
32 if (localcf != null) {
\r
33 localcf.cancel(true);
\r
35 if (clustercf != null) {
\r
36 clustercf.cancel(true);
\r
38 executor.shutdown();
\r
39 executor.awaitTermination(3, TimeUnit.SECONDS);
\r
40 } catch (InterruptedException e) {
\r
41 log.warn(e.getMessage(), e);
\r
43 StatDB.shutdownDBServer();
\r
44 executor.shutdownNow();
\r
49 public void contextInitialized(ServletContextEvent arg0) {
\r
50 String clusterWorkDir = getClusterJobDir();
\r
51 int clusterMaxRuntime = getClusterJobTimeOut();
\r
53 int localMaxRuntime = getLocalJobTimeOut();
\r
54 String localWorkDir = compbio.engine.client.Util
\r
55 .convertToAbsolute(getLocalJobDir());
\r
57 log.info("Initializing statistics collector");
\r
58 executor = Executors.newScheduledThreadPool(2);
\r
60 if (collectClusterStats()) {
\r
62 ExecutionStatCollector clusterCollector = new ExecutionStatCollector(
\r
63 clusterWorkDir, clusterMaxRuntime);
\r
64 clustercf = executor.scheduleAtFixedRate(clusterCollector, 60,
\r
65 24 * 60, TimeUnit.MINUTES);
\r
66 // clustercf = executor.scheduleAtFixedRate(clusterCollector, 15,
\r
68 // TimeUnit.SECONDS);
\r
69 log.info("Collecting cluster statistics ");
\r
71 log.info("Cluster statistics collector is disabled or not configured! ");
\r
73 if (collectLocalStats()) {
\r
74 ExecutionStatCollector localCollector = new ExecutionStatCollector(
\r
75 localWorkDir, localMaxRuntime);
\r
76 // localcf = executor.scheduleAtFixedRate(localCollector, 100, 60,
\r
77 // TimeUnit.SECONDS);
\r
79 localcf = executor.scheduleAtFixedRate(localCollector, 10, 24 * 60,
\r
81 log.info("Collecting local statistics ");
\r
83 log.info("Local statistics collector is disabled or not configured! ");
\r
87 static String getClusterJobDir() {
\r
88 return getStringProperty(ph.getProperty("cluster.tmp.directory"));
\r
91 static int getClusterJobTimeOut() {
\r
92 int maxRunTime = 24 * 7;
\r
93 String clusterMaxRuntime = ph.getProperty("cluster.stat.maxruntime");
\r
94 if (clusterMaxRuntime != null) {
\r
95 clusterMaxRuntime = clusterMaxRuntime.trim();
\r
96 maxRunTime = Integer.parseInt(clusterMaxRuntime);
\r
101 static int getLocalJobTimeOut() {
\r
102 int maxRunTime = 24;
\r
103 String localMaxRuntime = ph.getProperty("local.stat.maxruntime");
\r
104 if (localMaxRuntime != null) {
\r
105 localMaxRuntime = localMaxRuntime.trim();
\r
106 maxRunTime = Integer.parseInt(localMaxRuntime);
\r
112 static String getLocalJobDir() {
\r
113 return getStringProperty(ph.getProperty("local.tmp.directory"));
\r
116 private static String getStringProperty(String propName) {
\r
117 if (propName != null) {
\r
118 propName = propName.trim();
\r
123 static boolean collectClusterStats() {
\r
124 return getBooleanProperty(ph
\r
125 .getProperty("cluster.stat.collector.enable"));
\r
128 static boolean collectLocalStats() {
\r
129 return getBooleanProperty(ph.getProperty("local.stat.collector.enable"));
\r
132 private static boolean getBooleanProperty(String propValue) {
\r
133 boolean enabled = false;
\r
134 if (!Util.isEmpty(propValue)) {
\r
135 propValue = propValue.trim();
\r
136 enabled = Boolean.parseBoolean(propValue);
\r