1 /* Copyright (c) 2011 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0
\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
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
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
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
18 package compbio.stat.servlet;
\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
25 import javax.servlet.ServletContextEvent;
\r
26 import javax.servlet.ServletContextListener;
\r
28 import org.apache.log4j.Logger;
\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
36 public class StatisticCollector implements ServletContextListener {
\r
38 static PropertyHelper ph = PropertyHelperManager.getPropertyHelper();
\r
40 private final Logger log = Logger.getLogger(StatisticCollector.class);
\r
42 private ScheduledFuture<?> localcf;
\r
43 private ScheduledFuture<?> clustercf;
\r
44 private ScheduledExecutorService executor;
\r
47 public void contextDestroyed(ServletContextEvent arg0) {
\r
49 if (localcf != null) {
\r
50 localcf.cancel(true);
\r
52 if (clustercf != null) {
\r
53 clustercf.cancel(true);
\r
55 executor.shutdown();
\r
56 executor.awaitTermination(3, TimeUnit.SECONDS);
\r
57 } catch (InterruptedException e) {
\r
58 log.warn(e.getMessage(), e);
\r
60 StatDB.shutdownDBServer();
\r
61 executor.shutdownNow();
\r
66 public void contextInitialized(ServletContextEvent arg0) {
\r
67 String clusterWorkDir = getClusterJobDir();
\r
68 int clusterMaxRuntime = getClusterJobTimeOut();
\r
70 int localMaxRuntime = getLocalJobTimeOut();
\r
71 String localWorkDir = compbio.engine.client.Util
\r
72 .convertToAbsolute(getLocalJobDir());
\r
74 log.info("Initializing statistics collector");
\r
75 executor = Executors.newScheduledThreadPool(2);
\r
77 if (collectClusterStats()) {
\r
78 // collect statistics every minute. DEBUGGING ONLY!!!
\r
79 long CollectingFrequency = 1;
\r
80 // collect statistics every day. normal running...
\r
81 //CollectingFrequency = 24 * 60;
\r
83 ExecutionStatCollector clusterCollector = new ExecutionStatCollector(clusterWorkDir, clusterMaxRuntime);
\r
84 clustercf = executor.scheduleAtFixedRate(clusterCollector, 1, CollectingFrequency, TimeUnit.MINUTES);
\r
85 log.info("Collecting cluster statistics ");
\r
87 log.info("Cluster statistics collector is disabled or not configured! ");
\r
89 if (collectLocalStats()) {
\r
90 // collect statistics every minute. DEBUGGING ONLY!!!
\r
91 long CollectingFrequency = 1;
\r
92 // collect statistics every day. normal running...
\r
93 //CollectingFrequency = 24 * 60;
\r
95 ExecutionStatCollector localCollector = new ExecutionStatCollector( localWorkDir, localMaxRuntime);
\r
96 localcf = executor.scheduleAtFixedRate(localCollector, 1, CollectingFrequency, TimeUnit.MINUTES);
\r
97 log.info("Collecting local statistics every " + CollectingFrequency + " minutes");
\r
99 log.info("Local statistics collector is disabled or not configured! ");
\r
103 static String getClusterJobDir() {
\r
104 return getStringProperty(ph.getProperty("cluster.tmp.directory"));
\r
107 static int getClusterJobTimeOut() {
\r
108 int maxRunTime = 24 * 7;
\r
109 String clusterMaxRuntime = ph.getProperty("cluster.stat.maxruntime");
\r
110 if (clusterMaxRuntime != null) {
\r
111 clusterMaxRuntime = clusterMaxRuntime.trim();
\r
112 maxRunTime = Integer.parseInt(clusterMaxRuntime);
\r
117 static int getLocalJobTimeOut() {
\r
118 int maxRunTime = 24;
\r
119 String localMaxRuntime = ph.getProperty("local.stat.maxruntime");
\r
120 if (localMaxRuntime != null) {
\r
121 localMaxRuntime = localMaxRuntime.trim();
\r
122 maxRunTime = Integer.parseInt(localMaxRuntime);
\r
128 static String getLocalJobDir() {
\r
129 return getStringProperty(ph.getProperty("local.tmp.directory"));
\r
132 private static String getStringProperty(String propName) {
\r
133 if (propName != null) {
\r
134 propName = propName.trim();
\r
139 static boolean collectClusterStats() {
\r
140 return getBooleanProperty(ph
\r
141 .getProperty("cluster.stat.collector.enable"));
\r
144 static boolean collectLocalStats() {
\r
145 return getBooleanProperty(ph.getProperty("local.stat.collector.enable"));
\r
148 private static boolean getBooleanProperty(String propValue) {
\r
149 boolean enabled = false;
\r
150 if (!Util.isEmpty(propValue)) {
\r
151 propValue = propValue.trim();
\r
152 enabled = Boolean.parseBoolean(propValue);
\r