Delete old job directory automatically
[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.stat.collector.ExecutionStatCollector;\r
32 import compbio.stat.collector.StatDB;\r
33 import compbio.util.PropertyHelper;\r
34 import compbio.util.Util;\r
35 \r
36 public class StatisticCollector implements ServletContextListener {\r
37 \r
38         static PropertyHelper ph = PropertyHelperManager.getPropertyHelper();\r
39 \r
40         private final Logger log = Logger.getLogger(StatisticCollector.class);\r
41 \r
42         private ScheduledFuture<?> localcf;\r
43         private ScheduledFuture<?> clustercf;\r
44         private ScheduledExecutorService executor;\r
45 \r
46         @Override\r
47         public void contextDestroyed(ServletContextEvent arg0) {\r
48                 try {\r
49                         if (localcf != null) {\r
50                                 localcf.cancel(true);\r
51                         }\r
52                         if (clustercf != null) {\r
53                                 clustercf.cancel(true);\r
54                         }\r
55                         executor.shutdown();\r
56                         executor.awaitTermination(3, TimeUnit.SECONDS);\r
57                 } catch (InterruptedException e) {\r
58                         log.warn(e.getMessage(), e);\r
59                 } finally {\r
60                         StatDB.shutdownDBServer();\r
61                         executor.shutdownNow();\r
62                 }\r
63         }\r
64 \r
65         @Override\r
66         public void contextInitialized(ServletContextEvent arg0) {\r
67                 String clusterWorkDir = getClusterJobDir();\r
68                 int clusterMaxRuntime = getClusterJobTimeOut();\r
69 \r
70                 int localMaxRuntime = getLocalJobTimeOut();\r
71                 String localWorkDir = compbio.engine.client.Util.convertToAbsolute(getLocalJobDir());\r
72 \r
73                 log.info("Initializing statistics collectors");\r
74                 executor = Executors.newScheduledThreadPool(2);\r
75 \r
76                 if (collectClusterStats()) {\r
77                         // collect statistics with this frequency\r
78                         long CollectingFrequency = updateClusterStatsFrequency();\r
79                         // CollectingFrequency = 0 if the parameter is not found\r
80                         if (0 == CollectingFrequency) {\r
81                                 CollectingFrequency = 1;\r
82                         }\r
83 \r
84                         ExecutionStatCollector clusterCollector = new ExecutionStatCollector(clusterWorkDir, clusterMaxRuntime);\r
85                         clustercf = executor.scheduleAtFixedRate(clusterCollector, 30, 60 * CollectingFrequency, TimeUnit.SECONDS);\r
86                         log.info("Collecting cluster statistics every " + CollectingFrequency + " minutes");\r
87                 } else {\r
88                         log.info("Cluster statistics collector is disabled or not configured! ");\r
89                 }\r
90                 if (collectLocalStats()) {\r
91                         // collect statistics with this frequency\r
92                         long CollectingFrequency = updateLocalStatsFrequency();\r
93                         // CollectingFrequency = 0 if the parameter is not found\r
94                         if (0 == CollectingFrequency) {\r
95                                 CollectingFrequency = 1;\r
96                         }\r
97 \r
98                         ExecutionStatCollector localCollector = new ExecutionStatCollector(     localWorkDir, localMaxRuntime);\r
99                         localcf = executor.scheduleAtFixedRate(localCollector, 30, 60 * CollectingFrequency, TimeUnit.SECONDS);\r
100                         log.info("Collecting local statistics every " + CollectingFrequency + " minutes");\r
101                 } else {\r
102                         log.info("Local statistics collector is disabled or not configured! ");\r
103                 }\r
104         }\r
105 \r
106         static String getClusterJobDir() {\r
107                 return getStringProperty(ph.getProperty("cluster.tmp.directory"));\r
108         }\r
109 \r
110         static int getClusterJobTimeOut() {\r
111                 int maxRunTime = 24 * 7;\r
112                 String clusterMaxRuntime = ph.getProperty("cluster.stat.maxruntime");\r
113                 if (clusterMaxRuntime != null) {\r
114                         clusterMaxRuntime = clusterMaxRuntime.trim();\r
115                         maxRunTime = Integer.parseInt(clusterMaxRuntime);\r
116                 }\r
117                 return maxRunTime;\r
118         }\r
119 \r
120         static int getLocalJobTimeOut() {\r
121                 int maxRunTime = 24;\r
122                 String localMaxRuntime = ph.getProperty("local.stat.maxruntime");\r
123                 if (localMaxRuntime != null) {\r
124                         localMaxRuntime = localMaxRuntime.trim();\r
125                         maxRunTime = Integer.parseInt(localMaxRuntime);\r
126                 }\r
127 \r
128                 return maxRunTime;\r
129         }\r
130 \r
131         static String getLocalJobDir() {\r
132                 return getStringProperty(ph.getProperty("local.tmp.directory"));\r
133         }\r
134 \r
135         private static String getStringProperty(String propName) {\r
136                 if (propName != null) {\r
137                         propName = propName.trim();\r
138                 }\r
139                 return propName;\r
140         }\r
141 \r
142         private static int getIntProperty(String propValue) {\r
143                 int value = 0;\r
144                 if (!Util.isEmpty(propValue)) {\r
145                         propValue = propValue.trim();\r
146                         value = Integer.parseInt(propValue);\r
147                 }\r
148                 return value;\r
149         }\r
150 \r
151         \r
152         static boolean collectClusterStats() {\r
153                 return getBooleanProperty(ph\r
154                                 .getProperty("cluster.stat.collector.enable"));\r
155         }\r
156 \r
157         static boolean collectLocalStats() {\r
158                 return getBooleanProperty(ph.getProperty("local.stat.collector.enable"));\r
159         }\r
160 \r
161         static int updateClusterStatsFrequency() {\r
162                 return getIntProperty(ph\r
163                                 .getProperty("cluster.stat.collector.update.frequency"));\r
164         }\r
165 \r
166         static int updateLocalStatsFrequency() {\r
167                 return getIntProperty(ph.getProperty("local.stat.collector.update.frequency"));\r
168         }\r
169 \r
170         \r
171         private static boolean getBooleanProperty(String propValue) {\r
172                 boolean enabled = false;\r
173                 if (!Util.isEmpty(propValue)) {\r
174                         propValue = propValue.trim();\r
175                         enabled = Boolean.parseBoolean(propValue);\r
176                 }\r
177                 return enabled;\r
178         }\r
179 \r
180 }\r