Further work to enable stat collection and display
[jabaws.git] / webservices / compbio / stat / servlet / StatisticCollector.java
1 package compbio.stat.servlet;\r
2 \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
7 \r
8 import javax.servlet.ServletContextEvent;\r
9 import javax.servlet.ServletContextListener;\r
10 \r
11 import org.apache.log4j.Logger;\r
12 \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
18 \r
19 public class StatisticCollector implements ServletContextListener {\r
20 \r
21         static PropertyHelper ph = PropertyHelperManager.getPropertyHelper();\r
22 \r
23         private final Logger log = Logger.getLogger(StatisticCollector.class);\r
24 \r
25         private ScheduledFuture<?> localcf;\r
26         private ScheduledFuture<?> clustercf;\r
27         private ScheduledExecutorService executor;\r
28 \r
29         @Override\r
30         public void contextDestroyed(ServletContextEvent arg0) {\r
31                 try {\r
32                         if (localcf != null) {\r
33                                 localcf.cancel(true);\r
34                         }\r
35                         if (clustercf != null) {\r
36                                 clustercf.cancel(true);\r
37                         }\r
38                         executor.shutdown();\r
39                         executor.awaitTermination(3, TimeUnit.SECONDS);\r
40                 } catch (InterruptedException e) {\r
41                         log.warn(e.getMessage(), e);\r
42                 } finally {\r
43                         StatDB.shutdownDBServer();\r
44                         executor.shutdownNow();\r
45                 }\r
46         }\r
47 \r
48         @Override\r
49         public void contextInitialized(ServletContextEvent arg0) {\r
50                 String clusterWorkDir = getClusterJobDir();\r
51                 int clusterMaxRuntime = getClusterJobTimeOut();\r
52 \r
53                 int localMaxRuntime = getLocalJobTimeOut();\r
54                 String localWorkDir = compbio.engine.client.Util\r
55                                 .convertToAbsolute(getLocalJobDir());\r
56 \r
57                 log.info("Initializing statistics collector");\r
58                 executor = Executors.newScheduledThreadPool(2);\r
59 \r
60                 if (collectClusterStats()) {\r
61                         // TODO remove work out of the constructor Tomcat takes ages to\r
62                         // start!\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
68                         // 30,\r
69                         // TimeUnit.SECONDS);\r
70                         log.info("Collecting cluster statistics ");\r
71                 } else {\r
72                         log.info("Cluster statistics collector is disabled or not configured! ");\r
73                 }\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
79 \r
80                         localcf = executor.scheduleAtFixedRate(localCollector, 10, 24 * 60,\r
81                                         TimeUnit.MINUTES);\r
82                         log.info("Collecting local statistics ");\r
83                 } else {\r
84                         log.info("Local statistics collector is disabled or not configured! ");\r
85                 }\r
86 \r
87         }\r
88 \r
89         static String getClusterJobDir() {\r
90                 return getStringProperty(ph.getProperty("cluster.tmp.directory"));\r
91         }\r
92 \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
99                 }\r
100                 return maxRunTime;\r
101         }\r
102 \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
109                 }\r
110 \r
111                 return maxRunTime;\r
112         }\r
113 \r
114         static String getLocalJobDir() {\r
115                 return getStringProperty(ph.getProperty("local.tmp.directory"));\r
116         }\r
117 \r
118         private static String getStringProperty(String propName) {\r
119                 if (propName != null) {\r
120                         propName = propName.trim();\r
121                 }\r
122                 return propName;\r
123         }\r
124 \r
125         static boolean collectClusterStats() {\r
126                 return getBooleanProperty(ph\r
127                                 .getProperty("cluster.stat.collector.enable"));\r
128         }\r
129 \r
130         static boolean collectLocalStats() {\r
131                 return getBooleanProperty(ph.getProperty("local.stat.collector.enable"));\r
132         }\r
133 \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
139                 }\r
140                 return enabled;\r
141         }\r
142 \r
143 }\r