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.ws.server;
\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.stat.collector.DirCleaner;
\r
31 import compbio.stat.collector.StatDB;
\r
32 import compbio.engine.conf.PropertyHelperManager;
\r
33 import compbio.engine.local.ExecutableWrapper;
\r
34 import compbio.engine.local.LocalExecutorService;
\r
35 import compbio.util.PropertyHelper;
\r
36 import compbio.util.Util;
\r
40 * 1. Switch off engines if JABAWS web application is un-deployed, or web server is shutdown
\r
41 * 2. delete old job directories
\r
43 * @author Peter Troshin
\r
44 * @author Alexander Sherstnev
\r
47 public class MainManager implements ServletContextListener {
\r
49 private final Logger log = Logger.getLogger(MainManager.class);
\r
50 static PropertyHelper ph = PropertyHelperManager.getPropertyHelper();
\r
52 private ScheduledFuture<?> localcl;
\r
53 private ScheduledFuture<?> clustercl;
\r
54 private ScheduledExecutorService executor;
\r
57 public void contextDestroyed(ServletContextEvent ignored) {
\r
58 // stop cleaning job directories
\r
60 if (null != localcl) {
\r
61 localcl.cancel(true);
\r
63 if (null != clustercl) {
\r
64 clustercl.cancel(true);
\r
66 //executor.shutdown();
\r
67 //executor.awaitTermination(3, TimeUnit.SECONDS);
\r
68 // } catch (InterruptedException e) {
\r
69 // log.warn(e.getMessage(), e);
\r
71 // Shutdown local engine
\r
72 log.info("JABAWS context is destroyed. Shutting down engines...");
\r
73 LocalExecutorService.shutDown();
\r
74 log.info("Local engine is shutdown OK");
\r
75 ExecutableWrapper.shutdownService();
\r
76 log.info("Individual executables stream engine is shutdown OK");
\r
80 public void contextInitialized(ServletContextEvent arg0) {
\r
81 log.info("Initializing directory cleaners");
\r
82 executor = Executors.newScheduledThreadPool(2);
\r
84 // configure cluster cleaner
\r
85 String clusterWorkDir = getClusterJobDir();
\r
86 int clusterDirLifespan = PropertyHelperManager.getIntProperty(ph.getProperty("cluster.jobdir.maxlifespan"));
\r
87 int clusterCleaningRate = PropertyHelperManager.getIntProperty(ph.getProperty("cluster.jobdir.cleaning.frequency"));
\r
88 boolean cleanClasterDir = PropertyHelperManager.getBooleanProperty(ph.getProperty("cluster.stat.collector.enable"));
\r
90 if (0 < clusterDirLifespan && cleanClasterDir) {
\r
91 DirCleaner clusterDirCleaner = new DirCleaner( clusterWorkDir, clusterDirLifespan);
\r
92 clustercl = executor.scheduleAtFixedRate(clusterDirCleaner, 1, clusterCleaningRate, TimeUnit.MINUTES);
\r
93 log.info("Cleaning local job directory every " + clusterCleaningRate + " minutes");
\r
95 log.info("Cluster job directory cleaner is disabled. ");
\r
98 // configure local cleaner
\r
99 String localWorkDir = compbio.engine.client.Util.convertToAbsolute(getLocalJobDir());
\r
100 int localDirLiveSpan = PropertyHelperManager.getIntProperty(ph.getProperty("local.jobdir.maxlifespan"));
\r
101 int localCleaningRate = PropertyHelperManager.getIntProperty(ph.getProperty("local.jobdir.cleaning.frequency"));
\r
102 boolean cleanLocalDir = PropertyHelperManager.getBooleanProperty(ph.getProperty("local.stat.collector.enable"));
\r
104 if (0 < localDirLiveSpan && cleanLocalDir) {
\r
105 DirCleaner localDirCleaner = new DirCleaner( localWorkDir, localDirLiveSpan);
\r
106 localcl = executor.scheduleAtFixedRate(localDirCleaner, 1, localCleaningRate, TimeUnit.MINUTES);
\r
107 log.info("Cleaning local job directory every " + localCleaningRate + " minutes");
\r
109 log.info("Local job directory cleaner is disabled. ");
\r
113 static String getClusterJobDir() {
\r
114 String ln = ph.getProperty("cluster.tmp.directory");
\r
121 static String getLocalJobDir() {
\r
122 String ln = ph.getProperty("local.tmp.directory");
\r