e305fa5212012382301e3cdee1522b2e2aa6fbef
[jabaws.git] / webservices / compbio / stat / servlet / StatisticCollector.java
1 /* Copyright (c) 2011 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0     \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 package compbio.stat.servlet;\r
19 \r
20 import java.util.concurrent.Executors;\r
21 import java.util.concurrent.ScheduledExecutorService;\r
22 import java.util.concurrent.ScheduledFuture;\r
23 import java.util.concurrent.TimeUnit;\r
24 \r
25 import javax.servlet.ServletContextEvent;\r
26 import javax.servlet.ServletContextListener;\r
27 \r
28 import org.apache.log4j.Logger;\r
29 \r
30 import compbio.engine.conf.PropertyHelperManager;\r
31 import compbio.engine.client.EngineUtil;\r
32 import compbio.stat.collector.ExecutionStatCollector;\r
33 import compbio.stat.collector.StatDB;\r
34 import compbio.util.PropertyHelper;\r
35 import compbio.util.Util;\r
36 \r
37 public class StatisticCollector implements ServletContextListener {\r
38 \r
39         static PropertyHelper ph = PropertyHelperManager.getPropertyHelper();\r
40 \r
41         private final Logger log = Logger.getLogger(StatisticCollector.class);\r
42 \r
43         private ScheduledFuture<?> localcf;\r
44         private ScheduledFuture<?> clustercf;\r
45         private ScheduledExecutorService executor;\r
46 \r
47         @Override\r
48         public void contextDestroyed(ServletContextEvent arg0) {\r
49                 try {\r
50                         if (localcf != null) {\r
51                                 localcf.cancel(true);\r
52                         }\r
53                         if (clustercf != null) {\r
54                                 clustercf.cancel(true);\r
55                         }\r
56                         executor.shutdown();\r
57                         executor.awaitTermination(3, TimeUnit.SECONDS);\r
58                 } catch (InterruptedException e) {\r
59                         log.warn(e.getMessage(), e);\r
60                 } finally {\r
61                         StatDB.shutdownDBServer();\r
62                         executor.shutdownNow();\r
63                 }\r
64         }\r
65 \r
66         @Override\r
67         public void contextInitialized(ServletContextEvent arg0) {\r
68                 String clusterWorkDir = getClusterJobDir();\r
69                 int clusterMaxRuntime = getClusterJobTimeOut();\r
70 \r
71                 int localMaxRuntime = getLocalJobTimeOut();\r
72                 String localWorkDir = EngineUtil.convertToAbsolute(getLocalJobDir());\r
73 \r
74                 log.info("Initializing statistics collectors");\r
75                 executor = Executors.newScheduledThreadPool(2);\r
76 \r
77                 if (collectClusterStats()) {\r
78                         // collect statistics with this frequency\r
79                         long CollectingFrequency = updateClusterStatsFrequency();\r
80                         // CollectingFrequency = 0 if the parameter is not found\r
81                         if (0 == CollectingFrequency) {\r
82                                 CollectingFrequency = 1;\r
83                         }\r
84 \r
85                         ExecutionStatCollector clusterCollector = new ExecutionStatCollector(clusterWorkDir, clusterMaxRuntime);\r
86                         clustercf = executor.scheduleAtFixedRate(clusterCollector, 30, 60 * CollectingFrequency, TimeUnit.SECONDS);\r
87                         log.info("Collecting cluster statistics every " + CollectingFrequency + " minutes");\r
88                 } else {\r
89                         log.info("Cluster statistics collector is disabled or not configured! ");\r
90                 }\r
91                 if (collectLocalStats()) {\r
92                         // collect statistics with this frequency\r
93                         long CollectingFrequency = updateLocalStatsFrequency();\r
94                         // CollectingFrequency = 0 if the parameter is not found\r
95                         if (0 == CollectingFrequency) {\r
96                                 CollectingFrequency = 1;\r
97                         }\r
98 \r
99                         ExecutionStatCollector localCollector = new ExecutionStatCollector(     localWorkDir, localMaxRuntime);\r
100                         localcf = executor.scheduleAtFixedRate(localCollector, 30, 60 * CollectingFrequency, TimeUnit.SECONDS);\r
101                         log.info("Collecting local statistics every " + CollectingFrequency + " minutes");\r
102                 } else {\r
103                         log.info("Local statistics collector is disabled or not configured! ");\r
104                 }\r
105         }\r
106 \r
107         static String getClusterJobDir() {\r
108                 return getStringProperty(ph.getProperty("cluster.tmp.directory"));\r
109         }\r
110 \r
111         static int getClusterJobTimeOut() {\r
112                 int maxRunTime = 24 * 7;\r
113                 String clusterMaxRuntime = ph.getProperty("cluster.stat.maxruntime");\r
114                 if (clusterMaxRuntime != null) {\r
115                         clusterMaxRuntime = clusterMaxRuntime.trim();\r
116                         maxRunTime = Integer.parseInt(clusterMaxRuntime);\r
117                 }\r
118                 return maxRunTime;\r
119         }\r
120 \r
121         static int getLocalJobTimeOut() {\r
122                 int maxRunTime = 24;\r
123                 String localMaxRuntime = ph.getProperty("local.stat.maxruntime");\r
124                 if (localMaxRuntime != null) {\r
125                         localMaxRuntime = localMaxRuntime.trim();\r
126                         maxRunTime = Integer.parseInt(localMaxRuntime);\r
127                 }\r
128 \r
129                 return maxRunTime;\r
130         }\r
131 \r
132         static String getLocalJobDir() {\r
133                 return getStringProperty(ph.getProperty("local.tmp.directory"));\r
134         }\r
135 \r
136         private static String getStringProperty(String propName) {\r
137                 if (propName != null) {\r
138                         propName = propName.trim();\r
139                 }\r
140                 return propName;\r
141         }\r
142 \r
143         private static int getIntProperty(String propValue) {\r
144                 int value = 0;\r
145                 if (!Util.isEmpty(propValue)) {\r
146                         propValue = propValue.trim();\r
147                         value = Integer.parseInt(propValue);\r
148                 }\r
149                 return value;\r
150         }\r
151 \r
152         \r
153         static boolean collectClusterStats() {\r
154                 return getBooleanProperty(ph\r
155                                 .getProperty("cluster.stat.collector.enable"));\r
156         }\r
157 \r
158         static boolean collectLocalStats() {\r
159                 return getBooleanProperty(ph.getProperty("local.stat.collector.enable"));\r
160         }\r
161 \r
162         static int updateClusterStatsFrequency() {\r
163                 return getIntProperty(ph\r
164                                 .getProperty("cluster.stat.collector.update.frequency"));\r
165         }\r
166 \r
167         static int updateLocalStatsFrequency() {\r
168                 return getIntProperty(ph.getProperty("local.stat.collector.update.frequency"));\r
169         }\r
170 \r
171         \r
172         private static boolean getBooleanProperty(String propValue) {\r
173                 boolean enabled = false;\r
174                 if (!Util.isEmpty(propValue)) {\r
175                         propValue = propValue.trim();\r
176                         enabled = Boolean.parseBoolean(propValue);\r
177                 }\r
178                 return enabled;\r
179         }\r
180 \r
181 }\r