merge JABAWS_Release_2_1 into develop
[jabaws.git] / webservices / compbio / ws / server / MainManager.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.ws.server;\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.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 \r
37 /**\r
38  * Two tasks:\r
39  * 1. Switch off engines if JABAWS web application is un-deployed, or web server is shutdown\r
40  * 2. delete old job directories\r
41  * \r
42  * @author Peter Troshin\r
43  * @author Alexander Sherstnev\r
44  * @version 2.0\r
45  */\r
46 public class MainManager implements ServletContextListener {\r
47 \r
48         private final Logger log = Logger.getLogger(MainManager.class);\r
49         static PropertyHelper ph = PropertyHelperManager.getPropertyHelper();\r
50         \r
51         private ScheduledFuture<?> localcl;\r
52         private ScheduledFuture<?> clustercl;\r
53         private ScheduledExecutorService executor;\r
54         \r
55         @Override\r
56         public void contextDestroyed(ServletContextEvent ignored) {\r
57                 // stop cleaning job directories\r
58                 try {\r
59                         if (null != localcl) {\r
60                                 localcl.cancel(true);\r
61                         }\r
62                         if (null != clustercl) {\r
63                                 clustercl.cancel(true);\r
64                         }\r
65                         executor.shutdown();\r
66                         executor.awaitTermination(3, TimeUnit.SECONDS);\r
67                 } catch (InterruptedException e) {\r
68                         log.warn(e.getMessage(), e);\r
69                 }\r
70 \r
71                         // Shutdown local and cluster engines\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
77                         StatDB.shutdownDBServer();\r
78         }\r
79 \r
80         @Override\r
81         public void contextInitialized(ServletContextEvent arg0) {\r
82                 log.info("Initializing directory cleaners");\r
83                 executor = Executors.newScheduledThreadPool(2);\r
84 \r
85                 // configure cluster cleaner\r
86                 String clusterWorkDir = getClusterJobDir();\r
87                 int clusterDirLifespan = PropertyHelperManager.getIntProperty(ph.getProperty("cluster.jobdir.maxlifespan"));\r
88                 int clusterCleaningRate = PropertyHelperManager.getIntProperty(ph.getProperty("cluster.jobdir.cleaning.frequency"));\r
89                 boolean cleanClasterDir = PropertyHelperManager.getBooleanProperty(ph.getProperty("cluster.stat.collector.enable"));\r
90                 \r
91                 if (0 < clusterDirLifespan && cleanClasterDir) {\r
92                         DirCleaner clusterDirCleaner = new DirCleaner(clusterWorkDir, clusterDirLifespan);\r
93                         clustercl = executor.scheduleAtFixedRate(clusterDirCleaner, 1, clusterCleaningRate, TimeUnit.MINUTES);\r
94                         log.info("Cleaning local job directory every " + clusterCleaningRate + " minutes");\r
95                 } else {\r
96                         log.info("Cluster job directory cleaner is disabled. ");\r
97                 }\r
98 \r
99                 // configure local cleaner\r
100                 String localWorkDir = compbio.engine.client.Util.convertToAbsolute(getLocalJobDir());\r
101                 int localDirLiveSpan = PropertyHelperManager.getIntProperty(ph.getProperty("local.jobdir.maxlifespan"));\r
102                 int localCleaningRate = PropertyHelperManager.getIntProperty(ph.getProperty("local.jobdir.cleaning.frequency"));\r
103                 boolean cleanLocalDir = PropertyHelperManager.getBooleanProperty(ph.getProperty("local.stat.collector.enable"));\r
104 \r
105                 if (0 < localDirLiveSpan && cleanLocalDir) {\r
106                         DirCleaner localDirCleaner = new DirCleaner(localWorkDir, localDirLiveSpan);\r
107                         localcl = executor.scheduleAtFixedRate(localDirCleaner, 1, localCleaningRate, TimeUnit.MINUTES);\r
108                         log.info("Cleaning local job directory every " + localCleaningRate + " minutes");\r
109                 } else {\r
110                         log.info("Local job directory cleaner is disabled. ");\r
111                 }\r
112         }\r
113 \r
114         static String getClusterJobDir() {\r
115                 String ln = ph.getProperty("cluster.tmp.directory");\r
116                 if (null != ln ) {\r
117                   ln = ln.trim();\r
118                 }\r
119                 return ln;\r
120         }\r
121 \r
122         static String getLocalJobDir() {\r
123                 String ln = ph.getProperty("local.tmp.directory");\r
124                 if (null != ln ) {\r
125                         ln = ln.trim();\r
126                 }\r
127                 return ln;\r
128         }\r
129 }\r