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