Further work on statistics display - improvements to stat collector
[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 \r
18 public class StatisticCollector implements ServletContextListener {\r
19 \r
20         static PropertyHelper ph = PropertyHelperManager.getPropertyHelper();\r
21 \r
22         private final Logger log = Logger.getLogger(StatisticCollector.class);\r
23 \r
24         private ScheduledFuture<?> localcf;\r
25         private ScheduledFuture<?> clustercf;\r
26         private ScheduledExecutorService executor;\r
27 \r
28         @Override\r
29         public void contextDestroyed(ServletContextEvent arg0) {\r
30                 try {\r
31                         if (localcf != null) {\r
32                                 localcf.cancel(true);\r
33                         }\r
34                         if (clustercf != null) {\r
35                                 clustercf.cancel(true);\r
36                         }\r
37                         executor.shutdown();\r
38                         executor.awaitTermination(3, TimeUnit.SECONDS);\r
39                 } catch (InterruptedException e) {\r
40                         log.warn(e.getMessage(), e);\r
41                 } finally {\r
42                         StatDB.shutdownDBServer();\r
43                         executor.shutdownNow();\r
44                 }\r
45         }\r
46 \r
47         @Override\r
48         public void contextInitialized(ServletContextEvent arg0) {\r
49                 String clusterWorkDir = getClusterJobDir();\r
50                 int clusterMaxRuntime = getClusterJobTimeOut();\r
51 \r
52                 int localMaxRuntime = getLocalJobTimeOut();\r
53                 String localWorkDir = getLocalJobDir();\r
54 \r
55                 log.info("Initializing statistics collector");\r
56                 executor = Executors.newScheduledThreadPool(1);\r
57 \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
64                 }\r
65                 if (collectLocalStats()) {\r
66                         ExecutionStatCollector localCollector = new ExecutionStatCollector(\r
67                                         localWorkDir, localMaxRuntime);\r
68                         localcf = executor.scheduleAtFixedRate(localCollector, 10, 24 * 60,\r
69                                         TimeUnit.MINUTES);\r
70                         log.info("Collecting local statistics ");\r
71                 }\r
72 \r
73         }\r
74 \r
75         static String getClusterJobDir() {\r
76                 return getStringProperty(ph.getProperty("cluster.tmp.directory"));\r
77         }\r
78 \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
85                 }\r
86                 return maxRunTime;\r
87         }\r
88 \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
95                 }\r
96 \r
97                 return maxRunTime;\r
98         }\r
99 \r
100         static String getLocalJobDir() {\r
101                 return getStringProperty(ph.getProperty("local.tmp.directory"));\r
102         }\r
103 \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
108                 }\r
109                 return locdir;\r
110         }\r
111 \r
112         static boolean collectClusterStats() {\r
113                 return getBooleanProperty(ph\r
114                                 .getProperty("cluster.stat.collector.enable"));\r
115 \r
116         }\r
117 \r
118         static boolean collectLocalStats() {\r
119                 return getBooleanProperty(ph.getProperty("local.stat.collector.enable"));\r
120         }\r
121 \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
127                 }\r
128                 return enabled;\r
129         }\r
130 \r
131 }\r