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
61 // TODO remove work out of the constructor Tomcat takes ages to
\r
63 ExecutionStatCollector clusterCollector = new ExecutionStatCollector(
\r
64 clusterWorkDir, clusterMaxRuntime);
\r
65 clustercf = executor.scheduleAtFixedRate(clusterCollector, 60,
\r
66 24 * 60, TimeUnit.MINUTES);
\r
67 // clustercf = executor.scheduleAtFixedRate(clusterCollector, 15,
\r
69 // TimeUnit.SECONDS);
\r
70 log.info("Collecting cluster statistics ");
\r
72 log.info("Cluster statistics collector is disabled or not configured! ");
\r
74 if (collectLocalStats()) {
\r
75 ExecutionStatCollector localCollector = new ExecutionStatCollector(
\r
76 localWorkDir, localMaxRuntime);
\r
77 // localcf = executor.scheduleAtFixedRate(localCollector, 100, 60,
\r
78 // TimeUnit.SECONDS);
\r
80 localcf = executor.scheduleAtFixedRate(localCollector, 10, 24 * 60,
\r
82 log.info("Collecting local statistics ");
\r
84 log.info("Local statistics collector is disabled or not configured! ");
\r
89 static String getClusterJobDir() {
\r
90 return getStringProperty(ph.getProperty("cluster.tmp.directory"));
\r
93 static int getClusterJobTimeOut() {
\r
94 int maxRunTime = 24 * 7;
\r
95 String clusterMaxRuntime = ph.getProperty("cluster.stat.maxruntime");
\r
96 if (clusterMaxRuntime != null) {
\r
97 clusterMaxRuntime = clusterMaxRuntime.trim();
\r
98 maxRunTime = Integer.parseInt(clusterMaxRuntime);
\r
103 static int getLocalJobTimeOut() {
\r
104 int maxRunTime = 24;
\r
105 String localMaxRuntime = ph.getProperty("local.stat.maxruntime");
\r
106 if (localMaxRuntime != null) {
\r
107 localMaxRuntime = localMaxRuntime.trim();
\r
108 maxRunTime = Integer.parseInt(localMaxRuntime);
\r
114 static String getLocalJobDir() {
\r
115 return getStringProperty(ph.getProperty("local.tmp.directory"));
\r
118 private static String getStringProperty(String propName) {
\r
119 if (propName != null) {
\r
120 propName = propName.trim();
\r
125 static boolean collectClusterStats() {
\r
126 return getBooleanProperty(ph
\r
127 .getProperty("cluster.stat.collector.enable"));
\r
130 static boolean collectLocalStats() {
\r
131 return getBooleanProperty(ph.getProperty("local.stat.collector.enable"));
\r
134 private static boolean getBooleanProperty(String propValue) {
\r
135 boolean enabled = false;
\r
136 if (!Util.isEmpty(propValue)) {
\r
137 propValue = propValue.trim();
\r
138 enabled = Boolean.parseBoolean(propValue);
\r