0fadfb57aa2e3e58c0a4c3564db8c7567ffa462d
[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 \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
67                         // 30,\r
68                         // TimeUnit.SECONDS);\r
69                         log.info("Collecting cluster statistics ");\r
70                 } else {\r
71                         log.info("Cluster statistics collector is disabled or not configured! ");\r
72                 }\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
78 \r
79                         localcf = executor.scheduleAtFixedRate(localCollector, 10, 24 * 60,\r
80                                         TimeUnit.MINUTES);\r
81                         log.info("Collecting local statistics ");\r
82                 } else {\r
83                         log.info("Local statistics collector is disabled or not configured! ");\r
84                 }\r
85         }\r
86 \r
87         static String getClusterJobDir() {\r
88                 return getStringProperty(ph.getProperty("cluster.tmp.directory"));\r
89         }\r
90 \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
97                 }\r
98                 return maxRunTime;\r
99         }\r
100 \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
107                 }\r
108 \r
109                 return maxRunTime;\r
110         }\r
111 \r
112         static String getLocalJobDir() {\r
113                 return getStringProperty(ph.getProperty("local.tmp.directory"));\r
114         }\r
115 \r
116         private static String getStringProperty(String propName) {\r
117                 if (propName != null) {\r
118                         propName = propName.trim();\r
119                 }\r
120                 return propName;\r
121         }\r
122 \r
123         static boolean collectClusterStats() {\r
124                 return getBooleanProperty(ph\r
125                                 .getProperty("cluster.stat.collector.enable"));\r
126         }\r
127 \r
128         static boolean collectLocalStats() {\r
129                 return getBooleanProperty(ph.getProperty("local.stat.collector.enable"));\r
130         }\r
131 \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
137                 }\r
138                 return enabled;\r
139         }\r
140 \r
141 }\r