Clean up logging system
[jabaws.git] / webservices / compbio / stat / collector / DirCleaner.java
1 /* Copyright (c) 2013 Alexander Sherstnev\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.1     \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.collector;\r
19 \r
20 import java.io.File;\r
21 import java.io.FileFilter;\r
22 import java.util.Date;\r
23 \r
24 import org.apache.log4j.Logger;\r
25 \r
26 import compbio.engine.Cleaner;\r
27 import compbio.engine.client.PathValidator;\r
28 import compbio.stat.collector.JobStat;\r
29 \r
30 /**\r
31  * Number of runs of each WS = number of folders with name\r
32  * \r
33  * Number of successful runs = all runs with no result file\r
34  * \r
35  * Per period of time = limit per file creating time Runtime (avg/max) =\r
36  * \r
37  * started time - finished time\r
38  * \r
39  * Task & result size = result.size\r
40  * \r
41  * Abandoned runs - not collected runs\r
42  * \r
43  * Cancelled runs - cancelled\r
44  * \r
45  * Cluster vs local runs\r
46  * \r
47  * Reasons for failure = look in the err out?\r
48  * \r
49  * Metadata required:\r
50  * \r
51  * work directory for local and cluster tasks = from Helper or cmd parameter. WS\r
52  * names - enumeration. Status file names and content.\r
53  * \r
54  * @author pvtroshin\r
55  * \r
56  */\r
57 public class DirCleaner implements Runnable {\r
58 \r
59         static final int UNDEFINED = -1;\r
60 \r
61         private static final Logger log = Logger.getLogger(DirCleaner.class);\r
62 \r
63         final private File workDirectory;\r
64         final private int LifeSpanInHours;\r
65 \r
66         /**\r
67          * \r
68          * @param workDirectory\r
69          * @param timeOutInHours\r
70          */\r
71         public DirCleaner(String workDirectory, int LifeSpanInHours) {\r
72                 log.info("Starting cleaning for directory: " + workDirectory);\r
73                 log.info("Maximum allowed directory life span (h): " + LifeSpanInHours);\r
74                 if (!PathValidator.isValidDirectory(workDirectory)) {\r
75                         throw new IllegalArgumentException("workDirectory '" + workDirectory + "' does not exist!");\r
76                 }\r
77                 this.workDirectory = new File(workDirectory);\r
78                 this.LifeSpanInHours = LifeSpanInHours;\r
79         }\r
80 \r
81         boolean hasCompleted(JobDirectory jd) {\r
82                 JobStat jstat = jd.getJobStat();\r
83                 if (jstat.hasResult() || jstat.getIsCancelled() || jstat.getIsFinished()) {\r
84                         return true;\r
85                 }\r
86                 return false;\r
87         }\r
88 \r
89         boolean livesOverLifeSpan(JobDirectory jd) {\r
90                 long LifeTime = (System.currentTimeMillis() - jd.jobdir.lastModified()) / (1000 * 60 * 60);\r
91                 log.debug("lifetime = " + LifeTime + ", lifespan = " + LifeSpanInHours);\r
92                 return LifeTime > LifeSpanInHours;\r
93         }\r
94 \r
95         static FileFilter directories = new FileFilter() {\r
96                 @Override\r
97                 public boolean accept(File pathname) {\r
98                         return pathname.isDirectory() && !pathname.getName().startsWith(".");\r
99                 }\r
100         };\r
101         \r
102         // TODO test!\r
103         void doCleaning() {\r
104                 File[] dirs = workDirectory.listFiles(directories);\r
105                 for (File dir : dirs) {\r
106                         // Do not look at dirs with unfinished jobs\r
107                         JobDirectory jd = new JobDirectory(dir);\r
108                         Date d = new Date (dir.lastModified());\r
109                         log.debug("Directory " + dir.getName() + " has timestamp: " + d);\r
110                         // TODO. removed hasCompeted. Maybe it needs to be restored...\r
111                         // if (hasCompleted(jd) && livesOverLifeSpan(jd)) {\r
112                         if (livesOverLifeSpan(jd)) {\r
113                                 if (Cleaner.deleteDirectory(workDirectory.getAbsolutePath() + File.separator + dir.getName())) {\r
114                                         log.error("Directory " + dir.getName() + " failed to deleted...");\r
115                                 } else {\r
116                                         log.debug("Directory " + dir.getName() + " is deleted");\r
117                                 }\r
118                         } else {\r
119                                 log.debug("Directory " + dir.getName() + " is too new and kept");\r
120                         }\r
121                 }\r
122         }\r
123         @Override\r
124         public void run() {\r
125                 log.info("Started cleaning job directory at " + new Date());\r
126                 log.info("For directory: " + workDirectory.getAbsolutePath());\r
127                 doCleaning();\r
128                 log.info("Finished cleaning job directory at " + new Date());\r
129         }\r
130 }\r