1 /* Copyright (c) 2013 Alexander Sherstnev
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.1
\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.collector;
\r
20 import java.io.File;
\r
21 import java.io.FileFilter;
\r
22 import java.util.Date;
\r
24 import org.apache.log4j.Logger;
\r
26 import compbio.engine.Cleaner;
\r
27 import compbio.engine.client.PathValidator;
\r
28 import compbio.stat.collector.JobStat;
\r
31 * Number of runs of each WS = number of folders with name
\r
33 * Number of successful runs = all runs with no result file
\r
35 * Per period of time = limit per file creating time Runtime (avg/max) =
\r
37 * started time - finished time
\r
39 * Task & result size = result.size
\r
41 * Abandoned runs - not collected runs
\r
43 * Cancelled runs - cancelled
\r
45 * Cluster vs local runs
\r
47 * Reasons for failure = look in the err out?
\r
49 * Metadata required:
\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
57 public class DirCleaner implements Runnable {
\r
59 static final int UNDEFINED = -1;
\r
61 private static final Logger log = Logger.getLogger(DirCleaner.class);
\r
63 final private File workDirectory;
\r
64 final private int LifeSpanInHours;
\r
68 * @param workDirectory
\r
69 * @param timeOutInHours
\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
77 this.workDirectory = new File(workDirectory);
\r
78 this.LifeSpanInHours = LifeSpanInHours;
\r
81 boolean hasCompleted(JobDirectory jd) {
\r
82 JobStat jstat = jd.getJobStat();
\r
83 if (jstat.hasResult() || jstat.getIsCancelled() || jstat.getIsFinished()) {
\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
95 static FileFilter directories = new FileFilter() {
\r
97 public boolean accept(File pathname) {
\r
98 return pathname.isDirectory() && !pathname.getName().startsWith(".");
\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
116 log.debug("Directory " + dir.getName() + " is deleted");
\r
119 log.debug("Directory " + dir.getName() + " is too new and kept");
\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
128 log.info("Finished cleaning job directory at " + new Date());
\r